1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 /* Portions Copyright 2010 Robert Milkowski */ 26 27 #include <sys/cred.h> 28 #include <sys/zfs_context.h> 29 #include <sys/dmu_objset.h> 30 #include <sys/dsl_dir.h> 31 #include <sys/dsl_dataset.h> 32 #include <sys/dsl_prop.h> 33 #include <sys/dsl_pool.h> 34 #include <sys/dsl_synctask.h> 35 #include <sys/dsl_deleg.h> 36 #include <sys/dnode.h> 37 #include <sys/dbuf.h> 38 #include <sys/zvol.h> 39 #include <sys/dmu_tx.h> 40 #include <sys/zap.h> 41 #include <sys/zil.h> 42 #include <sys/dmu_impl.h> 43 #include <sys/zfs_ioctl.h> 44 #include <sys/sa.h> 45 46 /* 47 * Needed to close a window in dnode_move() that allows the objset to be freed 48 * before it can be safely accessed. 49 */ 50 krwlock_t os_lock; 51 52 void 53 dmu_objset_init(void) 54 { 55 rw_init(&os_lock, NULL, RW_DEFAULT, NULL); 56 } 57 58 void 59 dmu_objset_fini(void) 60 { 61 rw_destroy(&os_lock); 62 } 63 64 spa_t * 65 dmu_objset_spa(objset_t *os) 66 { 67 return (os->os_spa); 68 } 69 70 zilog_t * 71 dmu_objset_zil(objset_t *os) 72 { 73 return (os->os_zil); 74 } 75 76 dsl_pool_t * 77 dmu_objset_pool(objset_t *os) 78 { 79 dsl_dataset_t *ds; 80 81 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 82 return (ds->ds_dir->dd_pool); 83 else 84 return (spa_get_dsl(os->os_spa)); 85 } 86 87 dsl_dataset_t * 88 dmu_objset_ds(objset_t *os) 89 { 90 return (os->os_dsl_dataset); 91 } 92 93 dmu_objset_type_t 94 dmu_objset_type(objset_t *os) 95 { 96 return (os->os_phys->os_type); 97 } 98 99 void 100 dmu_objset_name(objset_t *os, char *buf) 101 { 102 dsl_dataset_name(os->os_dsl_dataset, buf); 103 } 104 105 uint64_t 106 dmu_objset_id(objset_t *os) 107 { 108 dsl_dataset_t *ds = os->os_dsl_dataset; 109 110 return (ds ? ds->ds_object : 0); 111 } 112 113 uint64_t 114 dmu_objset_syncprop(objset_t *os) 115 { 116 return (os->os_sync); 117 } 118 119 uint64_t 120 dmu_objset_logbias(objset_t *os) 121 { 122 return (os->os_logbias); 123 } 124 125 static void 126 checksum_changed_cb(void *arg, uint64_t newval) 127 { 128 objset_t *os = arg; 129 130 /* 131 * Inheritance should have been done by now. 132 */ 133 ASSERT(newval != ZIO_CHECKSUM_INHERIT); 134 135 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 136 } 137 138 static void 139 compression_changed_cb(void *arg, uint64_t newval) 140 { 141 objset_t *os = arg; 142 143 /* 144 * Inheritance and range checking should have been done by now. 145 */ 146 ASSERT(newval != ZIO_COMPRESS_INHERIT); 147 148 os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 149 } 150 151 static void 152 copies_changed_cb(void *arg, uint64_t newval) 153 { 154 objset_t *os = arg; 155 156 /* 157 * Inheritance and range checking should have been done by now. 158 */ 159 ASSERT(newval > 0); 160 ASSERT(newval <= spa_max_replication(os->os_spa)); 161 162 os->os_copies = newval; 163 } 164 165 static void 166 dedup_changed_cb(void *arg, uint64_t newval) 167 { 168 objset_t *os = arg; 169 spa_t *spa = os->os_spa; 170 enum zio_checksum checksum; 171 172 /* 173 * Inheritance should have been done by now. 174 */ 175 ASSERT(newval != ZIO_CHECKSUM_INHERIT); 176 177 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 178 179 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 180 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 181 } 182 183 static void 184 primary_cache_changed_cb(void *arg, uint64_t newval) 185 { 186 objset_t *os = arg; 187 188 /* 189 * Inheritance and range checking should have been done by now. 190 */ 191 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 192 newval == ZFS_CACHE_METADATA); 193 194 os->os_primary_cache = newval; 195 } 196 197 static void 198 secondary_cache_changed_cb(void *arg, uint64_t newval) 199 { 200 objset_t *os = arg; 201 202 /* 203 * Inheritance and range checking should have been done by now. 204 */ 205 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 206 newval == ZFS_CACHE_METADATA); 207 208 os->os_secondary_cache = newval; 209 } 210 211 static void 212 sync_changed_cb(void *arg, uint64_t newval) 213 { 214 objset_t *os = arg; 215 216 /* 217 * Inheritance and range checking should have been done by now. 218 */ 219 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 220 newval == ZFS_SYNC_DISABLED); 221 222 os->os_sync = newval; 223 if (os->os_zil) 224 zil_set_sync(os->os_zil, newval); 225 } 226 227 static void 228 logbias_changed_cb(void *arg, uint64_t newval) 229 { 230 objset_t *os = arg; 231 232 ASSERT(newval == ZFS_LOGBIAS_LATENCY || 233 newval == ZFS_LOGBIAS_THROUGHPUT); 234 os->os_logbias = newval; 235 if (os->os_zil) 236 zil_set_logbias(os->os_zil, newval); 237 } 238 239 void 240 dmu_objset_byteswap(void *buf, size_t size) 241 { 242 objset_phys_t *osp = buf; 243 244 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 245 dnode_byteswap(&osp->os_meta_dnode); 246 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 247 osp->os_type = BSWAP_64(osp->os_type); 248 osp->os_flags = BSWAP_64(osp->os_flags); 249 if (size == sizeof (objset_phys_t)) { 250 dnode_byteswap(&osp->os_userused_dnode); 251 dnode_byteswap(&osp->os_groupused_dnode); 252 } 253 } 254 255 int 256 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 257 objset_t **osp) 258 { 259 objset_t *os; 260 int i, err; 261 262 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 263 264 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 265 os->os_dsl_dataset = ds; 266 os->os_spa = spa; 267 os->os_rootbp = bp; 268 if (!BP_IS_HOLE(os->os_rootbp)) { 269 uint32_t aflags = ARC_WAIT; 270 zbookmark_t zb; 271 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 272 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 273 274 if (DMU_OS_IS_L2CACHEABLE(os)) 275 aflags |= ARC_L2CACHE; 276 277 dprintf_bp(os->os_rootbp, "reading %s", ""); 278 /* 279 * XXX when bprewrite scrub can change the bp, 280 * and this is called from dmu_objset_open_ds_os, the bp 281 * could change, and we'll need a lock. 282 */ 283 err = dsl_read_nolock(NULL, spa, os->os_rootbp, 284 arc_getbuf_func, &os->os_phys_buf, 285 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 286 if (err) { 287 kmem_free(os, sizeof (objset_t)); 288 /* convert checksum errors into IO errors */ 289 if (err == ECKSUM) 290 err = EIO; 291 return (err); 292 } 293 294 /* Increase the blocksize if we are permitted. */ 295 if (spa_version(spa) >= SPA_VERSION_USERSPACE && 296 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 297 arc_buf_t *buf = arc_buf_alloc(spa, 298 sizeof (objset_phys_t), &os->os_phys_buf, 299 ARC_BUFC_METADATA); 300 bzero(buf->b_data, sizeof (objset_phys_t)); 301 bcopy(os->os_phys_buf->b_data, buf->b_data, 302 arc_buf_size(os->os_phys_buf)); 303 (void) arc_buf_remove_ref(os->os_phys_buf, 304 &os->os_phys_buf); 305 os->os_phys_buf = buf; 306 } 307 308 os->os_phys = os->os_phys_buf->b_data; 309 os->os_flags = os->os_phys->os_flags; 310 } else { 311 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 312 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 313 os->os_phys_buf = arc_buf_alloc(spa, size, 314 &os->os_phys_buf, ARC_BUFC_METADATA); 315 os->os_phys = os->os_phys_buf->b_data; 316 bzero(os->os_phys, size); 317 } 318 319 /* 320 * Note: the changed_cb will be called once before the register 321 * func returns, thus changing the checksum/compression from the 322 * default (fletcher2/off). Snapshots don't need to know about 323 * checksum/compression/copies. 324 */ 325 if (ds) { 326 err = dsl_prop_register(ds, "primarycache", 327 primary_cache_changed_cb, os); 328 if (err == 0) 329 err = dsl_prop_register(ds, "secondarycache", 330 secondary_cache_changed_cb, os); 331 if (!dsl_dataset_is_snapshot(ds)) { 332 if (err == 0) 333 err = dsl_prop_register(ds, "checksum", 334 checksum_changed_cb, os); 335 if (err == 0) 336 err = dsl_prop_register(ds, "compression", 337 compression_changed_cb, os); 338 if (err == 0) 339 err = dsl_prop_register(ds, "copies", 340 copies_changed_cb, os); 341 if (err == 0) 342 err = dsl_prop_register(ds, "dedup", 343 dedup_changed_cb, os); 344 if (err == 0) 345 err = dsl_prop_register(ds, "logbias", 346 logbias_changed_cb, os); 347 if (err == 0) 348 err = dsl_prop_register(ds, "sync", 349 sync_changed_cb, os); 350 } 351 if (err) { 352 VERIFY(arc_buf_remove_ref(os->os_phys_buf, 353 &os->os_phys_buf) == 1); 354 kmem_free(os, sizeof (objset_t)); 355 return (err); 356 } 357 } else if (ds == NULL) { 358 /* It's the meta-objset. */ 359 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 360 os->os_compress = ZIO_COMPRESS_LZJB; 361 os->os_copies = spa_max_replication(spa); 362 os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 363 os->os_dedup_verify = 0; 364 os->os_logbias = 0; 365 os->os_sync = 0; 366 os->os_primary_cache = ZFS_CACHE_ALL; 367 os->os_secondary_cache = ZFS_CACHE_ALL; 368 } 369 370 os->os_zil_header = os->os_phys->os_zil_header; 371 os->os_zil = zil_alloc(os, &os->os_zil_header); 372 373 for (i = 0; i < TXG_SIZE; i++) { 374 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), 375 offsetof(dnode_t, dn_dirty_link[i])); 376 list_create(&os->os_free_dnodes[i], sizeof (dnode_t), 377 offsetof(dnode_t, dn_dirty_link[i])); 378 } 379 list_create(&os->os_dnodes, sizeof (dnode_t), 380 offsetof(dnode_t, dn_link)); 381 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 382 offsetof(dmu_buf_impl_t, db_link)); 383 384 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 385 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 386 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 387 388 DMU_META_DNODE(os) = dnode_special_open(os, 389 &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT, 390 &os->os_meta_dnode); 391 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 392 DMU_USERUSED_DNODE(os) = dnode_special_open(os, 393 &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT, 394 &os->os_userused_dnode); 395 DMU_GROUPUSED_DNODE(os) = dnode_special_open(os, 396 &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT, 397 &os->os_groupused_dnode); 398 } 399 400 /* 401 * We should be the only thread trying to do this because we 402 * have ds_opening_lock 403 */ 404 if (ds) { 405 mutex_enter(&ds->ds_lock); 406 ASSERT(ds->ds_objset == NULL); 407 ds->ds_objset = os; 408 mutex_exit(&ds->ds_lock); 409 } 410 411 *osp = os; 412 return (0); 413 } 414 415 int 416 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 417 { 418 int err = 0; 419 420 mutex_enter(&ds->ds_opening_lock); 421 *osp = ds->ds_objset; 422 if (*osp == NULL) { 423 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 424 ds, &ds->ds_phys->ds_bp, osp); 425 } 426 mutex_exit(&ds->ds_opening_lock); 427 return (err); 428 } 429 430 /* called from zpl */ 431 int 432 dmu_objset_hold(const char *name, void *tag, objset_t **osp) 433 { 434 dsl_dataset_t *ds; 435 int err; 436 437 err = dsl_dataset_hold(name, tag, &ds); 438 if (err) 439 return (err); 440 441 err = dmu_objset_from_ds(ds, osp); 442 if (err) 443 dsl_dataset_rele(ds, tag); 444 445 return (err); 446 } 447 448 /* called from zpl */ 449 int 450 dmu_objset_own(const char *name, dmu_objset_type_t type, 451 boolean_t readonly, void *tag, objset_t **osp) 452 { 453 dsl_dataset_t *ds; 454 int err; 455 456 err = dsl_dataset_own(name, B_FALSE, tag, &ds); 457 if (err) 458 return (err); 459 460 err = dmu_objset_from_ds(ds, osp); 461 if (err) { 462 dsl_dataset_disown(ds, tag); 463 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 464 dmu_objset_disown(*osp, tag); 465 return (EINVAL); 466 } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 467 dmu_objset_disown(*osp, tag); 468 return (EROFS); 469 } 470 return (err); 471 } 472 473 void 474 dmu_objset_rele(objset_t *os, void *tag) 475 { 476 dsl_dataset_rele(os->os_dsl_dataset, tag); 477 } 478 479 void 480 dmu_objset_disown(objset_t *os, void *tag) 481 { 482 dsl_dataset_disown(os->os_dsl_dataset, tag); 483 } 484 485 int 486 dmu_objset_evict_dbufs(objset_t *os) 487 { 488 dnode_t *dn; 489 490 mutex_enter(&os->os_lock); 491 492 /* process the mdn last, since the other dnodes have holds on it */ 493 list_remove(&os->os_dnodes, DMU_META_DNODE(os)); 494 list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os)); 495 496 /* 497 * Find the first dnode with holds. We have to do this dance 498 * because dnode_add_ref() only works if you already have a 499 * hold. If there are no holds then it has no dbufs so OK to 500 * skip. 501 */ 502 for (dn = list_head(&os->os_dnodes); 503 dn && !dnode_add_ref(dn, FTAG); 504 dn = list_next(&os->os_dnodes, dn)) 505 continue; 506 507 while (dn) { 508 dnode_t *next_dn = dn; 509 510 do { 511 next_dn = list_next(&os->os_dnodes, next_dn); 512 } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 513 514 mutex_exit(&os->os_lock); 515 dnode_evict_dbufs(dn); 516 dnode_rele(dn, FTAG); 517 mutex_enter(&os->os_lock); 518 dn = next_dn; 519 } 520 dn = list_head(&os->os_dnodes); 521 mutex_exit(&os->os_lock); 522 return (dn != DMU_META_DNODE(os)); 523 } 524 525 void 526 dmu_objset_evict(objset_t *os) 527 { 528 dsl_dataset_t *ds = os->os_dsl_dataset; 529 530 for (int t = 0; t < TXG_SIZE; t++) 531 ASSERT(!dmu_objset_is_dirty(os, t)); 532 533 if (ds) { 534 if (!dsl_dataset_is_snapshot(ds)) { 535 VERIFY(0 == dsl_prop_unregister(ds, "checksum", 536 checksum_changed_cb, os)); 537 VERIFY(0 == dsl_prop_unregister(ds, "compression", 538 compression_changed_cb, os)); 539 VERIFY(0 == dsl_prop_unregister(ds, "copies", 540 copies_changed_cb, os)); 541 VERIFY(0 == dsl_prop_unregister(ds, "dedup", 542 dedup_changed_cb, os)); 543 VERIFY(0 == dsl_prop_unregister(ds, "logbias", 544 logbias_changed_cb, os)); 545 VERIFY(0 == dsl_prop_unregister(ds, "sync", 546 sync_changed_cb, os)); 547 } 548 VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 549 primary_cache_changed_cb, os)); 550 VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 551 secondary_cache_changed_cb, os)); 552 } 553 554 if (os->os_sa) 555 sa_tear_down(os); 556 557 /* 558 * We should need only a single pass over the dnode list, since 559 * nothing can be added to the list at this point. 560 */ 561 (void) dmu_objset_evict_dbufs(os); 562 563 dnode_special_close(&os->os_meta_dnode); 564 if (DMU_USERUSED_DNODE(os)) { 565 dnode_special_close(&os->os_userused_dnode); 566 dnode_special_close(&os->os_groupused_dnode); 567 } 568 zil_free(os->os_zil); 569 570 ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 571 572 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1); 573 574 /* 575 * This is a barrier to prevent the objset from going away in 576 * dnode_move() until we can safely ensure that the objset is still in 577 * use. We consider the objset valid before the barrier and invalid 578 * after the barrier. 579 */ 580 rw_enter(&os_lock, RW_READER); 581 rw_exit(&os_lock); 582 583 mutex_destroy(&os->os_lock); 584 mutex_destroy(&os->os_obj_lock); 585 mutex_destroy(&os->os_user_ptr_lock); 586 kmem_free(os, sizeof (objset_t)); 587 } 588 589 timestruc_t 590 dmu_objset_snap_cmtime(objset_t *os) 591 { 592 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 593 } 594 595 /* called from dsl for meta-objset */ 596 objset_t * 597 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 598 dmu_objset_type_t type, dmu_tx_t *tx) 599 { 600 objset_t *os; 601 dnode_t *mdn; 602 603 ASSERT(dmu_tx_is_syncing(tx)); 604 if (ds) 605 mutex_enter(&ds->ds_opening_lock); 606 VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os)); 607 if (ds) 608 mutex_exit(&ds->ds_opening_lock); 609 mdn = DMU_META_DNODE(os); 610 611 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 612 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 613 614 /* 615 * We don't want to have to increase the meta-dnode's nlevels 616 * later, because then we could do it in quescing context while 617 * we are also accessing it in open context. 618 * 619 * This precaution is not necessary for the MOS (ds == NULL), 620 * because the MOS is only updated in syncing context. 621 * This is most fortunate: the MOS is the only objset that 622 * needs to be synced multiple times as spa_sync() iterates 623 * to convergence, so minimizing its dn_nlevels matters. 624 */ 625 if (ds != NULL) { 626 int levels = 1; 627 628 /* 629 * Determine the number of levels necessary for the meta-dnode 630 * to contain DN_MAX_OBJECT dnodes. 631 */ 632 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 633 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 634 DN_MAX_OBJECT * sizeof (dnode_phys_t)) 635 levels++; 636 637 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 638 mdn->dn_nlevels = levels; 639 } 640 641 ASSERT(type != DMU_OST_NONE); 642 ASSERT(type != DMU_OST_ANY); 643 ASSERT(type < DMU_OST_NUMTYPES); 644 os->os_phys->os_type = type; 645 if (dmu_objset_userused_enabled(os)) { 646 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 647 os->os_flags = os->os_phys->os_flags; 648 } 649 650 dsl_dataset_dirty(ds, tx); 651 652 return (os); 653 } 654 655 struct oscarg { 656 void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 657 void *userarg; 658 dsl_dataset_t *clone_origin; 659 const char *lastname; 660 dmu_objset_type_t type; 661 uint64_t flags; 662 cred_t *cr; 663 }; 664 665 /*ARGSUSED*/ 666 static int 667 dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 668 { 669 dsl_dir_t *dd = arg1; 670 struct oscarg *oa = arg2; 671 objset_t *mos = dd->dd_pool->dp_meta_objset; 672 int err; 673 uint64_t ddobj; 674 675 err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 676 oa->lastname, sizeof (uint64_t), 1, &ddobj); 677 if (err != ENOENT) 678 return (err ? err : EEXIST); 679 680 if (oa->clone_origin != NULL) { 681 /* You can't clone across pools. */ 682 if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool) 683 return (EXDEV); 684 685 /* You can only clone snapshots, not the head datasets. */ 686 if (!dsl_dataset_is_snapshot(oa->clone_origin)) 687 return (EINVAL); 688 } 689 690 return (0); 691 } 692 693 static void 694 dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx) 695 { 696 dsl_dir_t *dd = arg1; 697 struct oscarg *oa = arg2; 698 uint64_t dsobj; 699 700 ASSERT(dmu_tx_is_syncing(tx)); 701 702 dsobj = dsl_dataset_create_sync(dd, oa->lastname, 703 oa->clone_origin, oa->flags, oa->cr, tx); 704 705 if (oa->clone_origin == NULL) { 706 dsl_dataset_t *ds; 707 blkptr_t *bp; 708 objset_t *os; 709 710 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, 711 FTAG, &ds)); 712 bp = dsl_dataset_get_blkptr(ds); 713 ASSERT(BP_IS_HOLE(bp)); 714 715 os = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 716 ds, bp, oa->type, tx); 717 718 if (oa->userfunc) 719 oa->userfunc(os, oa->userarg, oa->cr, tx); 720 dsl_dataset_rele(ds, FTAG); 721 } 722 723 spa_history_log_internal(LOG_DS_CREATE, dd->dd_pool->dp_spa, 724 tx, "dataset = %llu", dsobj); 725 } 726 727 int 728 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 729 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 730 { 731 dsl_dir_t *pdd; 732 const char *tail; 733 int err = 0; 734 struct oscarg oa = { 0 }; 735 736 ASSERT(strchr(name, '@') == NULL); 737 err = dsl_dir_open(name, FTAG, &pdd, &tail); 738 if (err) 739 return (err); 740 if (tail == NULL) { 741 dsl_dir_close(pdd, FTAG); 742 return (EEXIST); 743 } 744 745 oa.userfunc = func; 746 oa.userarg = arg; 747 oa.lastname = tail; 748 oa.type = type; 749 oa.flags = flags; 750 oa.cr = CRED(); 751 752 err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 753 dmu_objset_create_sync, pdd, &oa, 5); 754 dsl_dir_close(pdd, FTAG); 755 return (err); 756 } 757 758 int 759 dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags) 760 { 761 dsl_dir_t *pdd; 762 const char *tail; 763 int err = 0; 764 struct oscarg oa = { 0 }; 765 766 ASSERT(strchr(name, '@') == NULL); 767 err = dsl_dir_open(name, FTAG, &pdd, &tail); 768 if (err) 769 return (err); 770 if (tail == NULL) { 771 dsl_dir_close(pdd, FTAG); 772 return (EEXIST); 773 } 774 775 oa.lastname = tail; 776 oa.clone_origin = clone_origin; 777 oa.flags = flags; 778 oa.cr = CRED(); 779 780 err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 781 dmu_objset_create_sync, pdd, &oa, 5); 782 dsl_dir_close(pdd, FTAG); 783 return (err); 784 } 785 786 int 787 dmu_objset_destroy(const char *name, boolean_t defer) 788 { 789 dsl_dataset_t *ds; 790 int error; 791 792 /* 793 * dsl_dataset_destroy() can free any claimed-but-unplayed 794 * intent log, but if there is an active log, it has blocks that 795 * are allocated, but may not yet be reflected in the on-disk 796 * structure. Only the ZIL knows how to free them, so we have 797 * to call into it here. 798 */ 799 error = dsl_dataset_own(name, B_TRUE, FTAG, &ds); 800 if (error == 0) { 801 objset_t *os; 802 if (dmu_objset_from_ds(ds, &os) == 0) 803 zil_destroy(dmu_objset_zil(os), B_FALSE); 804 error = dsl_dataset_destroy(ds, FTAG, defer); 805 /* dsl_dataset_destroy() closes the ds. */ 806 } 807 808 return (error); 809 } 810 811 struct snaparg { 812 dsl_sync_task_group_t *dstg; 813 char *snapname; 814 char failed[MAXPATHLEN]; 815 boolean_t recursive; 816 nvlist_t *props; 817 }; 818 819 static int 820 snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 821 { 822 objset_t *os = arg1; 823 struct snaparg *sn = arg2; 824 825 /* The props have already been checked by zfs_check_userprops(). */ 826 827 return (dsl_dataset_snapshot_check(os->os_dsl_dataset, 828 sn->snapname, tx)); 829 } 830 831 static void 832 snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx) 833 { 834 objset_t *os = arg1; 835 dsl_dataset_t *ds = os->os_dsl_dataset; 836 struct snaparg *sn = arg2; 837 838 dsl_dataset_snapshot_sync(ds, sn->snapname, tx); 839 840 if (sn->props) { 841 dsl_props_arg_t pa; 842 pa.pa_props = sn->props; 843 pa.pa_source = ZPROP_SRC_LOCAL; 844 dsl_props_set_sync(ds->ds_prev, &pa, tx); 845 } 846 } 847 848 static int 849 dmu_objset_snapshot_one(const char *name, void *arg) 850 { 851 struct snaparg *sn = arg; 852 objset_t *os; 853 int err; 854 char *cp; 855 856 /* 857 * If the objset starts with a '%', then ignore it unless it was 858 * explicitly named (ie, not recursive). These hidden datasets 859 * are always inconsistent, and by not opening them here, we can 860 * avoid a race with dsl_dir_destroy_check(). 861 */ 862 cp = strrchr(name, '/'); 863 if (cp && cp[1] == '%' && sn->recursive) 864 return (0); 865 866 (void) strcpy(sn->failed, name); 867 868 /* 869 * Check permissions if we are doing a recursive snapshot. The 870 * permission checks for the starting dataset have already been 871 * performed in zfs_secpolicy_snapshot() 872 */ 873 if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 874 return (err); 875 876 err = dmu_objset_hold(name, sn, &os); 877 if (err != 0) 878 return (err); 879 880 /* 881 * If the objset is in an inconsistent state (eg, in the process 882 * of being destroyed), don't snapshot it. As with %hidden 883 * datasets, we return EBUSY if this name was explicitly 884 * requested (ie, not recursive), and otherwise ignore it. 885 */ 886 if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 887 dmu_objset_rele(os, sn); 888 return (sn->recursive ? 0 : EBUSY); 889 } 890 891 /* 892 * NB: we need to wait for all in-flight changes to get to disk, 893 * so that we snapshot those changes. zil_suspend does this as 894 * a side effect. 895 */ 896 err = zil_suspend(dmu_objset_zil(os)); 897 if (err == 0) { 898 dsl_sync_task_create(sn->dstg, snapshot_check, 899 snapshot_sync, os, sn, 3); 900 } else { 901 dmu_objset_rele(os, sn); 902 } 903 904 return (err); 905 } 906 907 int 908 dmu_objset_snapshot(char *fsname, char *snapname, 909 nvlist_t *props, boolean_t recursive) 910 { 911 dsl_sync_task_t *dst; 912 struct snaparg sn; 913 spa_t *spa; 914 int err; 915 916 (void) strcpy(sn.failed, fsname); 917 918 err = spa_open(fsname, &spa, FTAG); 919 if (err) 920 return (err); 921 922 sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 923 sn.snapname = snapname; 924 sn.props = props; 925 sn.recursive = recursive; 926 927 if (recursive) { 928 err = dmu_objset_find(fsname, 929 dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 930 } else { 931 err = dmu_objset_snapshot_one(fsname, &sn); 932 } 933 934 if (err == 0) 935 err = dsl_sync_task_group_wait(sn.dstg); 936 937 for (dst = list_head(&sn.dstg->dstg_tasks); dst; 938 dst = list_next(&sn.dstg->dstg_tasks, dst)) { 939 objset_t *os = dst->dst_arg1; 940 dsl_dataset_t *ds = os->os_dsl_dataset; 941 if (dst->dst_err) 942 dsl_dataset_name(ds, sn.failed); 943 zil_resume(dmu_objset_zil(os)); 944 dmu_objset_rele(os, &sn); 945 } 946 947 if (err) 948 (void) strcpy(fsname, sn.failed); 949 dsl_sync_task_group_destroy(sn.dstg); 950 spa_close(spa, FTAG); 951 return (err); 952 } 953 954 static void 955 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 956 { 957 dnode_t *dn; 958 959 while (dn = list_head(list)) { 960 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 961 ASSERT(dn->dn_dbuf->db_data_pending); 962 /* 963 * Initialize dn_zio outside dnode_sync() because the 964 * meta-dnode needs to set it ouside dnode_sync(). 965 */ 966 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 967 ASSERT(dn->dn_zio); 968 969 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 970 list_remove(list, dn); 971 972 if (newlist) { 973 (void) dnode_add_ref(dn, newlist); 974 list_insert_tail(newlist, dn); 975 } 976 977 dnode_sync(dn, tx); 978 } 979 } 980 981 /* ARGSUSED */ 982 static void 983 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 984 { 985 blkptr_t *bp = zio->io_bp; 986 objset_t *os = arg; 987 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 988 989 ASSERT(bp == os->os_rootbp); 990 ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 991 ASSERT(BP_GET_LEVEL(bp) == 0); 992 993 /* 994 * Update rootbp fill count: it should be the number of objects 995 * allocated in the object set (not counting the "special" 996 * objects that are stored in the objset_phys_t -- the meta 997 * dnode and user/group accounting objects). 998 */ 999 bp->blk_fill = 0; 1000 for (int i = 0; i < dnp->dn_nblkptr; i++) 1001 bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 1002 } 1003 1004 /* ARGSUSED */ 1005 static void 1006 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 1007 { 1008 blkptr_t *bp = zio->io_bp; 1009 blkptr_t *bp_orig = &zio->io_bp_orig; 1010 objset_t *os = arg; 1011 1012 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 1013 ASSERT(BP_EQUAL(bp, bp_orig)); 1014 } else { 1015 dsl_dataset_t *ds = os->os_dsl_dataset; 1016 dmu_tx_t *tx = os->os_synctx; 1017 1018 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 1019 dsl_dataset_block_born(ds, bp, tx); 1020 } 1021 } 1022 1023 /* called from dsl */ 1024 void 1025 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 1026 { 1027 int txgoff; 1028 zbookmark_t zb; 1029 zio_prop_t zp; 1030 zio_t *zio; 1031 list_t *list; 1032 list_t *newlist = NULL; 1033 dbuf_dirty_record_t *dr; 1034 1035 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1036 1037 ASSERT(dmu_tx_is_syncing(tx)); 1038 /* XXX the write_done callback should really give us the tx... */ 1039 os->os_synctx = tx; 1040 1041 if (os->os_dsl_dataset == NULL) { 1042 /* 1043 * This is the MOS. If we have upgraded, 1044 * spa_max_replication() could change, so reset 1045 * os_copies here. 1046 */ 1047 os->os_copies = spa_max_replication(os->os_spa); 1048 } 1049 1050 /* 1051 * Create the root block IO 1052 */ 1053 SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1054 os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1055 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 1056 VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf, 1057 os->os_rootbp, os->os_spa, &zb)); 1058 1059 dmu_write_policy(os, NULL, 0, 0, &zp); 1060 1061 zio = arc_write(pio, os->os_spa, tx->tx_txg, 1062 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp, 1063 dmu_objset_write_ready, dmu_objset_write_done, os, 1064 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1065 1066 /* 1067 * Sync special dnodes - the parent IO for the sync is the root block 1068 */ 1069 DMU_META_DNODE(os)->dn_zio = zio; 1070 dnode_sync(DMU_META_DNODE(os), tx); 1071 1072 os->os_phys->os_flags = os->os_flags; 1073 1074 if (DMU_USERUSED_DNODE(os) && 1075 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1076 DMU_USERUSED_DNODE(os)->dn_zio = zio; 1077 dnode_sync(DMU_USERUSED_DNODE(os), tx); 1078 DMU_GROUPUSED_DNODE(os)->dn_zio = zio; 1079 dnode_sync(DMU_GROUPUSED_DNODE(os), tx); 1080 } 1081 1082 txgoff = tx->tx_txg & TXG_MASK; 1083 1084 if (dmu_objset_userused_enabled(os)) { 1085 newlist = &os->os_synced_dnodes; 1086 /* 1087 * We must create the list here because it uses the 1088 * dn_dirty_link[] of this txg. 1089 */ 1090 list_create(newlist, sizeof (dnode_t), 1091 offsetof(dnode_t, dn_dirty_link[txgoff])); 1092 } 1093 1094 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 1095 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 1096 1097 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; 1098 while (dr = list_head(list)) { 1099 ASSERT(dr->dr_dbuf->db_level == 0); 1100 list_remove(list, dr); 1101 if (dr->dr_zio) 1102 zio_nowait(dr->dr_zio); 1103 } 1104 /* 1105 * Free intent log blocks up to this tx. 1106 */ 1107 zil_sync(os->os_zil, tx); 1108 os->os_phys->os_zil_header = os->os_zil_header; 1109 zio_nowait(zio); 1110 } 1111 1112 boolean_t 1113 dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1114 { 1115 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) || 1116 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK])); 1117 } 1118 1119 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 1120 1121 void 1122 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 1123 { 1124 used_cbs[ost] = cb; 1125 } 1126 1127 boolean_t 1128 dmu_objset_userused_enabled(objset_t *os) 1129 { 1130 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 1131 used_cbs[os->os_phys->os_type] != NULL && 1132 DMU_USERUSED_DNODE(os) != NULL); 1133 } 1134 1135 static void 1136 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags, 1137 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx) 1138 { 1139 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 1140 int64_t delta = DNODE_SIZE + used; 1141 if (subtract) 1142 delta = -delta; 1143 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT, 1144 user, delta, tx)); 1145 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT, 1146 group, delta, tx)); 1147 } 1148 } 1149 1150 void 1151 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 1152 { 1153 dnode_t *dn; 1154 list_t *list = &os->os_synced_dnodes; 1155 1156 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 1157 1158 while (dn = list_head(list)) { 1159 int flags; 1160 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 1161 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 1162 dn->dn_phys->dn_flags & 1163 DNODE_FLAG_USERUSED_ACCOUNTED); 1164 1165 /* Allocate the user/groupused objects if necessary. */ 1166 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 1167 VERIFY(0 == zap_create_claim(os, 1168 DMU_USERUSED_OBJECT, 1169 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1170 VERIFY(0 == zap_create_claim(os, 1171 DMU_GROUPUSED_OBJECT, 1172 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1173 } 1174 1175 /* 1176 * We intentionally modify the zap object even if the 1177 * net delta is zero. Otherwise 1178 * the block of the zap obj could be shared between 1179 * datasets but need to be different between them after 1180 * a bprewrite. 1181 */ 1182 1183 flags = dn->dn_id_flags; 1184 ASSERT(flags); 1185 if (flags & DN_ID_OLD_EXIST) { 1186 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags, 1187 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx); 1188 } 1189 if (flags & DN_ID_NEW_EXIST) { 1190 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys), 1191 dn->dn_phys->dn_flags, dn->dn_newuid, 1192 dn->dn_newgid, B_FALSE, tx); 1193 } 1194 1195 mutex_enter(&dn->dn_mtx); 1196 dn->dn_oldused = 0; 1197 dn->dn_oldflags = 0; 1198 if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 1199 dn->dn_olduid = dn->dn_newuid; 1200 dn->dn_oldgid = dn->dn_newgid; 1201 dn->dn_id_flags |= DN_ID_OLD_EXIST; 1202 if (dn->dn_bonuslen == 0) 1203 dn->dn_id_flags |= DN_ID_CHKED_SPILL; 1204 else 1205 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 1206 } 1207 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); 1208 mutex_exit(&dn->dn_mtx); 1209 1210 list_remove(list, dn); 1211 dnode_rele(dn, list); 1212 } 1213 } 1214 1215 /* 1216 * Returns a pointer to data to find uid/gid from 1217 * 1218 * If a dirty record for transaction group that is syncing can't 1219 * be found then NULL is returned. In the NULL case it is assumed 1220 * the uid/gid aren't changing. 1221 */ 1222 static void * 1223 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 1224 { 1225 dbuf_dirty_record_t *dr, **drp; 1226 void *data; 1227 1228 if (db->db_dirtycnt == 0) 1229 return (db->db.db_data); /* Nothing is changing */ 1230 1231 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 1232 if (dr->dr_txg == tx->tx_txg) 1233 break; 1234 1235 if (dr == NULL) { 1236 data = NULL; 1237 } else { 1238 dnode_t *dn; 1239 1240 DB_DNODE_ENTER(dr->dr_dbuf); 1241 dn = DB_DNODE(dr->dr_dbuf); 1242 1243 if (dn->dn_bonuslen == 0 && 1244 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 1245 data = dr->dt.dl.dr_data->b_data; 1246 else 1247 data = dr->dt.dl.dr_data; 1248 1249 DB_DNODE_EXIT(dr->dr_dbuf); 1250 } 1251 1252 return (data); 1253 } 1254 1255 void 1256 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 1257 { 1258 objset_t *os = dn->dn_objset; 1259 void *data = NULL; 1260 dmu_buf_impl_t *db = NULL; 1261 uint64_t *user, *group; 1262 int flags = dn->dn_id_flags; 1263 int error; 1264 boolean_t have_spill = B_FALSE; 1265 1266 if (!dmu_objset_userused_enabled(dn->dn_objset)) 1267 return; 1268 1269 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 1270 DN_ID_CHKED_SPILL))) 1271 return; 1272 1273 if (before && dn->dn_bonuslen != 0) 1274 data = DN_BONUS(dn->dn_phys); 1275 else if (!before && dn->dn_bonuslen != 0) { 1276 if (dn->dn_bonus) { 1277 db = dn->dn_bonus; 1278 mutex_enter(&db->db_mtx); 1279 data = dmu_objset_userquota_find_data(db, tx); 1280 } else { 1281 data = DN_BONUS(dn->dn_phys); 1282 } 1283 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 1284 int rf = 0; 1285 1286 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 1287 rf |= DB_RF_HAVESTRUCT; 1288 error = dmu_spill_hold_by_dnode(dn, 1289 rf | DB_RF_MUST_SUCCEED, 1290 FTAG, (dmu_buf_t **)&db); 1291 ASSERT(error == 0); 1292 mutex_enter(&db->db_mtx); 1293 data = (before) ? db->db.db_data : 1294 dmu_objset_userquota_find_data(db, tx); 1295 have_spill = B_TRUE; 1296 } else { 1297 mutex_enter(&dn->dn_mtx); 1298 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 1299 mutex_exit(&dn->dn_mtx); 1300 return; 1301 } 1302 1303 if (before) { 1304 ASSERT(data); 1305 user = &dn->dn_olduid; 1306 group = &dn->dn_oldgid; 1307 } else if (data) { 1308 user = &dn->dn_newuid; 1309 group = &dn->dn_newgid; 1310 } 1311 1312 /* 1313 * Must always call the callback in case the object 1314 * type has changed and that type isn't an object type to track 1315 */ 1316 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 1317 user, group); 1318 1319 /* 1320 * Preserve existing uid/gid when the callback can't determine 1321 * what the new uid/gid are and the callback returned EEXIST. 1322 * The EEXIST error tells us to just use the existing uid/gid. 1323 * If we don't know what the old values are then just assign 1324 * them to 0, since that is a new file being created. 1325 */ 1326 if (!before && data == NULL && error == EEXIST) { 1327 if (flags & DN_ID_OLD_EXIST) { 1328 dn->dn_newuid = dn->dn_olduid; 1329 dn->dn_newgid = dn->dn_oldgid; 1330 } else { 1331 dn->dn_newuid = 0; 1332 dn->dn_newgid = 0; 1333 } 1334 error = 0; 1335 } 1336 1337 if (db) 1338 mutex_exit(&db->db_mtx); 1339 1340 mutex_enter(&dn->dn_mtx); 1341 if (error == 0 && before) 1342 dn->dn_id_flags |= DN_ID_OLD_EXIST; 1343 if (error == 0 && !before) 1344 dn->dn_id_flags |= DN_ID_NEW_EXIST; 1345 1346 if (have_spill) { 1347 dn->dn_id_flags |= DN_ID_CHKED_SPILL; 1348 } else { 1349 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 1350 } 1351 mutex_exit(&dn->dn_mtx); 1352 if (have_spill) 1353 dmu_buf_rele((dmu_buf_t *)db, FTAG); 1354 } 1355 1356 boolean_t 1357 dmu_objset_userspace_present(objset_t *os) 1358 { 1359 return (os->os_phys->os_flags & 1360 OBJSET_FLAG_USERACCOUNTING_COMPLETE); 1361 } 1362 1363 int 1364 dmu_objset_userspace_upgrade(objset_t *os) 1365 { 1366 uint64_t obj; 1367 int err = 0; 1368 1369 if (dmu_objset_userspace_present(os)) 1370 return (0); 1371 if (!dmu_objset_userused_enabled(os)) 1372 return (ENOTSUP); 1373 if (dmu_objset_is_snapshot(os)) 1374 return (EINVAL); 1375 1376 /* 1377 * We simply need to mark every object dirty, so that it will be 1378 * synced out and now accounted. If this is called 1379 * concurrently, or if we already did some work before crashing, 1380 * that's fine, since we track each object's accounted state 1381 * independently. 1382 */ 1383 1384 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 1385 dmu_tx_t *tx; 1386 dmu_buf_t *db; 1387 int objerr; 1388 1389 if (issig(JUSTLOOKING) && issig(FORREAL)) 1390 return (EINTR); 1391 1392 objerr = dmu_bonus_hold(os, obj, FTAG, &db); 1393 if (objerr) 1394 continue; 1395 tx = dmu_tx_create(os); 1396 dmu_tx_hold_bonus(tx, obj); 1397 objerr = dmu_tx_assign(tx, TXG_WAIT); 1398 if (objerr) { 1399 dmu_tx_abort(tx); 1400 continue; 1401 } 1402 dmu_buf_will_dirty(db, tx); 1403 dmu_buf_rele(db, FTAG); 1404 dmu_tx_commit(tx); 1405 } 1406 1407 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 1408 txg_wait_synced(dmu_objset_pool(os), 0); 1409 return (0); 1410 } 1411 1412 void 1413 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1414 uint64_t *usedobjsp, uint64_t *availobjsp) 1415 { 1416 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1417 usedobjsp, availobjsp); 1418 } 1419 1420 uint64_t 1421 dmu_objset_fsid_guid(objset_t *os) 1422 { 1423 return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1424 } 1425 1426 void 1427 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1428 { 1429 stat->dds_type = os->os_phys->os_type; 1430 if (os->os_dsl_dataset) 1431 dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1432 } 1433 1434 void 1435 dmu_objset_stats(objset_t *os, nvlist_t *nv) 1436 { 1437 ASSERT(os->os_dsl_dataset || 1438 os->os_phys->os_type == DMU_OST_META); 1439 1440 if (os->os_dsl_dataset != NULL) 1441 dsl_dataset_stats(os->os_dsl_dataset, nv); 1442 1443 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1444 os->os_phys->os_type); 1445 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 1446 dmu_objset_userspace_present(os)); 1447 } 1448 1449 int 1450 dmu_objset_is_snapshot(objset_t *os) 1451 { 1452 if (os->os_dsl_dataset != NULL) 1453 return (dsl_dataset_is_snapshot(os->os_dsl_dataset)); 1454 else 1455 return (B_FALSE); 1456 } 1457 1458 int 1459 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1460 boolean_t *conflict) 1461 { 1462 dsl_dataset_t *ds = os->os_dsl_dataset; 1463 uint64_t ignored; 1464 1465 if (ds->ds_phys->ds_snapnames_zapobj == 0) 1466 return (ENOENT); 1467 1468 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1469 ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 1470 real, maxlen, conflict)); 1471 } 1472 1473 int 1474 dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1475 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1476 { 1477 dsl_dataset_t *ds = os->os_dsl_dataset; 1478 zap_cursor_t cursor; 1479 zap_attribute_t attr; 1480 1481 if (ds->ds_phys->ds_snapnames_zapobj == 0) 1482 return (ENOENT); 1483 1484 zap_cursor_init_serialized(&cursor, 1485 ds->ds_dir->dd_pool->dp_meta_objset, 1486 ds->ds_phys->ds_snapnames_zapobj, *offp); 1487 1488 if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1489 zap_cursor_fini(&cursor); 1490 return (ENOENT); 1491 } 1492 1493 if (strlen(attr.za_name) + 1 > namelen) { 1494 zap_cursor_fini(&cursor); 1495 return (ENAMETOOLONG); 1496 } 1497 1498 (void) strcpy(name, attr.za_name); 1499 if (idp) 1500 *idp = attr.za_first_integer; 1501 if (case_conflict) 1502 *case_conflict = attr.za_normalization_conflict; 1503 zap_cursor_advance(&cursor); 1504 *offp = zap_cursor_serialize(&cursor); 1505 zap_cursor_fini(&cursor); 1506 1507 return (0); 1508 } 1509 1510 int 1511 dmu_dir_list_next(objset_t *os, int namelen, char *name, 1512 uint64_t *idp, uint64_t *offp) 1513 { 1514 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 1515 zap_cursor_t cursor; 1516 zap_attribute_t attr; 1517 1518 /* there is no next dir on a snapshot! */ 1519 if (os->os_dsl_dataset->ds_object != 1520 dd->dd_phys->dd_head_dataset_obj) 1521 return (ENOENT); 1522 1523 zap_cursor_init_serialized(&cursor, 1524 dd->dd_pool->dp_meta_objset, 1525 dd->dd_phys->dd_child_dir_zapobj, *offp); 1526 1527 if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1528 zap_cursor_fini(&cursor); 1529 return (ENOENT); 1530 } 1531 1532 if (strlen(attr.za_name) + 1 > namelen) { 1533 zap_cursor_fini(&cursor); 1534 return (ENAMETOOLONG); 1535 } 1536 1537 (void) strcpy(name, attr.za_name); 1538 if (idp) 1539 *idp = attr.za_first_integer; 1540 zap_cursor_advance(&cursor); 1541 *offp = zap_cursor_serialize(&cursor); 1542 zap_cursor_fini(&cursor); 1543 1544 return (0); 1545 } 1546 1547 struct findarg { 1548 int (*func)(const char *, void *); 1549 void *arg; 1550 }; 1551 1552 /* ARGSUSED */ 1553 static int 1554 findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1555 { 1556 struct findarg *fa = arg; 1557 return (fa->func(dsname, fa->arg)); 1558 } 1559 1560 /* 1561 * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1562 * Perhaps change all callers to use dmu_objset_find_spa()? 1563 */ 1564 int 1565 dmu_objset_find(char *name, int func(const char *, void *), void *arg, 1566 int flags) 1567 { 1568 struct findarg fa; 1569 fa.func = func; 1570 fa.arg = arg; 1571 return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1572 } 1573 1574 /* 1575 * Find all objsets under name, call func on each 1576 */ 1577 int 1578 dmu_objset_find_spa(spa_t *spa, const char *name, 1579 int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1580 { 1581 dsl_dir_t *dd; 1582 dsl_pool_t *dp; 1583 dsl_dataset_t *ds; 1584 zap_cursor_t zc; 1585 zap_attribute_t *attr; 1586 char *child; 1587 uint64_t thisobj; 1588 int err; 1589 1590 if (name == NULL) 1591 name = spa_name(spa); 1592 err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1593 if (err) 1594 return (err); 1595 1596 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1597 if (dd->dd_myname[0] == '$') { 1598 dsl_dir_close(dd, FTAG); 1599 return (0); 1600 } 1601 1602 thisobj = dd->dd_phys->dd_head_dataset_obj; 1603 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1604 dp = dd->dd_pool; 1605 1606 /* 1607 * Iterate over all children. 1608 */ 1609 if (flags & DS_FIND_CHILDREN) { 1610 for (zap_cursor_init(&zc, dp->dp_meta_objset, 1611 dd->dd_phys->dd_child_dir_zapobj); 1612 zap_cursor_retrieve(&zc, attr) == 0; 1613 (void) zap_cursor_advance(&zc)) { 1614 ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1615 ASSERT(attr->za_num_integers == 1); 1616 1617 child = kmem_asprintf("%s/%s", name, attr->za_name); 1618 err = dmu_objset_find_spa(spa, child, func, arg, flags); 1619 strfree(child); 1620 if (err) 1621 break; 1622 } 1623 zap_cursor_fini(&zc); 1624 1625 if (err) { 1626 dsl_dir_close(dd, FTAG); 1627 kmem_free(attr, sizeof (zap_attribute_t)); 1628 return (err); 1629 } 1630 } 1631 1632 /* 1633 * Iterate over all snapshots. 1634 */ 1635 if (flags & DS_FIND_SNAPSHOTS) { 1636 if (!dsl_pool_sync_context(dp)) 1637 rw_enter(&dp->dp_config_rwlock, RW_READER); 1638 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1639 if (!dsl_pool_sync_context(dp)) 1640 rw_exit(&dp->dp_config_rwlock); 1641 1642 if (err == 0) { 1643 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1644 dsl_dataset_rele(ds, FTAG); 1645 1646 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1647 zap_cursor_retrieve(&zc, attr) == 0; 1648 (void) zap_cursor_advance(&zc)) { 1649 ASSERT(attr->za_integer_length == 1650 sizeof (uint64_t)); 1651 ASSERT(attr->za_num_integers == 1); 1652 1653 child = kmem_asprintf("%s@%s", 1654 name, attr->za_name); 1655 err = func(spa, attr->za_first_integer, 1656 child, arg); 1657 strfree(child); 1658 if (err) 1659 break; 1660 } 1661 zap_cursor_fini(&zc); 1662 } 1663 } 1664 1665 dsl_dir_close(dd, FTAG); 1666 kmem_free(attr, sizeof (zap_attribute_t)); 1667 1668 if (err) 1669 return (err); 1670 1671 /* 1672 * Apply to self if appropriate. 1673 */ 1674 err = func(spa, thisobj, name, arg); 1675 return (err); 1676 } 1677 1678 /* ARGSUSED */ 1679 int 1680 dmu_objset_prefetch(const char *name, void *arg) 1681 { 1682 dsl_dataset_t *ds; 1683 1684 if (dsl_dataset_hold(name, FTAG, &ds)) 1685 return (0); 1686 1687 if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 1688 mutex_enter(&ds->ds_opening_lock); 1689 if (ds->ds_objset == NULL) { 1690 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 1691 zbookmark_t zb; 1692 1693 SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT, 1694 ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 1695 1696 (void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds), 1697 &ds->ds_phys->ds_bp, NULL, NULL, 1698 ZIO_PRIORITY_ASYNC_READ, 1699 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 1700 &aflags, &zb); 1701 } 1702 mutex_exit(&ds->ds_opening_lock); 1703 } 1704 1705 dsl_dataset_rele(ds, FTAG); 1706 return (0); 1707 } 1708 1709 void 1710 dmu_objset_set_user(objset_t *os, void *user_ptr) 1711 { 1712 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1713 os->os_user_ptr = user_ptr; 1714 } 1715 1716 void * 1717 dmu_objset_get_user(objset_t *os) 1718 { 1719 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1720 return (os->os_user_ptr); 1721 } 1722