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) 2012 by Delphix. All rights reserved. 24 */ 25 26 #include <sys/dmu.h> 27 #include <sys/dmu_impl.h> 28 #include <sys/dmu_tx.h> 29 #include <sys/dbuf.h> 30 #include <sys/dnode.h> 31 #include <sys/zfs_context.h> 32 #include <sys/dmu_objset.h> 33 #include <sys/dmu_traverse.h> 34 #include <sys/dsl_dataset.h> 35 #include <sys/dsl_dir.h> 36 #include <sys/dsl_pool.h> 37 #include <sys/dsl_synctask.h> 38 #include <sys/dsl_prop.h> 39 #include <sys/dmu_zfetch.h> 40 #include <sys/zfs_ioctl.h> 41 #include <sys/zap.h> 42 #include <sys/zio_checksum.h> 43 #include <sys/zio_compress.h> 44 #include <sys/sa.h> 45 #ifdef _KERNEL 46 #include <sys/vmsystm.h> 47 #include <sys/zfs_znode.h> 48 #endif 49 50 /* 51 * Enable/disable nopwrite feature. 52 */ 53 int zfs_nopwrite_enabled = 1; 54 55 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = { 56 { DMU_BSWAP_UINT8, TRUE, "unallocated" }, 57 { DMU_BSWAP_ZAP, TRUE, "object directory" }, 58 { DMU_BSWAP_UINT64, TRUE, "object array" }, 59 { DMU_BSWAP_UINT8, TRUE, "packed nvlist" }, 60 { DMU_BSWAP_UINT64, TRUE, "packed nvlist size" }, 61 { DMU_BSWAP_UINT64, TRUE, "bpobj" }, 62 { DMU_BSWAP_UINT64, TRUE, "bpobj header" }, 63 { DMU_BSWAP_UINT64, TRUE, "SPA space map header" }, 64 { DMU_BSWAP_UINT64, TRUE, "SPA space map" }, 65 { DMU_BSWAP_UINT64, TRUE, "ZIL intent log" }, 66 { DMU_BSWAP_DNODE, TRUE, "DMU dnode" }, 67 { DMU_BSWAP_OBJSET, TRUE, "DMU objset" }, 68 { DMU_BSWAP_UINT64, TRUE, "DSL directory" }, 69 { DMU_BSWAP_ZAP, TRUE, "DSL directory child map"}, 70 { DMU_BSWAP_ZAP, TRUE, "DSL dataset snap map" }, 71 { DMU_BSWAP_ZAP, TRUE, "DSL props" }, 72 { DMU_BSWAP_UINT64, TRUE, "DSL dataset" }, 73 { DMU_BSWAP_ZNODE, TRUE, "ZFS znode" }, 74 { DMU_BSWAP_OLDACL, TRUE, "ZFS V0 ACL" }, 75 { DMU_BSWAP_UINT8, FALSE, "ZFS plain file" }, 76 { DMU_BSWAP_ZAP, TRUE, "ZFS directory" }, 77 { DMU_BSWAP_ZAP, TRUE, "ZFS master node" }, 78 { DMU_BSWAP_ZAP, TRUE, "ZFS delete queue" }, 79 { DMU_BSWAP_UINT8, FALSE, "zvol object" }, 80 { DMU_BSWAP_ZAP, TRUE, "zvol prop" }, 81 { DMU_BSWAP_UINT8, FALSE, "other uint8[]" }, 82 { DMU_BSWAP_UINT64, FALSE, "other uint64[]" }, 83 { DMU_BSWAP_ZAP, TRUE, "other ZAP" }, 84 { DMU_BSWAP_ZAP, TRUE, "persistent error log" }, 85 { DMU_BSWAP_UINT8, TRUE, "SPA history" }, 86 { DMU_BSWAP_UINT64, TRUE, "SPA history offsets" }, 87 { DMU_BSWAP_ZAP, TRUE, "Pool properties" }, 88 { DMU_BSWAP_ZAP, TRUE, "DSL permissions" }, 89 { DMU_BSWAP_ACL, TRUE, "ZFS ACL" }, 90 { DMU_BSWAP_UINT8, TRUE, "ZFS SYSACL" }, 91 { DMU_BSWAP_UINT8, TRUE, "FUID table" }, 92 { DMU_BSWAP_UINT64, TRUE, "FUID table size" }, 93 { DMU_BSWAP_ZAP, TRUE, "DSL dataset next clones"}, 94 { DMU_BSWAP_ZAP, TRUE, "scan work queue" }, 95 { DMU_BSWAP_ZAP, TRUE, "ZFS user/group used" }, 96 { DMU_BSWAP_ZAP, TRUE, "ZFS user/group quota" }, 97 { DMU_BSWAP_ZAP, TRUE, "snapshot refcount tags"}, 98 { DMU_BSWAP_ZAP, TRUE, "DDT ZAP algorithm" }, 99 { DMU_BSWAP_ZAP, TRUE, "DDT statistics" }, 100 { DMU_BSWAP_UINT8, TRUE, "System attributes" }, 101 { DMU_BSWAP_ZAP, TRUE, "SA master node" }, 102 { DMU_BSWAP_ZAP, TRUE, "SA attr registration" }, 103 { DMU_BSWAP_ZAP, TRUE, "SA attr layouts" }, 104 { DMU_BSWAP_ZAP, TRUE, "scan translations" }, 105 { DMU_BSWAP_UINT8, FALSE, "deduplicated block" }, 106 { DMU_BSWAP_ZAP, TRUE, "DSL deadlist map" }, 107 { DMU_BSWAP_UINT64, TRUE, "DSL deadlist map hdr" }, 108 { DMU_BSWAP_ZAP, TRUE, "DSL dir clones" }, 109 { DMU_BSWAP_UINT64, TRUE, "bpobj subobj" } 110 }; 111 112 const dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = { 113 { byteswap_uint8_array, "uint8" }, 114 { byteswap_uint16_array, "uint16" }, 115 { byteswap_uint32_array, "uint32" }, 116 { byteswap_uint64_array, "uint64" }, 117 { zap_byteswap, "zap" }, 118 { dnode_buf_byteswap, "dnode" }, 119 { dmu_objset_byteswap, "objset" }, 120 { zfs_znode_byteswap, "znode" }, 121 { zfs_oldacl_byteswap, "oldacl" }, 122 { zfs_acl_byteswap, "acl" } 123 }; 124 125 int 126 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset, 127 void *tag, dmu_buf_t **dbp, int flags) 128 { 129 dnode_t *dn; 130 uint64_t blkid; 131 dmu_buf_impl_t *db; 132 int err; 133 int db_flags = DB_RF_CANFAIL; 134 135 if (flags & DMU_READ_NO_PREFETCH) 136 db_flags |= DB_RF_NOPREFETCH; 137 138 err = dnode_hold(os, object, FTAG, &dn); 139 if (err) 140 return (err); 141 blkid = dbuf_whichblock(dn, offset); 142 rw_enter(&dn->dn_struct_rwlock, RW_READER); 143 db = dbuf_hold(dn, blkid, tag); 144 rw_exit(&dn->dn_struct_rwlock); 145 if (db == NULL) { 146 err = EIO; 147 } else { 148 err = dbuf_read(db, NULL, db_flags); 149 if (err) { 150 dbuf_rele(db, tag); 151 db = NULL; 152 } 153 } 154 155 dnode_rele(dn, FTAG); 156 *dbp = &db->db; /* NULL db plus first field offset is NULL */ 157 return (err); 158 } 159 160 int 161 dmu_bonus_max(void) 162 { 163 return (DN_MAX_BONUSLEN); 164 } 165 166 int 167 dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx) 168 { 169 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 170 dnode_t *dn; 171 int error; 172 173 DB_DNODE_ENTER(db); 174 dn = DB_DNODE(db); 175 176 if (dn->dn_bonus != db) { 177 error = EINVAL; 178 } else if (newsize < 0 || newsize > db_fake->db_size) { 179 error = EINVAL; 180 } else { 181 dnode_setbonuslen(dn, newsize, tx); 182 error = 0; 183 } 184 185 DB_DNODE_EXIT(db); 186 return (error); 187 } 188 189 int 190 dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx) 191 { 192 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 193 dnode_t *dn; 194 int error; 195 196 DB_DNODE_ENTER(db); 197 dn = DB_DNODE(db); 198 199 if (!DMU_OT_IS_VALID(type)) { 200 error = EINVAL; 201 } else if (dn->dn_bonus != db) { 202 error = EINVAL; 203 } else { 204 dnode_setbonus_type(dn, type, tx); 205 error = 0; 206 } 207 208 DB_DNODE_EXIT(db); 209 return (error); 210 } 211 212 dmu_object_type_t 213 dmu_get_bonustype(dmu_buf_t *db_fake) 214 { 215 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 216 dnode_t *dn; 217 dmu_object_type_t type; 218 219 DB_DNODE_ENTER(db); 220 dn = DB_DNODE(db); 221 type = dn->dn_bonustype; 222 DB_DNODE_EXIT(db); 223 224 return (type); 225 } 226 227 int 228 dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx) 229 { 230 dnode_t *dn; 231 int error; 232 233 error = dnode_hold(os, object, FTAG, &dn); 234 dbuf_rm_spill(dn, tx); 235 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 236 dnode_rm_spill(dn, tx); 237 rw_exit(&dn->dn_struct_rwlock); 238 dnode_rele(dn, FTAG); 239 return (error); 240 } 241 242 /* 243 * returns ENOENT, EIO, or 0. 244 */ 245 int 246 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp) 247 { 248 dnode_t *dn; 249 dmu_buf_impl_t *db; 250 int error; 251 252 error = dnode_hold(os, object, FTAG, &dn); 253 if (error) 254 return (error); 255 256 rw_enter(&dn->dn_struct_rwlock, RW_READER); 257 if (dn->dn_bonus == NULL) { 258 rw_exit(&dn->dn_struct_rwlock); 259 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 260 if (dn->dn_bonus == NULL) 261 dbuf_create_bonus(dn); 262 } 263 db = dn->dn_bonus; 264 265 /* as long as the bonus buf is held, the dnode will be held */ 266 if (refcount_add(&db->db_holds, tag) == 1) { 267 VERIFY(dnode_add_ref(dn, db)); 268 (void) atomic_inc_32_nv(&dn->dn_dbufs_count); 269 } 270 271 /* 272 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's 273 * hold and incrementing the dbuf count to ensure that dnode_move() sees 274 * a dnode hold for every dbuf. 275 */ 276 rw_exit(&dn->dn_struct_rwlock); 277 278 dnode_rele(dn, FTAG); 279 280 VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH)); 281 282 *dbp = &db->db; 283 return (0); 284 } 285 286 /* 287 * returns ENOENT, EIO, or 0. 288 * 289 * This interface will allocate a blank spill dbuf when a spill blk 290 * doesn't already exist on the dnode. 291 * 292 * if you only want to find an already existing spill db, then 293 * dmu_spill_hold_existing() should be used. 294 */ 295 int 296 dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp) 297 { 298 dmu_buf_impl_t *db = NULL; 299 int err; 300 301 if ((flags & DB_RF_HAVESTRUCT) == 0) 302 rw_enter(&dn->dn_struct_rwlock, RW_READER); 303 304 db = dbuf_hold(dn, DMU_SPILL_BLKID, tag); 305 306 if ((flags & DB_RF_HAVESTRUCT) == 0) 307 rw_exit(&dn->dn_struct_rwlock); 308 309 ASSERT(db != NULL); 310 err = dbuf_read(db, NULL, flags); 311 if (err == 0) 312 *dbp = &db->db; 313 else 314 dbuf_rele(db, tag); 315 return (err); 316 } 317 318 int 319 dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp) 320 { 321 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus; 322 dnode_t *dn; 323 int err; 324 325 DB_DNODE_ENTER(db); 326 dn = DB_DNODE(db); 327 328 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) { 329 err = EINVAL; 330 } else { 331 rw_enter(&dn->dn_struct_rwlock, RW_READER); 332 333 if (!dn->dn_have_spill) { 334 err = ENOENT; 335 } else { 336 err = dmu_spill_hold_by_dnode(dn, 337 DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp); 338 } 339 340 rw_exit(&dn->dn_struct_rwlock); 341 } 342 343 DB_DNODE_EXIT(db); 344 return (err); 345 } 346 347 int 348 dmu_spill_hold_by_bonus(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp) 349 { 350 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus; 351 dnode_t *dn; 352 int err; 353 354 DB_DNODE_ENTER(db); 355 dn = DB_DNODE(db); 356 err = dmu_spill_hold_by_dnode(dn, DB_RF_CANFAIL, tag, dbp); 357 DB_DNODE_EXIT(db); 358 359 return (err); 360 } 361 362 /* 363 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces 364 * to take a held dnode rather than <os, object> -- the lookup is wasteful, 365 * and can induce severe lock contention when writing to several files 366 * whose dnodes are in the same block. 367 */ 368 static int 369 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length, 370 int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags) 371 { 372 dsl_pool_t *dp = NULL; 373 dmu_buf_t **dbp; 374 uint64_t blkid, nblks, i; 375 uint32_t dbuf_flags; 376 int err; 377 zio_t *zio; 378 hrtime_t start; 379 380 ASSERT(length <= DMU_MAX_ACCESS); 381 382 dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT; 383 if (flags & DMU_READ_NO_PREFETCH || length > zfetch_array_rd_sz) 384 dbuf_flags |= DB_RF_NOPREFETCH; 385 386 rw_enter(&dn->dn_struct_rwlock, RW_READER); 387 if (dn->dn_datablkshift) { 388 int blkshift = dn->dn_datablkshift; 389 nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) - 390 P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift; 391 } else { 392 if (offset + length > dn->dn_datablksz) { 393 zfs_panic_recover("zfs: accessing past end of object " 394 "%llx/%llx (size=%u access=%llu+%llu)", 395 (longlong_t)dn->dn_objset-> 396 os_dsl_dataset->ds_object, 397 (longlong_t)dn->dn_object, dn->dn_datablksz, 398 (longlong_t)offset, (longlong_t)length); 399 rw_exit(&dn->dn_struct_rwlock); 400 return (EIO); 401 } 402 nblks = 1; 403 } 404 dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP); 405 406 if (dn->dn_objset->os_dsl_dataset) 407 dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool; 408 if (dp && dsl_pool_sync_context(dp)) 409 start = gethrtime(); 410 zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL); 411 blkid = dbuf_whichblock(dn, offset); 412 for (i = 0; i < nblks; i++) { 413 dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag); 414 if (db == NULL) { 415 rw_exit(&dn->dn_struct_rwlock); 416 dmu_buf_rele_array(dbp, nblks, tag); 417 zio_nowait(zio); 418 return (EIO); 419 } 420 /* initiate async i/o */ 421 if (read) { 422 (void) dbuf_read(db, zio, dbuf_flags); 423 } 424 dbp[i] = &db->db; 425 } 426 rw_exit(&dn->dn_struct_rwlock); 427 428 /* wait for async i/o */ 429 err = zio_wait(zio); 430 /* track read overhead when we are in sync context */ 431 if (dp && dsl_pool_sync_context(dp)) 432 dp->dp_read_overhead += gethrtime() - start; 433 if (err) { 434 dmu_buf_rele_array(dbp, nblks, tag); 435 return (err); 436 } 437 438 /* wait for other io to complete */ 439 if (read) { 440 for (i = 0; i < nblks; i++) { 441 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i]; 442 mutex_enter(&db->db_mtx); 443 while (db->db_state == DB_READ || 444 db->db_state == DB_FILL) 445 cv_wait(&db->db_changed, &db->db_mtx); 446 if (db->db_state == DB_UNCACHED) 447 err = EIO; 448 mutex_exit(&db->db_mtx); 449 if (err) { 450 dmu_buf_rele_array(dbp, nblks, tag); 451 return (err); 452 } 453 } 454 } 455 456 *numbufsp = nblks; 457 *dbpp = dbp; 458 return (0); 459 } 460 461 static int 462 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset, 463 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp) 464 { 465 dnode_t *dn; 466 int err; 467 468 err = dnode_hold(os, object, FTAG, &dn); 469 if (err) 470 return (err); 471 472 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 473 numbufsp, dbpp, DMU_READ_PREFETCH); 474 475 dnode_rele(dn, FTAG); 476 477 return (err); 478 } 479 480 int 481 dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset, 482 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp) 483 { 484 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 485 dnode_t *dn; 486 int err; 487 488 DB_DNODE_ENTER(db); 489 dn = DB_DNODE(db); 490 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 491 numbufsp, dbpp, DMU_READ_PREFETCH); 492 DB_DNODE_EXIT(db); 493 494 return (err); 495 } 496 497 void 498 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag) 499 { 500 int i; 501 dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake; 502 503 if (numbufs == 0) 504 return; 505 506 for (i = 0; i < numbufs; i++) { 507 if (dbp[i]) 508 dbuf_rele(dbp[i], tag); 509 } 510 511 kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs); 512 } 513 514 void 515 dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len) 516 { 517 dnode_t *dn; 518 uint64_t blkid; 519 int nblks, i, err; 520 521 if (zfs_prefetch_disable) 522 return; 523 524 if (len == 0) { /* they're interested in the bonus buffer */ 525 dn = DMU_META_DNODE(os); 526 527 if (object == 0 || object >= DN_MAX_OBJECT) 528 return; 529 530 rw_enter(&dn->dn_struct_rwlock, RW_READER); 531 blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t)); 532 dbuf_prefetch(dn, blkid); 533 rw_exit(&dn->dn_struct_rwlock); 534 return; 535 } 536 537 /* 538 * XXX - Note, if the dnode for the requested object is not 539 * already cached, we will do a *synchronous* read in the 540 * dnode_hold() call. The same is true for any indirects. 541 */ 542 err = dnode_hold(os, object, FTAG, &dn); 543 if (err != 0) 544 return; 545 546 rw_enter(&dn->dn_struct_rwlock, RW_READER); 547 if (dn->dn_datablkshift) { 548 int blkshift = dn->dn_datablkshift; 549 nblks = (P2ROUNDUP(offset+len, 1<<blkshift) - 550 P2ALIGN(offset, 1<<blkshift)) >> blkshift; 551 } else { 552 nblks = (offset < dn->dn_datablksz); 553 } 554 555 if (nblks != 0) { 556 blkid = dbuf_whichblock(dn, offset); 557 for (i = 0; i < nblks; i++) 558 dbuf_prefetch(dn, blkid+i); 559 } 560 561 rw_exit(&dn->dn_struct_rwlock); 562 563 dnode_rele(dn, FTAG); 564 } 565 566 /* 567 * Get the next "chunk" of file data to free. We traverse the file from 568 * the end so that the file gets shorter over time (if we crashes in the 569 * middle, this will leave us in a better state). We find allocated file 570 * data by simply searching the allocated level 1 indirects. 571 */ 572 static int 573 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t limit) 574 { 575 uint64_t len = *start - limit; 576 uint64_t blkcnt = 0; 577 uint64_t maxblks = DMU_MAX_ACCESS / (1ULL << (dn->dn_indblkshift + 1)); 578 uint64_t iblkrange = 579 dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT); 580 581 ASSERT(limit <= *start); 582 583 if (len <= iblkrange * maxblks) { 584 *start = limit; 585 return (0); 586 } 587 ASSERT(ISP2(iblkrange)); 588 589 while (*start > limit && blkcnt < maxblks) { 590 int err; 591 592 /* find next allocated L1 indirect */ 593 err = dnode_next_offset(dn, 594 DNODE_FIND_BACKWARDS, start, 2, 1, 0); 595 596 /* if there are no more, then we are done */ 597 if (err == ESRCH) { 598 *start = limit; 599 return (0); 600 } else if (err) { 601 return (err); 602 } 603 blkcnt += 1; 604 605 /* reset offset to end of "next" block back */ 606 *start = P2ALIGN(*start, iblkrange); 607 if (*start <= limit) 608 *start = limit; 609 else 610 *start -= 1; 611 } 612 return (0); 613 } 614 615 static int 616 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset, 617 uint64_t length, boolean_t free_dnode) 618 { 619 dmu_tx_t *tx; 620 uint64_t object_size, start, end, len; 621 boolean_t trunc = (length == DMU_OBJECT_END); 622 int align, err; 623 624 align = 1 << dn->dn_datablkshift; 625 ASSERT(align > 0); 626 object_size = align == 1 ? dn->dn_datablksz : 627 (dn->dn_maxblkid + 1) << dn->dn_datablkshift; 628 629 end = offset + length; 630 if (trunc || end > object_size) 631 end = object_size; 632 if (end <= offset) 633 return (0); 634 length = end - offset; 635 636 while (length) { 637 start = end; 638 /* assert(offset <= start) */ 639 err = get_next_chunk(dn, &start, offset); 640 if (err) 641 return (err); 642 len = trunc ? DMU_OBJECT_END : end - start; 643 644 tx = dmu_tx_create(os); 645 dmu_tx_hold_free(tx, dn->dn_object, start, len); 646 err = dmu_tx_assign(tx, TXG_WAIT); 647 if (err) { 648 dmu_tx_abort(tx); 649 return (err); 650 } 651 652 dnode_free_range(dn, start, trunc ? -1 : len, tx); 653 654 if (start == 0 && free_dnode) { 655 ASSERT(trunc); 656 dnode_free(dn, tx); 657 } 658 659 length -= end - start; 660 661 dmu_tx_commit(tx); 662 end = start; 663 } 664 return (0); 665 } 666 667 int 668 dmu_free_long_range(objset_t *os, uint64_t object, 669 uint64_t offset, uint64_t length) 670 { 671 dnode_t *dn; 672 int err; 673 674 err = dnode_hold(os, object, FTAG, &dn); 675 if (err != 0) 676 return (err); 677 err = dmu_free_long_range_impl(os, dn, offset, length, FALSE); 678 dnode_rele(dn, FTAG); 679 return (err); 680 } 681 682 int 683 dmu_free_object(objset_t *os, uint64_t object) 684 { 685 dnode_t *dn; 686 dmu_tx_t *tx; 687 int err; 688 689 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 690 FTAG, &dn); 691 if (err != 0) 692 return (err); 693 if (dn->dn_nlevels == 1) { 694 tx = dmu_tx_create(os); 695 dmu_tx_hold_bonus(tx, object); 696 dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END); 697 err = dmu_tx_assign(tx, TXG_WAIT); 698 if (err == 0) { 699 dnode_free_range(dn, 0, DMU_OBJECT_END, tx); 700 dnode_free(dn, tx); 701 dmu_tx_commit(tx); 702 } else { 703 dmu_tx_abort(tx); 704 } 705 } else { 706 err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE); 707 } 708 dnode_rele(dn, FTAG); 709 return (err); 710 } 711 712 int 713 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset, 714 uint64_t size, dmu_tx_t *tx) 715 { 716 dnode_t *dn; 717 int err = dnode_hold(os, object, FTAG, &dn); 718 if (err) 719 return (err); 720 ASSERT(offset < UINT64_MAX); 721 ASSERT(size == -1ULL || size <= UINT64_MAX - offset); 722 dnode_free_range(dn, offset, size, tx); 723 dnode_rele(dn, FTAG); 724 return (0); 725 } 726 727 int 728 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 729 void *buf, uint32_t flags) 730 { 731 dnode_t *dn; 732 dmu_buf_t **dbp; 733 int numbufs, err; 734 735 err = dnode_hold(os, object, FTAG, &dn); 736 if (err) 737 return (err); 738 739 /* 740 * Deal with odd block sizes, where there can't be data past the first 741 * block. If we ever do the tail block optimization, we will need to 742 * handle that here as well. 743 */ 744 if (dn->dn_maxblkid == 0) { 745 int newsz = offset > dn->dn_datablksz ? 0 : 746 MIN(size, dn->dn_datablksz - offset); 747 bzero((char *)buf + newsz, size - newsz); 748 size = newsz; 749 } 750 751 while (size > 0) { 752 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2); 753 int i; 754 755 /* 756 * NB: we could do this block-at-a-time, but it's nice 757 * to be reading in parallel. 758 */ 759 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen, 760 TRUE, FTAG, &numbufs, &dbp, flags); 761 if (err) 762 break; 763 764 for (i = 0; i < numbufs; i++) { 765 int tocpy; 766 int bufoff; 767 dmu_buf_t *db = dbp[i]; 768 769 ASSERT(size > 0); 770 771 bufoff = offset - db->db_offset; 772 tocpy = (int)MIN(db->db_size - bufoff, size); 773 774 bcopy((char *)db->db_data + bufoff, buf, tocpy); 775 776 offset += tocpy; 777 size -= tocpy; 778 buf = (char *)buf + tocpy; 779 } 780 dmu_buf_rele_array(dbp, numbufs, FTAG); 781 } 782 dnode_rele(dn, FTAG); 783 return (err); 784 } 785 786 void 787 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 788 const void *buf, dmu_tx_t *tx) 789 { 790 dmu_buf_t **dbp; 791 int numbufs, i; 792 793 if (size == 0) 794 return; 795 796 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size, 797 FALSE, FTAG, &numbufs, &dbp)); 798 799 for (i = 0; i < numbufs; i++) { 800 int tocpy; 801 int bufoff; 802 dmu_buf_t *db = dbp[i]; 803 804 ASSERT(size > 0); 805 806 bufoff = offset - db->db_offset; 807 tocpy = (int)MIN(db->db_size - bufoff, size); 808 809 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 810 811 if (tocpy == db->db_size) 812 dmu_buf_will_fill(db, tx); 813 else 814 dmu_buf_will_dirty(db, tx); 815 816 bcopy(buf, (char *)db->db_data + bufoff, tocpy); 817 818 if (tocpy == db->db_size) 819 dmu_buf_fill_done(db, tx); 820 821 offset += tocpy; 822 size -= tocpy; 823 buf = (char *)buf + tocpy; 824 } 825 dmu_buf_rele_array(dbp, numbufs, FTAG); 826 } 827 828 void 829 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 830 dmu_tx_t *tx) 831 { 832 dmu_buf_t **dbp; 833 int numbufs, i; 834 835 if (size == 0) 836 return; 837 838 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size, 839 FALSE, FTAG, &numbufs, &dbp)); 840 841 for (i = 0; i < numbufs; i++) { 842 dmu_buf_t *db = dbp[i]; 843 844 dmu_buf_will_not_fill(db, tx); 845 } 846 dmu_buf_rele_array(dbp, numbufs, FTAG); 847 } 848 849 /* 850 * DMU support for xuio 851 */ 852 kstat_t *xuio_ksp = NULL; 853 854 int 855 dmu_xuio_init(xuio_t *xuio, int nblk) 856 { 857 dmu_xuio_t *priv; 858 uio_t *uio = &xuio->xu_uio; 859 860 uio->uio_iovcnt = nblk; 861 uio->uio_iov = kmem_zalloc(nblk * sizeof (iovec_t), KM_SLEEP); 862 863 priv = kmem_zalloc(sizeof (dmu_xuio_t), KM_SLEEP); 864 priv->cnt = nblk; 865 priv->bufs = kmem_zalloc(nblk * sizeof (arc_buf_t *), KM_SLEEP); 866 priv->iovp = uio->uio_iov; 867 XUIO_XUZC_PRIV(xuio) = priv; 868 869 if (XUIO_XUZC_RW(xuio) == UIO_READ) 870 XUIOSTAT_INCR(xuiostat_onloan_rbuf, nblk); 871 else 872 XUIOSTAT_INCR(xuiostat_onloan_wbuf, nblk); 873 874 return (0); 875 } 876 877 void 878 dmu_xuio_fini(xuio_t *xuio) 879 { 880 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio); 881 int nblk = priv->cnt; 882 883 kmem_free(priv->iovp, nblk * sizeof (iovec_t)); 884 kmem_free(priv->bufs, nblk * sizeof (arc_buf_t *)); 885 kmem_free(priv, sizeof (dmu_xuio_t)); 886 887 if (XUIO_XUZC_RW(xuio) == UIO_READ) 888 XUIOSTAT_INCR(xuiostat_onloan_rbuf, -nblk); 889 else 890 XUIOSTAT_INCR(xuiostat_onloan_wbuf, -nblk); 891 } 892 893 /* 894 * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf } 895 * and increase priv->next by 1. 896 */ 897 int 898 dmu_xuio_add(xuio_t *xuio, arc_buf_t *abuf, offset_t off, size_t n) 899 { 900 struct iovec *iov; 901 uio_t *uio = &xuio->xu_uio; 902 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio); 903 int i = priv->next++; 904 905 ASSERT(i < priv->cnt); 906 ASSERT(off + n <= arc_buf_size(abuf)); 907 iov = uio->uio_iov + i; 908 iov->iov_base = (char *)abuf->b_data + off; 909 iov->iov_len = n; 910 priv->bufs[i] = abuf; 911 return (0); 912 } 913 914 int 915 dmu_xuio_cnt(xuio_t *xuio) 916 { 917 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio); 918 return (priv->cnt); 919 } 920 921 arc_buf_t * 922 dmu_xuio_arcbuf(xuio_t *xuio, int i) 923 { 924 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio); 925 926 ASSERT(i < priv->cnt); 927 return (priv->bufs[i]); 928 } 929 930 void 931 dmu_xuio_clear(xuio_t *xuio, int i) 932 { 933 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio); 934 935 ASSERT(i < priv->cnt); 936 priv->bufs[i] = NULL; 937 } 938 939 static void 940 xuio_stat_init(void) 941 { 942 xuio_ksp = kstat_create("zfs", 0, "xuio_stats", "misc", 943 KSTAT_TYPE_NAMED, sizeof (xuio_stats) / sizeof (kstat_named_t), 944 KSTAT_FLAG_VIRTUAL); 945 if (xuio_ksp != NULL) { 946 xuio_ksp->ks_data = &xuio_stats; 947 kstat_install(xuio_ksp); 948 } 949 } 950 951 static void 952 xuio_stat_fini(void) 953 { 954 if (xuio_ksp != NULL) { 955 kstat_delete(xuio_ksp); 956 xuio_ksp = NULL; 957 } 958 } 959 960 void 961 xuio_stat_wbuf_copied() 962 { 963 XUIOSTAT_BUMP(xuiostat_wbuf_copied); 964 } 965 966 void 967 xuio_stat_wbuf_nocopy() 968 { 969 XUIOSTAT_BUMP(xuiostat_wbuf_nocopy); 970 } 971 972 #ifdef _KERNEL 973 int 974 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size) 975 { 976 dmu_buf_t **dbp; 977 int numbufs, i, err; 978 xuio_t *xuio = NULL; 979 980 /* 981 * NB: we could do this block-at-a-time, but it's nice 982 * to be reading in parallel. 983 */ 984 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG, 985 &numbufs, &dbp); 986 if (err) 987 return (err); 988 989 if (uio->uio_extflg == UIO_XUIO) 990 xuio = (xuio_t *)uio; 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 = uio->uio_loffset - db->db_offset; 1000 tocpy = (int)MIN(db->db_size - bufoff, size); 1001 1002 if (xuio) { 1003 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 1004 arc_buf_t *dbuf_abuf = dbi->db_buf; 1005 arc_buf_t *abuf = dbuf_loan_arcbuf(dbi); 1006 err = dmu_xuio_add(xuio, abuf, bufoff, tocpy); 1007 if (!err) { 1008 uio->uio_resid -= tocpy; 1009 uio->uio_loffset += tocpy; 1010 } 1011 1012 if (abuf == dbuf_abuf) 1013 XUIOSTAT_BUMP(xuiostat_rbuf_nocopy); 1014 else 1015 XUIOSTAT_BUMP(xuiostat_rbuf_copied); 1016 } else { 1017 err = uiomove((char *)db->db_data + bufoff, tocpy, 1018 UIO_READ, uio); 1019 } 1020 if (err) 1021 break; 1022 1023 size -= tocpy; 1024 } 1025 dmu_buf_rele_array(dbp, numbufs, FTAG); 1026 1027 return (err); 1028 } 1029 1030 static int 1031 dmu_write_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size, dmu_tx_t *tx) 1032 { 1033 dmu_buf_t **dbp; 1034 int numbufs; 1035 int err = 0; 1036 int i; 1037 1038 err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size, 1039 FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH); 1040 if (err) 1041 return (err); 1042 1043 for (i = 0; i < numbufs; i++) { 1044 int tocpy; 1045 int bufoff; 1046 dmu_buf_t *db = dbp[i]; 1047 1048 ASSERT(size > 0); 1049 1050 bufoff = uio->uio_loffset - db->db_offset; 1051 tocpy = (int)MIN(db->db_size - bufoff, size); 1052 1053 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 1054 1055 if (tocpy == db->db_size) 1056 dmu_buf_will_fill(db, tx); 1057 else 1058 dmu_buf_will_dirty(db, tx); 1059 1060 /* 1061 * XXX uiomove could block forever (eg. nfs-backed 1062 * pages). There needs to be a uiolockdown() function 1063 * to lock the pages in memory, so that uiomove won't 1064 * block. 1065 */ 1066 err = uiomove((char *)db->db_data + bufoff, tocpy, 1067 UIO_WRITE, uio); 1068 1069 if (tocpy == db->db_size) 1070 dmu_buf_fill_done(db, tx); 1071 1072 if (err) 1073 break; 1074 1075 size -= tocpy; 1076 } 1077 1078 dmu_buf_rele_array(dbp, numbufs, FTAG); 1079 return (err); 1080 } 1081 1082 int 1083 dmu_write_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size, 1084 dmu_tx_t *tx) 1085 { 1086 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb; 1087 dnode_t *dn; 1088 int err; 1089 1090 if (size == 0) 1091 return (0); 1092 1093 DB_DNODE_ENTER(db); 1094 dn = DB_DNODE(db); 1095 err = dmu_write_uio_dnode(dn, uio, size, tx); 1096 DB_DNODE_EXIT(db); 1097 1098 return (err); 1099 } 1100 1101 int 1102 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size, 1103 dmu_tx_t *tx) 1104 { 1105 dnode_t *dn; 1106 int err; 1107 1108 if (size == 0) 1109 return (0); 1110 1111 err = dnode_hold(os, object, FTAG, &dn); 1112 if (err) 1113 return (err); 1114 1115 err = dmu_write_uio_dnode(dn, uio, size, tx); 1116 1117 dnode_rele(dn, FTAG); 1118 1119 return (err); 1120 } 1121 1122 int 1123 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1124 page_t *pp, dmu_tx_t *tx) 1125 { 1126 dmu_buf_t **dbp; 1127 int numbufs, i; 1128 int err; 1129 1130 if (size == 0) 1131 return (0); 1132 1133 err = dmu_buf_hold_array(os, object, offset, size, 1134 FALSE, FTAG, &numbufs, &dbp); 1135 if (err) 1136 return (err); 1137 1138 for (i = 0; i < numbufs; i++) { 1139 int tocpy, copied, thiscpy; 1140 int bufoff; 1141 dmu_buf_t *db = dbp[i]; 1142 caddr_t va; 1143 1144 ASSERT(size > 0); 1145 ASSERT3U(db->db_size, >=, PAGESIZE); 1146 1147 bufoff = offset - db->db_offset; 1148 tocpy = (int)MIN(db->db_size - bufoff, size); 1149 1150 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 1151 1152 if (tocpy == db->db_size) 1153 dmu_buf_will_fill(db, tx); 1154 else 1155 dmu_buf_will_dirty(db, tx); 1156 1157 for (copied = 0; copied < tocpy; copied += PAGESIZE) { 1158 ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff); 1159 thiscpy = MIN(PAGESIZE, tocpy - copied); 1160 va = zfs_map_page(pp, S_READ); 1161 bcopy(va, (char *)db->db_data + bufoff, thiscpy); 1162 zfs_unmap_page(pp, va); 1163 pp = pp->p_next; 1164 bufoff += PAGESIZE; 1165 } 1166 1167 if (tocpy == db->db_size) 1168 dmu_buf_fill_done(db, tx); 1169 1170 offset += tocpy; 1171 size -= tocpy; 1172 } 1173 dmu_buf_rele_array(dbp, numbufs, FTAG); 1174 return (err); 1175 } 1176 #endif 1177 1178 /* 1179 * Allocate a loaned anonymous arc buffer. 1180 */ 1181 arc_buf_t * 1182 dmu_request_arcbuf(dmu_buf_t *handle, int size) 1183 { 1184 dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle; 1185 spa_t *spa; 1186 1187 DB_GET_SPA(&spa, db); 1188 return (arc_loan_buf(spa, size)); 1189 } 1190 1191 /* 1192 * Free a loaned arc buffer. 1193 */ 1194 void 1195 dmu_return_arcbuf(arc_buf_t *buf) 1196 { 1197 arc_return_buf(buf, FTAG); 1198 VERIFY(arc_buf_remove_ref(buf, FTAG) == 1); 1199 } 1200 1201 /* 1202 * When possible directly assign passed loaned arc buffer to a dbuf. 1203 * If this is not possible copy the contents of passed arc buf via 1204 * dmu_write(). 1205 */ 1206 void 1207 dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf, 1208 dmu_tx_t *tx) 1209 { 1210 dmu_buf_impl_t *dbuf = (dmu_buf_impl_t *)handle; 1211 dnode_t *dn; 1212 dmu_buf_impl_t *db; 1213 uint32_t blksz = (uint32_t)arc_buf_size(buf); 1214 uint64_t blkid; 1215 1216 DB_DNODE_ENTER(dbuf); 1217 dn = DB_DNODE(dbuf); 1218 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1219 blkid = dbuf_whichblock(dn, offset); 1220 VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL); 1221 rw_exit(&dn->dn_struct_rwlock); 1222 DB_DNODE_EXIT(dbuf); 1223 1224 if (offset == db->db.db_offset && blksz == db->db.db_size) { 1225 dbuf_assign_arcbuf(db, buf, tx); 1226 dbuf_rele(db, FTAG); 1227 } else { 1228 objset_t *os; 1229 uint64_t object; 1230 1231 DB_DNODE_ENTER(dbuf); 1232 dn = DB_DNODE(dbuf); 1233 os = dn->dn_objset; 1234 object = dn->dn_object; 1235 DB_DNODE_EXIT(dbuf); 1236 1237 dbuf_rele(db, FTAG); 1238 dmu_write(os, object, offset, blksz, buf->b_data, tx); 1239 dmu_return_arcbuf(buf); 1240 XUIOSTAT_BUMP(xuiostat_wbuf_copied); 1241 } 1242 } 1243 1244 typedef struct { 1245 dbuf_dirty_record_t *dsa_dr; 1246 dmu_sync_cb_t *dsa_done; 1247 zgd_t *dsa_zgd; 1248 dmu_tx_t *dsa_tx; 1249 } dmu_sync_arg_t; 1250 1251 /* ARGSUSED */ 1252 static void 1253 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg) 1254 { 1255 dmu_sync_arg_t *dsa = varg; 1256 dmu_buf_t *db = dsa->dsa_zgd->zgd_db; 1257 blkptr_t *bp = zio->io_bp; 1258 1259 if (zio->io_error == 0) { 1260 if (BP_IS_HOLE(bp)) { 1261 /* 1262 * A block of zeros may compress to a hole, but the 1263 * block size still needs to be known for replay. 1264 */ 1265 BP_SET_LSIZE(bp, db->db_size); 1266 } else { 1267 ASSERT(BP_GET_LEVEL(bp) == 0); 1268 bp->blk_fill = 1; 1269 } 1270 } 1271 } 1272 1273 static void 1274 dmu_sync_late_arrival_ready(zio_t *zio) 1275 { 1276 dmu_sync_ready(zio, NULL, zio->io_private); 1277 } 1278 1279 /* ARGSUSED */ 1280 static void 1281 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg) 1282 { 1283 dmu_sync_arg_t *dsa = varg; 1284 dbuf_dirty_record_t *dr = dsa->dsa_dr; 1285 dmu_buf_impl_t *db = dr->dr_dbuf; 1286 1287 mutex_enter(&db->db_mtx); 1288 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC); 1289 if (zio->io_error == 0) { 1290 dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE); 1291 if (dr->dt.dl.dr_nopwrite) { 1292 blkptr_t *bp = zio->io_bp; 1293 blkptr_t *bp_orig = &zio->io_bp_orig; 1294 uint8_t chksum = BP_GET_CHECKSUM(bp_orig); 1295 1296 ASSERT(BP_EQUAL(bp, bp_orig)); 1297 ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF); 1298 ASSERT(zio_checksum_table[chksum].ci_dedup); 1299 } 1300 dr->dt.dl.dr_overridden_by = *zio->io_bp; 1301 dr->dt.dl.dr_override_state = DR_OVERRIDDEN; 1302 dr->dt.dl.dr_copies = zio->io_prop.zp_copies; 1303 if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by)) 1304 BP_ZERO(&dr->dt.dl.dr_overridden_by); 1305 } else { 1306 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 1307 } 1308 cv_broadcast(&db->db_changed); 1309 mutex_exit(&db->db_mtx); 1310 1311 dsa->dsa_done(dsa->dsa_zgd, zio->io_error); 1312 1313 kmem_free(dsa, sizeof (*dsa)); 1314 } 1315 1316 static void 1317 dmu_sync_late_arrival_done(zio_t *zio) 1318 { 1319 blkptr_t *bp = zio->io_bp; 1320 dmu_sync_arg_t *dsa = zio->io_private; 1321 blkptr_t *bp_orig = &zio->io_bp_orig; 1322 1323 if (zio->io_error == 0 && !BP_IS_HOLE(bp)) { 1324 /* 1325 * If we didn't allocate a new block (i.e. ZIO_FLAG_NOPWRITE) 1326 * then there is nothing to do here. Otherwise, free the 1327 * newly allocated block in this txg. 1328 */ 1329 if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 1330 ASSERT(BP_EQUAL(bp, bp_orig)); 1331 } else { 1332 ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig)); 1333 ASSERT(zio->io_bp->blk_birth == zio->io_txg); 1334 ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa)); 1335 zio_free(zio->io_spa, zio->io_txg, zio->io_bp); 1336 } 1337 } 1338 1339 dmu_tx_commit(dsa->dsa_tx); 1340 1341 dsa->dsa_done(dsa->dsa_zgd, zio->io_error); 1342 1343 kmem_free(dsa, sizeof (*dsa)); 1344 } 1345 1346 static int 1347 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd, 1348 zio_prop_t *zp, zbookmark_t *zb) 1349 { 1350 dmu_sync_arg_t *dsa; 1351 dmu_tx_t *tx; 1352 1353 tx = dmu_tx_create(os); 1354 dmu_tx_hold_space(tx, zgd->zgd_db->db_size); 1355 if (dmu_tx_assign(tx, TXG_WAIT) != 0) { 1356 dmu_tx_abort(tx); 1357 return (EIO); /* Make zl_get_data do txg_waited_synced() */ 1358 } 1359 1360 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP); 1361 dsa->dsa_dr = NULL; 1362 dsa->dsa_done = done; 1363 dsa->dsa_zgd = zgd; 1364 dsa->dsa_tx = tx; 1365 1366 zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp, 1367 zgd->zgd_db->db_data, zgd->zgd_db->db_size, zp, 1368 dmu_sync_late_arrival_ready, dmu_sync_late_arrival_done, dsa, 1369 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb)); 1370 1371 return (0); 1372 } 1373 1374 /* 1375 * Intent log support: sync the block associated with db to disk. 1376 * N.B. and XXX: the caller is responsible for making sure that the 1377 * data isn't changing while dmu_sync() is writing it. 1378 * 1379 * Return values: 1380 * 1381 * EEXIST: this txg has already been synced, so there's nothing to do. 1382 * The caller should not log the write. 1383 * 1384 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do. 1385 * The caller should not log the write. 1386 * 1387 * EALREADY: this block is already in the process of being synced. 1388 * The caller should track its progress (somehow). 1389 * 1390 * EIO: could not do the I/O. 1391 * The caller should do a txg_wait_synced(). 1392 * 1393 * 0: the I/O has been initiated. 1394 * The caller should log this blkptr in the done callback. 1395 * It is possible that the I/O will fail, in which case 1396 * the error will be reported to the done callback and 1397 * propagated to pio from zio_done(). 1398 */ 1399 int 1400 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd) 1401 { 1402 blkptr_t *bp = zgd->zgd_bp; 1403 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db; 1404 objset_t *os = db->db_objset; 1405 dsl_dataset_t *ds = os->os_dsl_dataset; 1406 dbuf_dirty_record_t *dr; 1407 dmu_sync_arg_t *dsa; 1408 zbookmark_t zb; 1409 zio_prop_t zp; 1410 dnode_t *dn; 1411 1412 ASSERT(pio != NULL); 1413 ASSERT(txg != 0); 1414 1415 SET_BOOKMARK(&zb, ds->ds_object, 1416 db->db.db_object, db->db_level, db->db_blkid); 1417 1418 DB_DNODE_ENTER(db); 1419 dn = DB_DNODE(db); 1420 dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp); 1421 DB_DNODE_EXIT(db); 1422 1423 /* 1424 * If we're frozen (running ziltest), we always need to generate a bp. 1425 */ 1426 if (txg > spa_freeze_txg(os->os_spa)) 1427 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb)); 1428 1429 /* 1430 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf() 1431 * and us. If we determine that this txg is not yet syncing, 1432 * but it begins to sync a moment later, that's OK because the 1433 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx. 1434 */ 1435 mutex_enter(&db->db_mtx); 1436 1437 if (txg <= spa_last_synced_txg(os->os_spa)) { 1438 /* 1439 * This txg has already synced. There's nothing to do. 1440 */ 1441 mutex_exit(&db->db_mtx); 1442 return (EEXIST); 1443 } 1444 1445 if (txg <= spa_syncing_txg(os->os_spa)) { 1446 /* 1447 * This txg is currently syncing, so we can't mess with 1448 * the dirty record anymore; just write a new log block. 1449 */ 1450 mutex_exit(&db->db_mtx); 1451 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb)); 1452 } 1453 1454 dr = db->db_last_dirty; 1455 while (dr && dr->dr_txg != txg) 1456 dr = dr->dr_next; 1457 1458 if (dr == NULL) { 1459 /* 1460 * There's no dr for this dbuf, so it must have been freed. 1461 * There's no need to log writes to freed blocks, so we're done. 1462 */ 1463 mutex_exit(&db->db_mtx); 1464 return (ENOENT); 1465 } 1466 1467 ASSERT(dr->dr_next == NULL || dr->dr_next->dr_txg < txg); 1468 1469 /* 1470 * Assume the on-disk data is X, the current syncing data is Y, 1471 * and the current in-memory data is Z (currently in dmu_sync). 1472 * X and Z are identical but Y is has been modified. Normally, 1473 * when X and Z are the same we will perform a nopwrite but if Y 1474 * is different we must disable nopwrite since the resulting write 1475 * of Y to disk can free the block containing X. If we allowed a 1476 * nopwrite to occur the block pointing to Z would reference a freed 1477 * block. Since this is a rare case we simplify this by disabling 1478 * nopwrite if the current dmu_sync-ing dbuf has been modified in 1479 * a previous transaction. 1480 */ 1481 if (dr->dr_next) 1482 zp.zp_nopwrite = B_FALSE; 1483 1484 ASSERT(dr->dr_txg == txg); 1485 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC || 1486 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 1487 /* 1488 * We have already issued a sync write for this buffer, 1489 * or this buffer has already been synced. It could not 1490 * have been dirtied since, or we would have cleared the state. 1491 */ 1492 mutex_exit(&db->db_mtx); 1493 return (EALREADY); 1494 } 1495 1496 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN); 1497 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC; 1498 mutex_exit(&db->db_mtx); 1499 1500 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP); 1501 dsa->dsa_dr = dr; 1502 dsa->dsa_done = done; 1503 dsa->dsa_zgd = zgd; 1504 dsa->dsa_tx = NULL; 1505 1506 zio_nowait(arc_write(pio, os->os_spa, txg, 1507 bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db), &zp, 1508 dmu_sync_ready, dmu_sync_done, dsa, 1509 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, &zb)); 1510 1511 return (0); 1512 } 1513 1514 int 1515 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs, 1516 dmu_tx_t *tx) 1517 { 1518 dnode_t *dn; 1519 int err; 1520 1521 err = dnode_hold(os, object, FTAG, &dn); 1522 if (err) 1523 return (err); 1524 err = dnode_set_blksz(dn, size, ibs, tx); 1525 dnode_rele(dn, FTAG); 1526 return (err); 1527 } 1528 1529 void 1530 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum, 1531 dmu_tx_t *tx) 1532 { 1533 dnode_t *dn; 1534 1535 /* XXX assumes dnode_hold will not get an i/o error */ 1536 (void) dnode_hold(os, object, FTAG, &dn); 1537 ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS); 1538 dn->dn_checksum = checksum; 1539 dnode_setdirty(dn, tx); 1540 dnode_rele(dn, FTAG); 1541 } 1542 1543 void 1544 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress, 1545 dmu_tx_t *tx) 1546 { 1547 dnode_t *dn; 1548 1549 /* XXX assumes dnode_hold will not get an i/o error */ 1550 (void) dnode_hold(os, object, FTAG, &dn); 1551 ASSERT(compress < ZIO_COMPRESS_FUNCTIONS); 1552 dn->dn_compress = compress; 1553 dnode_setdirty(dn, tx); 1554 dnode_rele(dn, FTAG); 1555 } 1556 1557 int zfs_mdcomp_disable = 0; 1558 1559 void 1560 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp) 1561 { 1562 dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET; 1563 boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) || 1564 (wp & WP_SPILL)); 1565 enum zio_checksum checksum = os->os_checksum; 1566 enum zio_compress compress = os->os_compress; 1567 enum zio_checksum dedup_checksum = os->os_dedup_checksum; 1568 boolean_t dedup = B_FALSE; 1569 boolean_t nopwrite = B_FALSE; 1570 boolean_t dedup_verify = os->os_dedup_verify; 1571 int copies = os->os_copies; 1572 1573 /* 1574 * We maintain different write policies for each of the following 1575 * types of data: 1576 * 1. metadata 1577 * 2. preallocated blocks (i.e. level-0 blocks of a dump device) 1578 * 3. all other level 0 blocks 1579 */ 1580 if (ismd) { 1581 /* 1582 * XXX -- we should design a compression algorithm 1583 * that specializes in arrays of bps. 1584 */ 1585 compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY : 1586 ZIO_COMPRESS_LZJB; 1587 1588 /* 1589 * Metadata always gets checksummed. If the data 1590 * checksum is multi-bit correctable, and it's not a 1591 * ZBT-style checksum, then it's suitable for metadata 1592 * as well. Otherwise, the metadata checksum defaults 1593 * to fletcher4. 1594 */ 1595 if (zio_checksum_table[checksum].ci_correctable < 1 || 1596 zio_checksum_table[checksum].ci_eck) 1597 checksum = ZIO_CHECKSUM_FLETCHER_4; 1598 } else if (wp & WP_NOFILL) { 1599 ASSERT(level == 0); 1600 1601 /* 1602 * If we're writing preallocated blocks, we aren't actually 1603 * writing them so don't set any policy properties. These 1604 * blocks are currently only used by an external subsystem 1605 * outside of zfs (i.e. dump) and not written by the zio 1606 * pipeline. 1607 */ 1608 compress = ZIO_COMPRESS_OFF; 1609 checksum = ZIO_CHECKSUM_OFF; 1610 } else { 1611 compress = zio_compress_select(dn->dn_compress, compress); 1612 1613 checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ? 1614 zio_checksum_select(dn->dn_checksum, checksum) : 1615 dedup_checksum; 1616 1617 /* 1618 * Determine dedup setting. If we are in dmu_sync(), 1619 * we won't actually dedup now because that's all 1620 * done in syncing context; but we do want to use the 1621 * dedup checkum. If the checksum is not strong 1622 * enough to ensure unique signatures, force 1623 * dedup_verify. 1624 */ 1625 if (dedup_checksum != ZIO_CHECKSUM_OFF) { 1626 dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE; 1627 if (!zio_checksum_table[checksum].ci_dedup) 1628 dedup_verify = B_TRUE; 1629 } 1630 1631 /* 1632 * Enable nopwrite if we have a cryptographically secure 1633 * checksum that has no known collisions (i.e. SHA-256) 1634 * and compression is enabled. We don't enable nopwrite if 1635 * dedup is enabled as the two features are mutually exclusive. 1636 */ 1637 nopwrite = (!dedup && zio_checksum_table[checksum].ci_dedup && 1638 compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled); 1639 } 1640 1641 zp->zp_checksum = checksum; 1642 zp->zp_compress = compress; 1643 zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type; 1644 zp->zp_level = level; 1645 zp->zp_copies = MIN(copies + ismd, spa_max_replication(os->os_spa)); 1646 zp->zp_dedup = dedup; 1647 zp->zp_dedup_verify = dedup && dedup_verify; 1648 zp->zp_nopwrite = nopwrite; 1649 } 1650 1651 int 1652 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off) 1653 { 1654 dnode_t *dn; 1655 int i, err; 1656 1657 err = dnode_hold(os, object, FTAG, &dn); 1658 if (err) 1659 return (err); 1660 /* 1661 * Sync any current changes before 1662 * we go trundling through the block pointers. 1663 */ 1664 for (i = 0; i < TXG_SIZE; i++) { 1665 if (list_link_active(&dn->dn_dirty_link[i])) 1666 break; 1667 } 1668 if (i != TXG_SIZE) { 1669 dnode_rele(dn, FTAG); 1670 txg_wait_synced(dmu_objset_pool(os), 0); 1671 err = dnode_hold(os, object, FTAG, &dn); 1672 if (err) 1673 return (err); 1674 } 1675 1676 err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0); 1677 dnode_rele(dn, FTAG); 1678 1679 return (err); 1680 } 1681 1682 void 1683 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi) 1684 { 1685 dnode_phys_t *dnp; 1686 1687 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1688 mutex_enter(&dn->dn_mtx); 1689 1690 dnp = dn->dn_phys; 1691 1692 doi->doi_data_block_size = dn->dn_datablksz; 1693 doi->doi_metadata_block_size = dn->dn_indblkshift ? 1694 1ULL << dn->dn_indblkshift : 0; 1695 doi->doi_type = dn->dn_type; 1696 doi->doi_bonus_type = dn->dn_bonustype; 1697 doi->doi_bonus_size = dn->dn_bonuslen; 1698 doi->doi_indirection = dn->dn_nlevels; 1699 doi->doi_checksum = dn->dn_checksum; 1700 doi->doi_compress = dn->dn_compress; 1701 doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9; 1702 doi->doi_max_offset = (dnp->dn_maxblkid + 1) * dn->dn_datablksz; 1703 doi->doi_fill_count = 0; 1704 for (int i = 0; i < dnp->dn_nblkptr; i++) 1705 doi->doi_fill_count += dnp->dn_blkptr[i].blk_fill; 1706 1707 mutex_exit(&dn->dn_mtx); 1708 rw_exit(&dn->dn_struct_rwlock); 1709 } 1710 1711 /* 1712 * Get information on a DMU object. 1713 * If doi is NULL, just indicates whether the object exists. 1714 */ 1715 int 1716 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi) 1717 { 1718 dnode_t *dn; 1719 int err = dnode_hold(os, object, FTAG, &dn); 1720 1721 if (err) 1722 return (err); 1723 1724 if (doi != NULL) 1725 dmu_object_info_from_dnode(dn, doi); 1726 1727 dnode_rele(dn, FTAG); 1728 return (0); 1729 } 1730 1731 /* 1732 * As above, but faster; can be used when you have a held dbuf in hand. 1733 */ 1734 void 1735 dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi) 1736 { 1737 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1738 1739 DB_DNODE_ENTER(db); 1740 dmu_object_info_from_dnode(DB_DNODE(db), doi); 1741 DB_DNODE_EXIT(db); 1742 } 1743 1744 /* 1745 * Faster still when you only care about the size. 1746 * This is specifically optimized for zfs_getattr(). 1747 */ 1748 void 1749 dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize, 1750 u_longlong_t *nblk512) 1751 { 1752 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1753 dnode_t *dn; 1754 1755 DB_DNODE_ENTER(db); 1756 dn = DB_DNODE(db); 1757 1758 *blksize = dn->dn_datablksz; 1759 /* add 1 for dnode space */ 1760 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >> 1761 SPA_MINBLOCKSHIFT) + 1; 1762 DB_DNODE_EXIT(db); 1763 } 1764 1765 void 1766 byteswap_uint64_array(void *vbuf, size_t size) 1767 { 1768 uint64_t *buf = vbuf; 1769 size_t count = size >> 3; 1770 int i; 1771 1772 ASSERT((size & 7) == 0); 1773 1774 for (i = 0; i < count; i++) 1775 buf[i] = BSWAP_64(buf[i]); 1776 } 1777 1778 void 1779 byteswap_uint32_array(void *vbuf, size_t size) 1780 { 1781 uint32_t *buf = vbuf; 1782 size_t count = size >> 2; 1783 int i; 1784 1785 ASSERT((size & 3) == 0); 1786 1787 for (i = 0; i < count; i++) 1788 buf[i] = BSWAP_32(buf[i]); 1789 } 1790 1791 void 1792 byteswap_uint16_array(void *vbuf, size_t size) 1793 { 1794 uint16_t *buf = vbuf; 1795 size_t count = size >> 1; 1796 int i; 1797 1798 ASSERT((size & 1) == 0); 1799 1800 for (i = 0; i < count; i++) 1801 buf[i] = BSWAP_16(buf[i]); 1802 } 1803 1804 /* ARGSUSED */ 1805 void 1806 byteswap_uint8_array(void *vbuf, size_t size) 1807 { 1808 } 1809 1810 void 1811 dmu_init(void) 1812 { 1813 zfs_dbgmsg_init(); 1814 sa_cache_init(); 1815 xuio_stat_init(); 1816 dmu_objset_init(); 1817 dnode_init(); 1818 dbuf_init(); 1819 zfetch_init(); 1820 l2arc_init(); 1821 arc_init(); 1822 } 1823 1824 void 1825 dmu_fini(void) 1826 { 1827 arc_fini(); 1828 l2arc_fini(); 1829 zfetch_fini(); 1830 dbuf_fini(); 1831 dnode_fini(); 1832 dmu_objset_fini(); 1833 xuio_stat_fini(); 1834 sa_cache_fini(); 1835 zfs_dbgmsg_fini(); 1836 } 1837