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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 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 #ifdef _KERNEL 44 #include <sys/vmsystm.h> 45 #include <sys/zfs_znode.h> 46 #endif 47 48 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = { 49 { byteswap_uint8_array, TRUE, "unallocated" }, 50 { zap_byteswap, TRUE, "object directory" }, 51 { byteswap_uint64_array, TRUE, "object array" }, 52 { byteswap_uint8_array, TRUE, "packed nvlist" }, 53 { byteswap_uint64_array, TRUE, "packed nvlist size" }, 54 { byteswap_uint64_array, TRUE, "bplist" }, 55 { byteswap_uint64_array, TRUE, "bplist header" }, 56 { byteswap_uint64_array, TRUE, "SPA space map header" }, 57 { byteswap_uint64_array, TRUE, "SPA space map" }, 58 { byteswap_uint64_array, TRUE, "ZIL intent log" }, 59 { dnode_buf_byteswap, TRUE, "DMU dnode" }, 60 { dmu_objset_byteswap, TRUE, "DMU objset" }, 61 { byteswap_uint64_array, TRUE, "DSL directory" }, 62 { zap_byteswap, TRUE, "DSL directory child map"}, 63 { zap_byteswap, TRUE, "DSL dataset snap map" }, 64 { zap_byteswap, TRUE, "DSL props" }, 65 { byteswap_uint64_array, TRUE, "DSL dataset" }, 66 { zfs_znode_byteswap, TRUE, "ZFS znode" }, 67 { zfs_oldacl_byteswap, TRUE, "ZFS V0 ACL" }, 68 { byteswap_uint8_array, FALSE, "ZFS plain file" }, 69 { zap_byteswap, TRUE, "ZFS directory" }, 70 { zap_byteswap, TRUE, "ZFS master node" }, 71 { zap_byteswap, TRUE, "ZFS delete queue" }, 72 { byteswap_uint8_array, FALSE, "zvol object" }, 73 { zap_byteswap, TRUE, "zvol prop" }, 74 { byteswap_uint8_array, FALSE, "other uint8[]" }, 75 { byteswap_uint64_array, FALSE, "other uint64[]" }, 76 { zap_byteswap, TRUE, "other ZAP" }, 77 { zap_byteswap, TRUE, "persistent error log" }, 78 { byteswap_uint8_array, TRUE, "SPA history" }, 79 { byteswap_uint64_array, TRUE, "SPA history offsets" }, 80 { zap_byteswap, TRUE, "Pool properties" }, 81 { zap_byteswap, TRUE, "DSL permissions" }, 82 { zfs_acl_byteswap, TRUE, "ZFS ACL" }, 83 { byteswap_uint8_array, TRUE, "ZFS SYSACL" }, 84 { byteswap_uint8_array, TRUE, "FUID table" }, 85 { byteswap_uint64_array, TRUE, "FUID table size" }, 86 { zap_byteswap, TRUE, "DSL dataset next clones"}, 87 { zap_byteswap, TRUE, "scrub work queue" }, 88 { zap_byteswap, TRUE, "ZFS user/group used" }, 89 { zap_byteswap, TRUE, "ZFS user/group quota" }, 90 }; 91 92 int 93 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset, 94 void *tag, dmu_buf_t **dbp) 95 { 96 dnode_t *dn; 97 uint64_t blkid; 98 dmu_buf_impl_t *db; 99 int err; 100 101 err = dnode_hold(os->os, object, FTAG, &dn); 102 if (err) 103 return (err); 104 blkid = dbuf_whichblock(dn, offset); 105 rw_enter(&dn->dn_struct_rwlock, RW_READER); 106 db = dbuf_hold(dn, blkid, tag); 107 rw_exit(&dn->dn_struct_rwlock); 108 if (db == NULL) { 109 err = EIO; 110 } else { 111 err = dbuf_read(db, NULL, DB_RF_CANFAIL); 112 if (err) { 113 dbuf_rele(db, tag); 114 db = NULL; 115 } 116 } 117 118 dnode_rele(dn, FTAG); 119 *dbp = &db->db; 120 return (err); 121 } 122 123 int 124 dmu_bonus_max(void) 125 { 126 return (DN_MAX_BONUSLEN); 127 } 128 129 int 130 dmu_set_bonus(dmu_buf_t *db, int newsize, dmu_tx_t *tx) 131 { 132 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode; 133 134 if (dn->dn_bonus != (dmu_buf_impl_t *)db) 135 return (EINVAL); 136 if (newsize < 0 || newsize > db->db_size) 137 return (EINVAL); 138 dnode_setbonuslen(dn, newsize, tx); 139 return (0); 140 } 141 142 /* 143 * returns ENOENT, EIO, or 0. 144 */ 145 int 146 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp) 147 { 148 dnode_t *dn; 149 dmu_buf_impl_t *db; 150 int error; 151 152 error = dnode_hold(os->os, object, FTAG, &dn); 153 if (error) 154 return (error); 155 156 rw_enter(&dn->dn_struct_rwlock, RW_READER); 157 if (dn->dn_bonus == NULL) { 158 rw_exit(&dn->dn_struct_rwlock); 159 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 160 if (dn->dn_bonus == NULL) 161 dbuf_create_bonus(dn); 162 } 163 db = dn->dn_bonus; 164 rw_exit(&dn->dn_struct_rwlock); 165 166 /* as long as the bonus buf is held, the dnode will be held */ 167 if (refcount_add(&db->db_holds, tag) == 1) 168 VERIFY(dnode_add_ref(dn, db)); 169 170 dnode_rele(dn, FTAG); 171 172 VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED)); 173 174 *dbp = &db->db; 175 return (0); 176 } 177 178 /* 179 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces 180 * to take a held dnode rather than <os, object> -- the lookup is wasteful, 181 * and can induce severe lock contention when writing to several files 182 * whose dnodes are in the same block. 183 */ 184 static int 185 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length, 186 int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags) 187 { 188 dsl_pool_t *dp = NULL; 189 dmu_buf_t **dbp; 190 uint64_t blkid, nblks, i; 191 uint32_t dbuf_flags; 192 int err; 193 zio_t *zio; 194 hrtime_t start; 195 196 ASSERT(length <= DMU_MAX_ACCESS); 197 198 dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT; 199 if (flags & DMU_READ_NO_PREFETCH || length > zfetch_array_rd_sz) 200 dbuf_flags |= DB_RF_NOPREFETCH; 201 202 rw_enter(&dn->dn_struct_rwlock, RW_READER); 203 if (dn->dn_datablkshift) { 204 int blkshift = dn->dn_datablkshift; 205 nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) - 206 P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift; 207 } else { 208 if (offset + length > dn->dn_datablksz) { 209 zfs_panic_recover("zfs: accessing past end of object " 210 "%llx/%llx (size=%u access=%llu+%llu)", 211 (longlong_t)dn->dn_objset-> 212 os_dsl_dataset->ds_object, 213 (longlong_t)dn->dn_object, dn->dn_datablksz, 214 (longlong_t)offset, (longlong_t)length); 215 rw_exit(&dn->dn_struct_rwlock); 216 return (EIO); 217 } 218 nblks = 1; 219 } 220 dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP); 221 222 if (dn->dn_objset->os_dsl_dataset) 223 dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool; 224 if (dp && dsl_pool_sync_context(dp)) 225 start = gethrtime(); 226 zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL); 227 blkid = dbuf_whichblock(dn, offset); 228 for (i = 0; i < nblks; i++) { 229 dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag); 230 if (db == NULL) { 231 rw_exit(&dn->dn_struct_rwlock); 232 dmu_buf_rele_array(dbp, nblks, tag); 233 zio_nowait(zio); 234 return (EIO); 235 } 236 /* initiate async i/o */ 237 if (read) { 238 (void) dbuf_read(db, zio, dbuf_flags); 239 } 240 dbp[i] = &db->db; 241 } 242 rw_exit(&dn->dn_struct_rwlock); 243 244 /* wait for async i/o */ 245 err = zio_wait(zio); 246 /* track read overhead when we are in sync context */ 247 if (dp && dsl_pool_sync_context(dp)) 248 dp->dp_read_overhead += gethrtime() - start; 249 if (err) { 250 dmu_buf_rele_array(dbp, nblks, tag); 251 return (err); 252 } 253 254 /* wait for other io to complete */ 255 if (read) { 256 for (i = 0; i < nblks; i++) { 257 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i]; 258 mutex_enter(&db->db_mtx); 259 while (db->db_state == DB_READ || 260 db->db_state == DB_FILL) 261 cv_wait(&db->db_changed, &db->db_mtx); 262 if (db->db_state == DB_UNCACHED) 263 err = EIO; 264 mutex_exit(&db->db_mtx); 265 if (err) { 266 dmu_buf_rele_array(dbp, nblks, tag); 267 return (err); 268 } 269 } 270 } 271 272 *numbufsp = nblks; 273 *dbpp = dbp; 274 return (0); 275 } 276 277 static int 278 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset, 279 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp) 280 { 281 dnode_t *dn; 282 int err; 283 284 err = dnode_hold(os->os, object, FTAG, &dn); 285 if (err) 286 return (err); 287 288 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 289 numbufsp, dbpp, DMU_READ_PREFETCH); 290 291 dnode_rele(dn, FTAG); 292 293 return (err); 294 } 295 296 int 297 dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset, 298 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp) 299 { 300 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode; 301 int err; 302 303 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 304 numbufsp, dbpp, DMU_READ_PREFETCH); 305 306 return (err); 307 } 308 309 void 310 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag) 311 { 312 int i; 313 dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake; 314 315 if (numbufs == 0) 316 return; 317 318 for (i = 0; i < numbufs; i++) { 319 if (dbp[i]) 320 dbuf_rele(dbp[i], tag); 321 } 322 323 kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs); 324 } 325 326 void 327 dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len) 328 { 329 dnode_t *dn; 330 uint64_t blkid; 331 int nblks, i, err; 332 333 if (zfs_prefetch_disable) 334 return; 335 336 if (len == 0) { /* they're interested in the bonus buffer */ 337 dn = os->os->os_meta_dnode; 338 339 if (object == 0 || object >= DN_MAX_OBJECT) 340 return; 341 342 rw_enter(&dn->dn_struct_rwlock, RW_READER); 343 blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t)); 344 dbuf_prefetch(dn, blkid); 345 rw_exit(&dn->dn_struct_rwlock); 346 return; 347 } 348 349 /* 350 * XXX - Note, if the dnode for the requested object is not 351 * already cached, we will do a *synchronous* read in the 352 * dnode_hold() call. The same is true for any indirects. 353 */ 354 err = dnode_hold(os->os, object, FTAG, &dn); 355 if (err != 0) 356 return; 357 358 rw_enter(&dn->dn_struct_rwlock, RW_READER); 359 if (dn->dn_datablkshift) { 360 int blkshift = dn->dn_datablkshift; 361 nblks = (P2ROUNDUP(offset+len, 1<<blkshift) - 362 P2ALIGN(offset, 1<<blkshift)) >> blkshift; 363 } else { 364 nblks = (offset < dn->dn_datablksz); 365 } 366 367 if (nblks != 0) { 368 blkid = dbuf_whichblock(dn, offset); 369 for (i = 0; i < nblks; i++) 370 dbuf_prefetch(dn, blkid+i); 371 } 372 373 rw_exit(&dn->dn_struct_rwlock); 374 375 dnode_rele(dn, FTAG); 376 } 377 378 /* 379 * Get the next "chunk" of file data to free. We traverse the file from 380 * the end so that the file gets shorter over time (if we crashes in the 381 * middle, this will leave us in a better state). We find allocated file 382 * data by simply searching the allocated level 1 indirects. 383 */ 384 static int 385 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t limit) 386 { 387 uint64_t len = *start - limit; 388 uint64_t blkcnt = 0; 389 uint64_t maxblks = DMU_MAX_ACCESS / (1ULL << (dn->dn_indblkshift + 1)); 390 uint64_t iblkrange = 391 dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT); 392 393 ASSERT(limit <= *start); 394 395 if (len <= iblkrange * maxblks) { 396 *start = limit; 397 return (0); 398 } 399 ASSERT(ISP2(iblkrange)); 400 401 while (*start > limit && blkcnt < maxblks) { 402 int err; 403 404 /* find next allocated L1 indirect */ 405 err = dnode_next_offset(dn, 406 DNODE_FIND_BACKWARDS, start, 2, 1, 0); 407 408 /* if there are no more, then we are done */ 409 if (err == ESRCH) { 410 *start = limit; 411 return (0); 412 } else if (err) { 413 return (err); 414 } 415 blkcnt += 1; 416 417 /* reset offset to end of "next" block back */ 418 *start = P2ALIGN(*start, iblkrange); 419 if (*start <= limit) 420 *start = limit; 421 else 422 *start -= 1; 423 } 424 return (0); 425 } 426 427 static int 428 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset, 429 uint64_t length, boolean_t free_dnode) 430 { 431 dmu_tx_t *tx; 432 uint64_t object_size, start, end, len; 433 boolean_t trunc = (length == DMU_OBJECT_END); 434 int align, err; 435 436 align = 1 << dn->dn_datablkshift; 437 ASSERT(align > 0); 438 object_size = align == 1 ? dn->dn_datablksz : 439 (dn->dn_maxblkid + 1) << dn->dn_datablkshift; 440 441 end = offset + length; 442 if (trunc || end > object_size) 443 end = object_size; 444 if (end <= offset) 445 return (0); 446 length = end - offset; 447 448 while (length) { 449 start = end; 450 /* assert(offset <= start) */ 451 err = get_next_chunk(dn, &start, offset); 452 if (err) 453 return (err); 454 len = trunc ? DMU_OBJECT_END : end - start; 455 456 tx = dmu_tx_create(os); 457 dmu_tx_hold_free(tx, dn->dn_object, start, len); 458 err = dmu_tx_assign(tx, TXG_WAIT); 459 if (err) { 460 dmu_tx_abort(tx); 461 return (err); 462 } 463 464 dnode_free_range(dn, start, trunc ? -1 : len, tx); 465 466 if (start == 0 && free_dnode) { 467 ASSERT(trunc); 468 dnode_free(dn, tx); 469 } 470 471 length -= end - start; 472 473 dmu_tx_commit(tx); 474 end = start; 475 } 476 return (0); 477 } 478 479 int 480 dmu_free_long_range(objset_t *os, uint64_t object, 481 uint64_t offset, uint64_t length) 482 { 483 dnode_t *dn; 484 int err; 485 486 err = dnode_hold(os->os, object, FTAG, &dn); 487 if (err != 0) 488 return (err); 489 err = dmu_free_long_range_impl(os, dn, offset, length, FALSE); 490 dnode_rele(dn, FTAG); 491 return (err); 492 } 493 494 int 495 dmu_free_object(objset_t *os, uint64_t object) 496 { 497 dnode_t *dn; 498 dmu_tx_t *tx; 499 int err; 500 501 err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED, 502 FTAG, &dn); 503 if (err != 0) 504 return (err); 505 if (dn->dn_nlevels == 1) { 506 tx = dmu_tx_create(os); 507 dmu_tx_hold_bonus(tx, object); 508 dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END); 509 err = dmu_tx_assign(tx, TXG_WAIT); 510 if (err == 0) { 511 dnode_free_range(dn, 0, DMU_OBJECT_END, tx); 512 dnode_free(dn, tx); 513 dmu_tx_commit(tx); 514 } else { 515 dmu_tx_abort(tx); 516 } 517 } else { 518 err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE); 519 } 520 dnode_rele(dn, FTAG); 521 return (err); 522 } 523 524 int 525 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset, 526 uint64_t size, dmu_tx_t *tx) 527 { 528 dnode_t *dn; 529 int err = dnode_hold(os->os, object, FTAG, &dn); 530 if (err) 531 return (err); 532 ASSERT(offset < UINT64_MAX); 533 ASSERT(size == -1ULL || size <= UINT64_MAX - offset); 534 dnode_free_range(dn, offset, size, tx); 535 dnode_rele(dn, FTAG); 536 return (0); 537 } 538 539 int 540 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 541 void *buf, uint32_t flags) 542 { 543 dnode_t *dn; 544 dmu_buf_t **dbp; 545 int numbufs, err; 546 547 err = dnode_hold(os->os, object, FTAG, &dn); 548 if (err) 549 return (err); 550 551 /* 552 * Deal with odd block sizes, where there can't be data past the first 553 * block. If we ever do the tail block optimization, we will need to 554 * handle that here as well. 555 */ 556 if (dn->dn_maxblkid == 0) { 557 int newsz = offset > dn->dn_datablksz ? 0 : 558 MIN(size, dn->dn_datablksz - offset); 559 bzero((char *)buf + newsz, size - newsz); 560 size = newsz; 561 } 562 563 while (size > 0) { 564 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2); 565 int i; 566 567 /* 568 * NB: we could do this block-at-a-time, but it's nice 569 * to be reading in parallel. 570 */ 571 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen, 572 TRUE, FTAG, &numbufs, &dbp, flags); 573 if (err) 574 break; 575 576 for (i = 0; i < numbufs; i++) { 577 int tocpy; 578 int bufoff; 579 dmu_buf_t *db = dbp[i]; 580 581 ASSERT(size > 0); 582 583 bufoff = offset - db->db_offset; 584 tocpy = (int)MIN(db->db_size - bufoff, size); 585 586 bcopy((char *)db->db_data + bufoff, buf, tocpy); 587 588 offset += tocpy; 589 size -= tocpy; 590 buf = (char *)buf + tocpy; 591 } 592 dmu_buf_rele_array(dbp, numbufs, FTAG); 593 } 594 dnode_rele(dn, FTAG); 595 return (err); 596 } 597 598 void 599 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 600 const void *buf, dmu_tx_t *tx) 601 { 602 dmu_buf_t **dbp; 603 int numbufs, i; 604 605 if (size == 0) 606 return; 607 608 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size, 609 FALSE, FTAG, &numbufs, &dbp)); 610 611 for (i = 0; i < numbufs; i++) { 612 int tocpy; 613 int bufoff; 614 dmu_buf_t *db = dbp[i]; 615 616 ASSERT(size > 0); 617 618 bufoff = offset - db->db_offset; 619 tocpy = (int)MIN(db->db_size - bufoff, size); 620 621 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 622 623 if (tocpy == db->db_size) 624 dmu_buf_will_fill(db, tx); 625 else 626 dmu_buf_will_dirty(db, tx); 627 628 bcopy(buf, (char *)db->db_data + bufoff, tocpy); 629 630 if (tocpy == db->db_size) 631 dmu_buf_fill_done(db, tx); 632 633 offset += tocpy; 634 size -= tocpy; 635 buf = (char *)buf + tocpy; 636 } 637 dmu_buf_rele_array(dbp, numbufs, FTAG); 638 } 639 640 void 641 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 642 dmu_tx_t *tx) 643 { 644 dmu_buf_t **dbp; 645 int numbufs, i; 646 647 if (size == 0) 648 return; 649 650 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size, 651 FALSE, FTAG, &numbufs, &dbp)); 652 653 for (i = 0; i < numbufs; i++) { 654 dmu_buf_t *db = dbp[i]; 655 656 dmu_buf_will_not_fill(db, tx); 657 } 658 dmu_buf_rele_array(dbp, numbufs, FTAG); 659 } 660 661 #ifdef _KERNEL 662 int 663 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size) 664 { 665 dmu_buf_t **dbp; 666 int numbufs, i, err; 667 668 /* 669 * NB: we could do this block-at-a-time, but it's nice 670 * to be reading in parallel. 671 */ 672 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG, 673 &numbufs, &dbp); 674 if (err) 675 return (err); 676 677 for (i = 0; i < numbufs; i++) { 678 int tocpy; 679 int bufoff; 680 dmu_buf_t *db = dbp[i]; 681 682 ASSERT(size > 0); 683 684 bufoff = uio->uio_loffset - db->db_offset; 685 tocpy = (int)MIN(db->db_size - bufoff, size); 686 687 err = uiomove((char *)db->db_data + bufoff, tocpy, 688 UIO_READ, uio); 689 if (err) 690 break; 691 692 size -= tocpy; 693 } 694 dmu_buf_rele_array(dbp, numbufs, FTAG); 695 696 return (err); 697 } 698 699 int 700 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size, 701 dmu_tx_t *tx) 702 { 703 dmu_buf_t **dbp; 704 int numbufs, i; 705 int err = 0; 706 707 if (size == 0) 708 return (0); 709 710 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, 711 FALSE, FTAG, &numbufs, &dbp); 712 if (err) 713 return (err); 714 715 for (i = 0; i < numbufs; i++) { 716 int tocpy; 717 int bufoff; 718 dmu_buf_t *db = dbp[i]; 719 720 ASSERT(size > 0); 721 722 bufoff = uio->uio_loffset - db->db_offset; 723 tocpy = (int)MIN(db->db_size - bufoff, size); 724 725 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 726 727 if (tocpy == db->db_size) 728 dmu_buf_will_fill(db, tx); 729 else 730 dmu_buf_will_dirty(db, tx); 731 732 /* 733 * XXX uiomove could block forever (eg. nfs-backed 734 * pages). There needs to be a uiolockdown() function 735 * to lock the pages in memory, so that uiomove won't 736 * block. 737 */ 738 err = uiomove((char *)db->db_data + bufoff, tocpy, 739 UIO_WRITE, uio); 740 741 if (tocpy == db->db_size) 742 dmu_buf_fill_done(db, tx); 743 744 if (err) 745 break; 746 747 size -= tocpy; 748 } 749 dmu_buf_rele_array(dbp, numbufs, FTAG); 750 return (err); 751 } 752 753 int 754 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 755 page_t *pp, dmu_tx_t *tx) 756 { 757 dmu_buf_t **dbp; 758 int numbufs, i; 759 int err; 760 761 if (size == 0) 762 return (0); 763 764 err = dmu_buf_hold_array(os, object, offset, size, 765 FALSE, FTAG, &numbufs, &dbp); 766 if (err) 767 return (err); 768 769 for (i = 0; i < numbufs; i++) { 770 int tocpy, copied, thiscpy; 771 int bufoff; 772 dmu_buf_t *db = dbp[i]; 773 caddr_t va; 774 775 ASSERT(size > 0); 776 ASSERT3U(db->db_size, >=, PAGESIZE); 777 778 bufoff = offset - db->db_offset; 779 tocpy = (int)MIN(db->db_size - bufoff, size); 780 781 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 782 783 if (tocpy == db->db_size) 784 dmu_buf_will_fill(db, tx); 785 else 786 dmu_buf_will_dirty(db, tx); 787 788 for (copied = 0; copied < tocpy; copied += PAGESIZE) { 789 ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff); 790 thiscpy = MIN(PAGESIZE, tocpy - copied); 791 va = zfs_map_page(pp, S_READ); 792 bcopy(va, (char *)db->db_data + bufoff, thiscpy); 793 zfs_unmap_page(pp, va); 794 pp = pp->p_next; 795 bufoff += PAGESIZE; 796 } 797 798 if (tocpy == db->db_size) 799 dmu_buf_fill_done(db, tx); 800 801 offset += tocpy; 802 size -= tocpy; 803 } 804 dmu_buf_rele_array(dbp, numbufs, FTAG); 805 return (err); 806 } 807 #endif 808 809 /* 810 * Allocate a loaned anonymous arc buffer. 811 */ 812 arc_buf_t * 813 dmu_request_arcbuf(dmu_buf_t *handle, int size) 814 { 815 dnode_t *dn = ((dmu_buf_impl_t *)handle)->db_dnode; 816 817 return (arc_loan_buf(dn->dn_objset->os_spa, size)); 818 } 819 820 /* 821 * Free a loaned arc buffer. 822 */ 823 void 824 dmu_return_arcbuf(arc_buf_t *buf) 825 { 826 arc_return_buf(buf, FTAG); 827 VERIFY(arc_buf_remove_ref(buf, FTAG) == 1); 828 } 829 830 /* 831 * When possible directly assign passed loaned arc buffer to a dbuf. 832 * If this is not possible copy the contents of passed arc buf via 833 * dmu_write(). 834 */ 835 void 836 dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf, 837 dmu_tx_t *tx) 838 { 839 dnode_t *dn = ((dmu_buf_impl_t *)handle)->db_dnode; 840 dmu_buf_impl_t *db; 841 uint32_t blksz = (uint32_t)arc_buf_size(buf); 842 uint64_t blkid; 843 844 rw_enter(&dn->dn_struct_rwlock, RW_READER); 845 blkid = dbuf_whichblock(dn, offset); 846 VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL); 847 rw_exit(&dn->dn_struct_rwlock); 848 849 if (offset == db->db.db_offset && blksz == db->db.db_size) { 850 dbuf_assign_arcbuf(db, buf, tx); 851 dbuf_rele(db, FTAG); 852 } else { 853 dbuf_rele(db, FTAG); 854 ASSERT(dn->dn_objset->os.os == dn->dn_objset); 855 dmu_write(&dn->dn_objset->os, dn->dn_object, offset, blksz, 856 buf->b_data, tx); 857 dmu_return_arcbuf(buf); 858 } 859 } 860 861 typedef struct { 862 dbuf_dirty_record_t *dr; 863 dmu_sync_cb_t *done; 864 void *arg; 865 } dmu_sync_arg_t; 866 867 /* ARGSUSED */ 868 static void 869 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg) 870 { 871 blkptr_t *bp = zio->io_bp; 872 873 if (!BP_IS_HOLE(bp)) { 874 dmu_sync_arg_t *in = varg; 875 dbuf_dirty_record_t *dr = in->dr; 876 dmu_buf_impl_t *db = dr->dr_dbuf; 877 ASSERT(BP_GET_TYPE(bp) == db->db_dnode->dn_type); 878 ASSERT(BP_GET_LEVEL(bp) == 0); 879 bp->blk_fill = 1; 880 } 881 } 882 883 /* ARGSUSED */ 884 static void 885 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg) 886 { 887 dmu_sync_arg_t *in = varg; 888 dbuf_dirty_record_t *dr = in->dr; 889 dmu_buf_impl_t *db = dr->dr_dbuf; 890 dmu_sync_cb_t *done = in->done; 891 892 mutex_enter(&db->db_mtx); 893 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC); 894 dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */ 895 dr->dt.dl.dr_override_state = DR_OVERRIDDEN; 896 cv_broadcast(&db->db_changed); 897 mutex_exit(&db->db_mtx); 898 899 if (done) 900 done(&(db->db), in->arg); 901 902 kmem_free(in, sizeof (dmu_sync_arg_t)); 903 } 904 905 /* 906 * Intent log support: sync the block associated with db to disk. 907 * N.B. and XXX: the caller is responsible for making sure that the 908 * data isn't changing while dmu_sync() is writing it. 909 * 910 * Return values: 911 * 912 * EEXIST: this txg has already been synced, so there's nothing to to. 913 * The caller should not log the write. 914 * 915 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do. 916 * The caller should not log the write. 917 * 918 * EALREADY: this block is already in the process of being synced. 919 * The caller should track its progress (somehow). 920 * 921 * EINPROGRESS: the IO has been initiated. 922 * The caller should log this blkptr in the callback. 923 * 924 * 0: completed. Sets *bp to the blkptr just written. 925 * The caller should log this blkptr immediately. 926 */ 927 int 928 dmu_sync(zio_t *pio, dmu_buf_t *db_fake, 929 blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg) 930 { 931 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 932 objset_impl_t *os = db->db_objset; 933 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; 934 tx_state_t *tx = &dp->dp_tx; 935 dbuf_dirty_record_t *dr; 936 dmu_sync_arg_t *in; 937 zbookmark_t zb; 938 writeprops_t wp = { 0 }; 939 zio_t *zio; 940 int err; 941 942 ASSERT(BP_IS_HOLE(bp)); 943 ASSERT(txg != 0); 944 945 dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n", 946 txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg); 947 948 /* 949 * XXX - would be nice if we could do this without suspending... 950 */ 951 txg_suspend(dp); 952 953 /* 954 * If this txg already synced, there's nothing to do. 955 */ 956 if (txg <= tx->tx_synced_txg) { 957 txg_resume(dp); 958 /* 959 * If we're running ziltest, we need the blkptr regardless. 960 */ 961 if (txg > spa_freeze_txg(dp->dp_spa)) { 962 /* if db_blkptr == NULL, this was an empty write */ 963 if (db->db_blkptr) 964 *bp = *db->db_blkptr; /* structure assignment */ 965 return (0); 966 } 967 return (EEXIST); 968 } 969 970 mutex_enter(&db->db_mtx); 971 972 if (txg == tx->tx_syncing_txg) { 973 while (db->db_data_pending) { 974 /* 975 * IO is in-progress. Wait for it to finish. 976 * XXX - would be nice to be able to somehow "attach" 977 * this zio to the parent zio passed in. 978 */ 979 cv_wait(&db->db_changed, &db->db_mtx); 980 if (!db->db_data_pending && 981 db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) { 982 /* 983 * IO was compressed away 984 */ 985 *bp = *db->db_blkptr; /* structure assignment */ 986 mutex_exit(&db->db_mtx); 987 txg_resume(dp); 988 return (0); 989 } 990 ASSERT(db->db_data_pending || 991 (db->db_blkptr && db->db_blkptr->blk_birth == txg)); 992 } 993 994 if (db->db_blkptr && db->db_blkptr->blk_birth == txg) { 995 /* 996 * IO is already completed. 997 */ 998 *bp = *db->db_blkptr; /* structure assignment */ 999 mutex_exit(&db->db_mtx); 1000 txg_resume(dp); 1001 return (0); 1002 } 1003 } 1004 1005 dr = db->db_last_dirty; 1006 while (dr && dr->dr_txg > txg) 1007 dr = dr->dr_next; 1008 if (dr == NULL || dr->dr_txg < txg) { 1009 /* 1010 * This dbuf isn't dirty, must have been free_range'd. 1011 * There's no need to log writes to freed blocks, so we're done. 1012 */ 1013 mutex_exit(&db->db_mtx); 1014 txg_resume(dp); 1015 return (ENOENT); 1016 } 1017 1018 ASSERT(dr->dr_txg == txg); 1019 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) { 1020 /* 1021 * We have already issued a sync write for this buffer. 1022 */ 1023 mutex_exit(&db->db_mtx); 1024 txg_resume(dp); 1025 return (EALREADY); 1026 } else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 1027 /* 1028 * This buffer has already been synced. It could not 1029 * have been dirtied since, or we would have cleared the state. 1030 */ 1031 *bp = dr->dt.dl.dr_overridden_by; /* structure assignment */ 1032 mutex_exit(&db->db_mtx); 1033 txg_resume(dp); 1034 return (0); 1035 } 1036 1037 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC; 1038 in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP); 1039 in->dr = dr; 1040 in->done = done; 1041 in->arg = arg; 1042 mutex_exit(&db->db_mtx); 1043 txg_resume(dp); 1044 1045 zb.zb_objset = os->os_dsl_dataset->ds_object; 1046 zb.zb_object = db->db.db_object; 1047 zb.zb_level = db->db_level; 1048 zb.zb_blkid = db->db_blkid; 1049 1050 wp.wp_type = db->db_dnode->dn_type; 1051 wp.wp_level = db->db_level; 1052 wp.wp_copies = os->os_copies; 1053 wp.wp_dnchecksum = db->db_dnode->dn_checksum; 1054 wp.wp_oschecksum = os->os_checksum; 1055 wp.wp_dncompress = db->db_dnode->dn_compress; 1056 wp.wp_oscompress = os->os_compress; 1057 1058 ASSERT(BP_IS_HOLE(bp)); 1059 1060 zio = arc_write(pio, os->os_spa, &wp, DBUF_IS_L2CACHEABLE(db), 1061 txg, bp, dr->dt.dl.dr_data, dmu_sync_ready, dmu_sync_done, in, 1062 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1063 if (pio) { 1064 zio_nowait(zio); 1065 err = EINPROGRESS; 1066 } else { 1067 err = zio_wait(zio); 1068 ASSERT(err == 0); 1069 } 1070 return (err); 1071 } 1072 1073 int 1074 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs, 1075 dmu_tx_t *tx) 1076 { 1077 dnode_t *dn; 1078 int err; 1079 1080 err = dnode_hold(os->os, object, FTAG, &dn); 1081 if (err) 1082 return (err); 1083 err = dnode_set_blksz(dn, size, ibs, tx); 1084 dnode_rele(dn, FTAG); 1085 return (err); 1086 } 1087 1088 void 1089 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum, 1090 dmu_tx_t *tx) 1091 { 1092 dnode_t *dn; 1093 1094 /* XXX assumes dnode_hold will not get an i/o error */ 1095 (void) dnode_hold(os->os, object, FTAG, &dn); 1096 ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS); 1097 dn->dn_checksum = checksum; 1098 dnode_setdirty(dn, tx); 1099 dnode_rele(dn, FTAG); 1100 } 1101 1102 void 1103 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress, 1104 dmu_tx_t *tx) 1105 { 1106 dnode_t *dn; 1107 1108 /* XXX assumes dnode_hold will not get an i/o error */ 1109 (void) dnode_hold(os->os, object, FTAG, &dn); 1110 ASSERT(compress < ZIO_COMPRESS_FUNCTIONS); 1111 dn->dn_compress = compress; 1112 dnode_setdirty(dn, tx); 1113 dnode_rele(dn, FTAG); 1114 } 1115 1116 int 1117 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off) 1118 { 1119 dnode_t *dn; 1120 int i, err; 1121 1122 err = dnode_hold(os->os, object, FTAG, &dn); 1123 if (err) 1124 return (err); 1125 /* 1126 * Sync any current changes before 1127 * we go trundling through the block pointers. 1128 */ 1129 for (i = 0; i < TXG_SIZE; i++) { 1130 if (list_link_active(&dn->dn_dirty_link[i])) 1131 break; 1132 } 1133 if (i != TXG_SIZE) { 1134 dnode_rele(dn, FTAG); 1135 txg_wait_synced(dmu_objset_pool(os), 0); 1136 err = dnode_hold(os->os, object, FTAG, &dn); 1137 if (err) 1138 return (err); 1139 } 1140 1141 err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0); 1142 dnode_rele(dn, FTAG); 1143 1144 return (err); 1145 } 1146 1147 void 1148 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi) 1149 { 1150 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1151 mutex_enter(&dn->dn_mtx); 1152 1153 doi->doi_data_block_size = dn->dn_datablksz; 1154 doi->doi_metadata_block_size = dn->dn_indblkshift ? 1155 1ULL << dn->dn_indblkshift : 0; 1156 doi->doi_indirection = dn->dn_nlevels; 1157 doi->doi_checksum = dn->dn_checksum; 1158 doi->doi_compress = dn->dn_compress; 1159 doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) + 1160 SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT; 1161 doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid; 1162 doi->doi_type = dn->dn_type; 1163 doi->doi_bonus_size = dn->dn_bonuslen; 1164 doi->doi_bonus_type = dn->dn_bonustype; 1165 1166 mutex_exit(&dn->dn_mtx); 1167 rw_exit(&dn->dn_struct_rwlock); 1168 } 1169 1170 /* 1171 * Get information on a DMU object. 1172 * If doi is NULL, just indicates whether the object exists. 1173 */ 1174 int 1175 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi) 1176 { 1177 dnode_t *dn; 1178 int err = dnode_hold(os->os, object, FTAG, &dn); 1179 1180 if (err) 1181 return (err); 1182 1183 if (doi != NULL) 1184 dmu_object_info_from_dnode(dn, doi); 1185 1186 dnode_rele(dn, FTAG); 1187 return (0); 1188 } 1189 1190 /* 1191 * As above, but faster; can be used when you have a held dbuf in hand. 1192 */ 1193 void 1194 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi) 1195 { 1196 dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi); 1197 } 1198 1199 /* 1200 * Faster still when you only care about the size. 1201 * This is specifically optimized for zfs_getattr(). 1202 */ 1203 void 1204 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512) 1205 { 1206 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode; 1207 1208 *blksize = dn->dn_datablksz; 1209 /* add 1 for dnode space */ 1210 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >> 1211 SPA_MINBLOCKSHIFT) + 1; 1212 } 1213 1214 void 1215 byteswap_uint64_array(void *vbuf, size_t size) 1216 { 1217 uint64_t *buf = vbuf; 1218 size_t count = size >> 3; 1219 int i; 1220 1221 ASSERT((size & 7) == 0); 1222 1223 for (i = 0; i < count; i++) 1224 buf[i] = BSWAP_64(buf[i]); 1225 } 1226 1227 void 1228 byteswap_uint32_array(void *vbuf, size_t size) 1229 { 1230 uint32_t *buf = vbuf; 1231 size_t count = size >> 2; 1232 int i; 1233 1234 ASSERT((size & 3) == 0); 1235 1236 for (i = 0; i < count; i++) 1237 buf[i] = BSWAP_32(buf[i]); 1238 } 1239 1240 void 1241 byteswap_uint16_array(void *vbuf, size_t size) 1242 { 1243 uint16_t *buf = vbuf; 1244 size_t count = size >> 1; 1245 int i; 1246 1247 ASSERT((size & 1) == 0); 1248 1249 for (i = 0; i < count; i++) 1250 buf[i] = BSWAP_16(buf[i]); 1251 } 1252 1253 /* ARGSUSED */ 1254 void 1255 byteswap_uint8_array(void *vbuf, size_t size) 1256 { 1257 } 1258 1259 void 1260 dmu_init(void) 1261 { 1262 dbuf_init(); 1263 dnode_init(); 1264 arc_init(); 1265 l2arc_init(); 1266 } 1267 1268 void 1269 dmu_fini(void) 1270 { 1271 arc_fini(); 1272 dnode_fini(); 1273 dbuf_fini(); 1274 l2arc_fini(); 1275 } 1276