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