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 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved. 25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 28 * Copyright (c) 2015, STRATO AG, Inc. All rights reserved. 29 * Copyright (c) 2014 Integros [integros.com] 30 * Copyright 2017 Nexenta Systems, Inc. 31 */ 32 33 /* Portions Copyright 2010 Robert Milkowski */ 34 35 #include <sys/cred.h> 36 #include <sys/zfs_context.h> 37 #include <sys/dmu_objset.h> 38 #include <sys/dsl_dir.h> 39 #include <sys/dsl_dataset.h> 40 #include <sys/dsl_prop.h> 41 #include <sys/dsl_pool.h> 42 #include <sys/dsl_synctask.h> 43 #include <sys/dsl_deleg.h> 44 #include <sys/dnode.h> 45 #include <sys/dbuf.h> 46 #include <sys/zvol.h> 47 #include <sys/dmu_tx.h> 48 #include <sys/zap.h> 49 #include <sys/zil.h> 50 #include <sys/dmu_impl.h> 51 #include <sys/zfs_ioctl.h> 52 #include <sys/sa.h> 53 #include <sys/zfs_onexit.h> 54 #include <sys/dsl_destroy.h> 55 #include <sys/vdev.h> 56 #include <sys/zfeature.h> 57 #include <sys/dmu_recv.h> 58 #include "zfs_namecheck.h" 59 60 /* 61 * Needed to close a window in dnode_move() that allows the objset to be freed 62 * before it can be safely accessed. 63 */ 64 krwlock_t os_lock; 65 66 /* 67 * Tunable to overwrite the maximum number of threads for the parallization 68 * of dmu_objset_find_dp, needed to speed up the import of pools with many 69 * datasets. 70 * Default is 4 times the number of leaf vdevs. 71 */ 72 int dmu_find_threads = 0; 73 74 /* 75 * Backfill lower metadnode objects after this many have been freed. 76 * Backfilling negatively impacts object creation rates, so only do it 77 * if there are enough holes to fill. 78 */ 79 int dmu_rescan_dnode_threshold = 131072; 80 81 static void dmu_objset_find_dp_cb(void *arg); 82 83 void 84 dmu_objset_init(void) 85 { 86 rw_init(&os_lock, NULL, RW_DEFAULT, NULL); 87 } 88 89 void 90 dmu_objset_fini(void) 91 { 92 rw_destroy(&os_lock); 93 } 94 95 spa_t * 96 dmu_objset_spa(objset_t *os) 97 { 98 return (os->os_spa); 99 } 100 101 zilog_t * 102 dmu_objset_zil(objset_t *os) 103 { 104 return (os->os_zil); 105 } 106 107 dsl_pool_t * 108 dmu_objset_pool(objset_t *os) 109 { 110 dsl_dataset_t *ds; 111 112 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 113 return (ds->ds_dir->dd_pool); 114 else 115 return (spa_get_dsl(os->os_spa)); 116 } 117 118 dsl_dataset_t * 119 dmu_objset_ds(objset_t *os) 120 { 121 return (os->os_dsl_dataset); 122 } 123 124 dmu_objset_type_t 125 dmu_objset_type(objset_t *os) 126 { 127 return (os->os_phys->os_type); 128 } 129 130 void 131 dmu_objset_name(objset_t *os, char *buf) 132 { 133 dsl_dataset_name(os->os_dsl_dataset, buf); 134 } 135 136 uint64_t 137 dmu_objset_id(objset_t *os) 138 { 139 dsl_dataset_t *ds = os->os_dsl_dataset; 140 141 return (ds ? ds->ds_object : 0); 142 } 143 144 uint64_t 145 dmu_objset_dnodesize(objset_t *os) 146 { 147 return (os->os_dnodesize); 148 } 149 150 zfs_sync_type_t 151 dmu_objset_syncprop(objset_t *os) 152 { 153 return (os->os_sync); 154 } 155 156 zfs_logbias_op_t 157 dmu_objset_logbias(objset_t *os) 158 { 159 return (os->os_logbias); 160 } 161 162 static void 163 checksum_changed_cb(void *arg, uint64_t newval) 164 { 165 objset_t *os = arg; 166 167 /* 168 * Inheritance should have been done by now. 169 */ 170 ASSERT(newval != ZIO_CHECKSUM_INHERIT); 171 172 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 173 } 174 175 static void 176 compression_changed_cb(void *arg, uint64_t newval) 177 { 178 objset_t *os = arg; 179 180 /* 181 * Inheritance and range checking should have been done by now. 182 */ 183 ASSERT(newval != ZIO_COMPRESS_INHERIT); 184 185 os->os_compress = zio_compress_select(os->os_spa, newval, 186 ZIO_COMPRESS_ON); 187 } 188 189 static void 190 copies_changed_cb(void *arg, uint64_t newval) 191 { 192 objset_t *os = arg; 193 194 /* 195 * Inheritance and range checking should have been done by now. 196 */ 197 ASSERT(newval > 0); 198 ASSERT(newval <= spa_max_replication(os->os_spa)); 199 200 os->os_copies = newval; 201 } 202 203 static void 204 dedup_changed_cb(void *arg, uint64_t newval) 205 { 206 objset_t *os = arg; 207 spa_t *spa = os->os_spa; 208 enum zio_checksum checksum; 209 210 /* 211 * Inheritance should have been done by now. 212 */ 213 ASSERT(newval != ZIO_CHECKSUM_INHERIT); 214 215 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 216 217 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 218 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 219 } 220 221 static void 222 primary_cache_changed_cb(void *arg, uint64_t newval) 223 { 224 objset_t *os = arg; 225 226 /* 227 * Inheritance and range checking should have been done by now. 228 */ 229 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 230 newval == ZFS_CACHE_METADATA); 231 232 os->os_primary_cache = newval; 233 } 234 235 static void 236 secondary_cache_changed_cb(void *arg, uint64_t newval) 237 { 238 objset_t *os = arg; 239 240 /* 241 * Inheritance and range checking should have been done by now. 242 */ 243 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 244 newval == ZFS_CACHE_METADATA); 245 246 os->os_secondary_cache = newval; 247 } 248 249 static void 250 sync_changed_cb(void *arg, uint64_t newval) 251 { 252 objset_t *os = arg; 253 254 /* 255 * Inheritance and range checking should have been done by now. 256 */ 257 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 258 newval == ZFS_SYNC_DISABLED); 259 260 os->os_sync = newval; 261 if (os->os_zil) 262 zil_set_sync(os->os_zil, newval); 263 } 264 265 static void 266 redundant_metadata_changed_cb(void *arg, uint64_t newval) 267 { 268 objset_t *os = arg; 269 270 /* 271 * Inheritance and range checking should have been done by now. 272 */ 273 ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL || 274 newval == ZFS_REDUNDANT_METADATA_MOST); 275 276 os->os_redundant_metadata = newval; 277 } 278 279 static void 280 dnodesize_changed_cb(void *arg, uint64_t newval) 281 { 282 objset_t *os = arg; 283 284 switch (newval) { 285 case ZFS_DNSIZE_LEGACY: 286 os->os_dnodesize = DNODE_MIN_SIZE; 287 break; 288 case ZFS_DNSIZE_AUTO: 289 /* 290 * Choose a dnode size that will work well for most 291 * workloads if the user specified "auto". Future code 292 * improvements could dynamically select a dnode size 293 * based on observed workload patterns. 294 */ 295 os->os_dnodesize = DNODE_MIN_SIZE * 2; 296 break; 297 case ZFS_DNSIZE_1K: 298 case ZFS_DNSIZE_2K: 299 case ZFS_DNSIZE_4K: 300 case ZFS_DNSIZE_8K: 301 case ZFS_DNSIZE_16K: 302 os->os_dnodesize = newval; 303 break; 304 } 305 } 306 307 static void 308 smallblk_changed_cb(void *arg, uint64_t newval) 309 { 310 objset_t *os = arg; 311 312 /* 313 * Inheritance and range checking should have been done by now. 314 */ 315 ASSERT(newval <= SPA_OLD_MAXBLOCKSIZE); 316 ASSERT(ISP2(newval)); 317 318 os->os_zpl_special_smallblock = newval; 319 } 320 321 static void 322 logbias_changed_cb(void *arg, uint64_t newval) 323 { 324 objset_t *os = arg; 325 326 ASSERT(newval == ZFS_LOGBIAS_LATENCY || 327 newval == ZFS_LOGBIAS_THROUGHPUT); 328 os->os_logbias = newval; 329 if (os->os_zil) 330 zil_set_logbias(os->os_zil, newval); 331 } 332 333 static void 334 recordsize_changed_cb(void *arg, uint64_t newval) 335 { 336 objset_t *os = arg; 337 338 os->os_recordsize = newval; 339 } 340 341 void 342 dmu_objset_byteswap(void *buf, size_t size) 343 { 344 objset_phys_t *osp = buf; 345 346 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 347 dnode_byteswap(&osp->os_meta_dnode); 348 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 349 osp->os_type = BSWAP_64(osp->os_type); 350 osp->os_flags = BSWAP_64(osp->os_flags); 351 if (size == sizeof (objset_phys_t)) { 352 dnode_byteswap(&osp->os_userused_dnode); 353 dnode_byteswap(&osp->os_groupused_dnode); 354 } 355 } 356 357 /* 358 * The hash is a CRC-based hash of the objset_t pointer and the object number. 359 */ 360 static uint64_t 361 dnode_hash(const objset_t *os, uint64_t obj) 362 { 363 uintptr_t osv = (uintptr_t)os; 364 uint64_t crc = -1ULL; 365 366 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 367 /* 368 * The low 6 bits of the pointer don't have much entropy, because 369 * the objset_t is larger than 2^6 bytes long. 370 */ 371 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF]; 372 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF]; 373 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF]; 374 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF]; 375 376 crc ^= (osv>>14) ^ (obj>>24); 377 378 return (crc); 379 } 380 381 unsigned int 382 dnode_multilist_index_func(multilist_t *ml, void *obj) 383 { 384 dnode_t *dn = obj; 385 return (dnode_hash(dn->dn_objset, dn->dn_object) % 386 multilist_get_num_sublists(ml)); 387 } 388 389 /* 390 * Instantiates the objset_t in-memory structure corresponding to the 391 * objset_phys_t that's pointed to by the specified blkptr_t. 392 */ 393 int 394 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 395 objset_t **osp) 396 { 397 objset_t *os; 398 int i, err; 399 400 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 401 402 #if 0 403 /* 404 * The $ORIGIN dataset (if it exists) doesn't have an associated 405 * objset, so there's no reason to open it. The $ORIGIN dataset 406 * will not exist on pools older than SPA_VERSION_ORIGIN. 407 */ 408 if (ds != NULL && spa_get_dsl(spa) != NULL && 409 spa_get_dsl(spa)->dp_origin_snap != NULL) { 410 ASSERT3P(ds->ds_dir, !=, 411 spa_get_dsl(spa)->dp_origin_snap->ds_dir); 412 } 413 #endif 414 415 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 416 os->os_dsl_dataset = ds; 417 os->os_spa = spa; 418 os->os_rootbp = bp; 419 if (!BP_IS_HOLE(os->os_rootbp)) { 420 arc_flags_t aflags = ARC_FLAG_WAIT; 421 zbookmark_phys_t zb; 422 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 423 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 424 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 425 426 if (DMU_OS_IS_L2CACHEABLE(os)) 427 aflags |= ARC_FLAG_L2CACHE; 428 429 if (ds != NULL && ds->ds_dir->dd_crypto_obj != 0) { 430 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); 431 ASSERT(BP_IS_AUTHENTICATED(bp)); 432 zio_flags |= ZIO_FLAG_RAW; 433 } 434 435 dprintf_bp(os->os_rootbp, "reading %s", ""); 436 err = arc_read(NULL, spa, os->os_rootbp, 437 arc_getbuf_func, &os->os_phys_buf, 438 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 439 if (err != 0) { 440 kmem_free(os, sizeof (objset_t)); 441 /* convert checksum errors into IO errors */ 442 if (err == ECKSUM) 443 err = SET_ERROR(EIO); 444 return (err); 445 } 446 447 /* Increase the blocksize if we are permitted. */ 448 if (spa_version(spa) >= SPA_VERSION_USERSPACE && 449 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 450 arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf, 451 ARC_BUFC_METADATA, sizeof (objset_phys_t)); 452 bzero(buf->b_data, sizeof (objset_phys_t)); 453 bcopy(os->os_phys_buf->b_data, buf->b_data, 454 arc_buf_size(os->os_phys_buf)); 455 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 456 os->os_phys_buf = buf; 457 } 458 459 os->os_phys = os->os_phys_buf->b_data; 460 os->os_flags = os->os_phys->os_flags; 461 } else { 462 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 463 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 464 os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf, 465 ARC_BUFC_METADATA, size); 466 os->os_phys = os->os_phys_buf->b_data; 467 bzero(os->os_phys, size); 468 } 469 470 /* 471 * Note: the changed_cb will be called once before the register 472 * func returns, thus changing the checksum/compression from the 473 * default (fletcher2/off). Snapshots don't need to know about 474 * checksum/compression/copies. 475 */ 476 if (ds != NULL) { 477 boolean_t needlock = B_FALSE; 478 479 os->os_encrypted = (ds->ds_dir->dd_crypto_obj != 0); 480 481 /* 482 * Note: it's valid to open the objset if the dataset is 483 * long-held, in which case the pool_config lock will not 484 * be held. 485 */ 486 if (!dsl_pool_config_held(dmu_objset_pool(os))) { 487 needlock = B_TRUE; 488 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 489 } 490 491 err = dsl_prop_register(ds, 492 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE), 493 primary_cache_changed_cb, os); 494 if (err == 0) { 495 err = dsl_prop_register(ds, 496 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE), 497 secondary_cache_changed_cb, os); 498 } 499 if (!ds->ds_is_snapshot) { 500 if (err == 0) { 501 err = dsl_prop_register(ds, 502 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 503 checksum_changed_cb, os); 504 } 505 if (err == 0) { 506 err = dsl_prop_register(ds, 507 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 508 compression_changed_cb, os); 509 } 510 if (err == 0) { 511 err = dsl_prop_register(ds, 512 zfs_prop_to_name(ZFS_PROP_COPIES), 513 copies_changed_cb, os); 514 } 515 if (err == 0) { 516 err = dsl_prop_register(ds, 517 zfs_prop_to_name(ZFS_PROP_DEDUP), 518 dedup_changed_cb, os); 519 } 520 if (err == 0) { 521 err = dsl_prop_register(ds, 522 zfs_prop_to_name(ZFS_PROP_LOGBIAS), 523 logbias_changed_cb, os); 524 } 525 if (err == 0) { 526 err = dsl_prop_register(ds, 527 zfs_prop_to_name(ZFS_PROP_SYNC), 528 sync_changed_cb, os); 529 } 530 if (err == 0) { 531 err = dsl_prop_register(ds, 532 zfs_prop_to_name( 533 ZFS_PROP_REDUNDANT_METADATA), 534 redundant_metadata_changed_cb, os); 535 } 536 if (err == 0) { 537 err = dsl_prop_register(ds, 538 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), 539 recordsize_changed_cb, os); 540 } 541 if (err == 0) { 542 err = dsl_prop_register(ds, 543 zfs_prop_to_name(ZFS_PROP_DNODESIZE), 544 dnodesize_changed_cb, os); 545 } 546 if (err == 0) { 547 err = dsl_prop_register(ds, 548 zfs_prop_to_name( 549 ZFS_PROP_SPECIAL_SMALL_BLOCKS), 550 smallblk_changed_cb, os); 551 } 552 } 553 if (needlock) 554 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 555 if (err != 0) { 556 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 557 kmem_free(os, sizeof (objset_t)); 558 return (err); 559 } 560 } else { 561 /* It's the meta-objset. */ 562 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 563 os->os_compress = ZIO_COMPRESS_ON; 564 os->os_encrypted = B_FALSE; 565 os->os_copies = spa_max_replication(spa); 566 os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 567 os->os_dedup_verify = B_FALSE; 568 os->os_logbias = ZFS_LOGBIAS_LATENCY; 569 os->os_sync = ZFS_SYNC_STANDARD; 570 os->os_primary_cache = ZFS_CACHE_ALL; 571 os->os_secondary_cache = ZFS_CACHE_ALL; 572 os->os_dnodesize = DNODE_MIN_SIZE; 573 } 574 /* 575 * These properties will be filled in by the logic in zfs_get_zplprop() 576 * when they are queried for the first time. 577 */ 578 os->os_version = OBJSET_PROP_UNINITIALIZED; 579 os->os_normalization = OBJSET_PROP_UNINITIALIZED; 580 os->os_utf8only = OBJSET_PROP_UNINITIALIZED; 581 os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED; 582 583 if (ds == NULL || !ds->ds_is_snapshot) 584 os->os_zil_header = os->os_phys->os_zil_header; 585 os->os_zil = zil_alloc(os, &os->os_zil_header); 586 587 for (i = 0; i < TXG_SIZE; i++) { 588 os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t), 589 offsetof(dnode_t, dn_dirty_link[i]), 590 dnode_multilist_index_func); 591 } 592 list_create(&os->os_dnodes, sizeof (dnode_t), 593 offsetof(dnode_t, dn_link)); 594 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 595 offsetof(dmu_buf_impl_t, db_link)); 596 597 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 598 mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL); 599 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 600 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 601 os->os_obj_next_percpu_len = boot_ncpus; 602 os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len * 603 sizeof (os->os_obj_next_percpu[0]), KM_SLEEP); 604 605 dnode_special_open(os, &os->os_phys->os_meta_dnode, 606 DMU_META_DNODE_OBJECT, &os->os_meta_dnode); 607 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 608 dnode_special_open(os, &os->os_phys->os_userused_dnode, 609 DMU_USERUSED_OBJECT, &os->os_userused_dnode); 610 dnode_special_open(os, &os->os_phys->os_groupused_dnode, 611 DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode); 612 } 613 614 *osp = os; 615 return (0); 616 } 617 618 int 619 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 620 { 621 int err = 0; 622 623 /* 624 * We shouldn't be doing anything with dsl_dataset_t's unless the 625 * pool_config lock is held, or the dataset is long-held. 626 */ 627 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) || 628 dsl_dataset_long_held(ds)); 629 630 mutex_enter(&ds->ds_opening_lock); 631 if (ds->ds_objset == NULL) { 632 objset_t *os; 633 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 634 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 635 ds, dsl_dataset_get_blkptr(ds), &os); 636 rrw_exit(&ds->ds_bp_rwlock, FTAG); 637 638 if (err == 0) { 639 mutex_enter(&ds->ds_lock); 640 ASSERT(ds->ds_objset == NULL); 641 ds->ds_objset = os; 642 mutex_exit(&ds->ds_lock); 643 } 644 } 645 *osp = ds->ds_objset; 646 mutex_exit(&ds->ds_opening_lock); 647 return (err); 648 } 649 650 /* 651 * Holds the pool while the objset is held. Therefore only one objset 652 * can be held at a time. 653 */ 654 int 655 dmu_objset_hold_flags(const char *name, boolean_t decrypt, void *tag, 656 objset_t **osp) 657 { 658 dsl_pool_t *dp; 659 dsl_dataset_t *ds; 660 int err; 661 ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; 662 663 err = dsl_pool_hold(name, tag, &dp); 664 if (err != 0) 665 return (err); 666 err = dsl_dataset_hold_flags(dp, name, flags, tag, &ds); 667 if (err != 0) { 668 dsl_pool_rele(dp, tag); 669 return (err); 670 } 671 672 err = dmu_objset_from_ds(ds, osp); 673 if (err != 0) { 674 dsl_dataset_rele(ds, tag); 675 dsl_pool_rele(dp, tag); 676 } 677 678 return (err); 679 } 680 681 int 682 dmu_objset_hold(const char *name, void *tag, objset_t **osp) 683 { 684 return (dmu_objset_hold_flags(name, B_FALSE, tag, osp)); 685 } 686 687 /* ARGSUSED */ 688 static int 689 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type, 690 boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) 691 { 692 int err; 693 694 err = dmu_objset_from_ds(ds, osp); 695 if (err != 0) { 696 return (err); 697 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 698 return (SET_ERROR(EINVAL)); 699 } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 700 return (SET_ERROR(EROFS)); 701 } else if (!readonly && decrypt && 702 dsl_dir_incompatible_encryption_version(ds->ds_dir)) { 703 return (SET_ERROR(EROFS)); 704 } 705 706 /* if we are decrypting, we can now check MACs in os->os_phys_buf */ 707 if (decrypt && arc_is_unauthenticated((*osp)->os_phys_buf)) { 708 zbookmark_phys_t zb; 709 710 SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT, 711 ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 712 err = arc_untransform((*osp)->os_phys_buf, (*osp)->os_spa, 713 &zb, B_FALSE); 714 if (err != 0) 715 return (err); 716 717 ASSERT0(arc_is_unauthenticated((*osp)->os_phys_buf)); 718 } 719 720 return (0); 721 } 722 723 /* 724 * dsl_pool must not be held when this is called. 725 * Upon successful return, there will be a longhold on the dataset, 726 * and the dsl_pool will not be held. 727 */ 728 int 729 dmu_objset_own(const char *name, dmu_objset_type_t type, 730 boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) 731 { 732 dsl_pool_t *dp; 733 dsl_dataset_t *ds; 734 int err; 735 ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; 736 737 err = dsl_pool_hold(name, FTAG, &dp); 738 if (err != 0) 739 return (err); 740 err = dsl_dataset_own(dp, name, flags, tag, &ds); 741 if (err != 0) { 742 dsl_pool_rele(dp, FTAG); 743 return (err); 744 } 745 err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp); 746 if (err != 0) { 747 dsl_dataset_disown(ds, flags, tag); 748 dsl_pool_rele(dp, FTAG); 749 return (err); 750 } 751 752 dsl_pool_rele(dp, FTAG); 753 754 return (0); 755 } 756 757 int 758 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type, 759 boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) 760 { 761 dsl_dataset_t *ds; 762 int err; 763 ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; 764 765 err = dsl_dataset_own_obj(dp, obj, flags, tag, &ds); 766 if (err != 0) 767 return (err); 768 769 err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp); 770 if (err != 0) { 771 dsl_dataset_disown(ds, flags, tag); 772 return (err); 773 } 774 775 return (0); 776 } 777 778 void 779 dmu_objset_rele_flags(objset_t *os, boolean_t decrypt, void *tag) 780 { 781 ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; 782 783 dsl_pool_t *dp = dmu_objset_pool(os); 784 dsl_dataset_rele_flags(os->os_dsl_dataset, flags, tag); 785 dsl_pool_rele(dp, tag); 786 } 787 788 void 789 dmu_objset_rele(objset_t *os, void *tag) 790 { 791 dmu_objset_rele_flags(os, B_FALSE, tag); 792 } 793 794 /* 795 * When we are called, os MUST refer to an objset associated with a dataset 796 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner 797 * == tag. We will then release and reacquire ownership of the dataset while 798 * holding the pool config_rwlock to avoid intervening namespace or ownership 799 * changes may occur. 800 * 801 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to 802 * release the hold on its dataset and acquire a new one on the dataset of the 803 * same name so that it can be partially torn down and reconstructed. 804 */ 805 void 806 dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds, 807 boolean_t decrypt, void *tag) 808 { 809 dsl_pool_t *dp; 810 char name[ZFS_MAX_DATASET_NAME_LEN]; 811 812 VERIFY3P(ds, !=, NULL); 813 VERIFY3P(ds->ds_owner, ==, tag); 814 VERIFY(dsl_dataset_long_held(ds)); 815 816 dsl_dataset_name(ds, name); 817 dp = ds->ds_dir->dd_pool; 818 dsl_pool_config_enter(dp, FTAG); 819 820 dsl_dataset_disown(ds, 0, tag); 821 VERIFY0(dsl_dataset_own(dp, name, 822 (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0, tag, newds)); 823 dsl_pool_config_exit(dp, FTAG); 824 } 825 826 void 827 dmu_objset_disown(objset_t *os, boolean_t decrypt, void *tag) 828 { 829 dsl_dataset_disown(os->os_dsl_dataset, 830 (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0, tag); 831 } 832 833 void 834 dmu_objset_evict_dbufs(objset_t *os) 835 { 836 dnode_t dn_marker; 837 dnode_t *dn; 838 839 mutex_enter(&os->os_lock); 840 dn = list_head(&os->os_dnodes); 841 while (dn != NULL) { 842 /* 843 * Skip dnodes without holds. We have to do this dance 844 * because dnode_add_ref() only works if there is already a 845 * hold. If the dnode has no holds, then it has no dbufs. 846 */ 847 if (dnode_add_ref(dn, FTAG)) { 848 list_insert_after(&os->os_dnodes, dn, &dn_marker); 849 mutex_exit(&os->os_lock); 850 851 dnode_evict_dbufs(dn); 852 dnode_rele(dn, FTAG); 853 854 mutex_enter(&os->os_lock); 855 dn = list_next(&os->os_dnodes, &dn_marker); 856 list_remove(&os->os_dnodes, &dn_marker); 857 } else { 858 dn = list_next(&os->os_dnodes, dn); 859 } 860 } 861 mutex_exit(&os->os_lock); 862 863 if (DMU_USERUSED_DNODE(os) != NULL) { 864 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os)); 865 dnode_evict_dbufs(DMU_USERUSED_DNODE(os)); 866 } 867 dnode_evict_dbufs(DMU_META_DNODE(os)); 868 } 869 870 /* 871 * Objset eviction processing is split into into two pieces. 872 * The first marks the objset as evicting, evicts any dbufs that 873 * have a refcount of zero, and then queues up the objset for the 874 * second phase of eviction. Once os->os_dnodes has been cleared by 875 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed. 876 * The second phase closes the special dnodes, dequeues the objset from 877 * the list of those undergoing eviction, and finally frees the objset. 878 * 879 * NOTE: Due to asynchronous eviction processing (invocation of 880 * dnode_buf_pageout()), it is possible for the meta dnode for the 881 * objset to have no holds even though os->os_dnodes is not empty. 882 */ 883 void 884 dmu_objset_evict(objset_t *os) 885 { 886 dsl_dataset_t *ds = os->os_dsl_dataset; 887 888 for (int t = 0; t < TXG_SIZE; t++) 889 ASSERT(!dmu_objset_is_dirty(os, t)); 890 891 if (ds) 892 dsl_prop_unregister_all(ds, os); 893 894 if (os->os_sa) 895 sa_tear_down(os); 896 897 dmu_objset_evict_dbufs(os); 898 899 mutex_enter(&os->os_lock); 900 spa_evicting_os_register(os->os_spa, os); 901 if (list_is_empty(&os->os_dnodes)) { 902 mutex_exit(&os->os_lock); 903 dmu_objset_evict_done(os); 904 } else { 905 mutex_exit(&os->os_lock); 906 } 907 908 909 } 910 911 void 912 dmu_objset_evict_done(objset_t *os) 913 { 914 ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 915 916 dnode_special_close(&os->os_meta_dnode); 917 if (DMU_USERUSED_DNODE(os)) { 918 dnode_special_close(&os->os_userused_dnode); 919 dnode_special_close(&os->os_groupused_dnode); 920 } 921 zil_free(os->os_zil); 922 923 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 924 925 /* 926 * This is a barrier to prevent the objset from going away in 927 * dnode_move() until we can safely ensure that the objset is still in 928 * use. We consider the objset valid before the barrier and invalid 929 * after the barrier. 930 */ 931 rw_enter(&os_lock, RW_READER); 932 rw_exit(&os_lock); 933 934 kmem_free(os->os_obj_next_percpu, 935 os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0])); 936 937 mutex_destroy(&os->os_lock); 938 mutex_destroy(&os->os_userused_lock); 939 mutex_destroy(&os->os_obj_lock); 940 mutex_destroy(&os->os_user_ptr_lock); 941 for (int i = 0; i < TXG_SIZE; i++) { 942 multilist_destroy(os->os_dirty_dnodes[i]); 943 } 944 spa_evicting_os_deregister(os->os_spa, os); 945 kmem_free(os, sizeof (objset_t)); 946 } 947 948 timestruc_t 949 dmu_objset_snap_cmtime(objset_t *os) 950 { 951 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 952 } 953 954 /* ARGSUSED */ 955 objset_t * 956 dmu_objset_create_impl_dnstats(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 957 dmu_objset_type_t type, int levels, int blksz, int ibs, dmu_tx_t *tx) 958 { 959 objset_t *os; 960 dnode_t *mdn; 961 962 ASSERT(dmu_tx_is_syncing(tx)); 963 964 if (blksz == 0) 965 blksz = 1 << DNODE_BLOCK_SHIFT; 966 if (ibs == 0) 967 ibs = DN_MAX_INDBLKSHIFT; 968 969 if (ds != NULL) 970 VERIFY0(dmu_objset_from_ds(ds, &os)); 971 else 972 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os)); 973 974 mdn = DMU_META_DNODE(os); 975 976 dnode_allocate(mdn, DMU_OT_DNODE, DNODE_BLOCK_SIZE, DN_MAX_INDBLKSHIFT, 977 DMU_OT_NONE, 0, DNODE_MIN_SLOTS, tx); 978 979 /* 980 * We don't want to have to increase the meta-dnode's nlevels 981 * later, because then we could do it in quescing context while 982 * we are also accessing it in open context. 983 * 984 * This precaution is not necessary for the MOS (ds == NULL), 985 * because the MOS is only updated in syncing context. 986 * This is most fortunate: the MOS is the only objset that 987 * needs to be synced multiple times as spa_sync() iterates 988 * to convergence, so minimizing its dn_nlevels matters. 989 */ 990 if (ds != NULL) { 991 if (levels == 0) { 992 levels = 1; 993 994 /* 995 * Determine the number of levels necessary for the 996 * meta-dnode to contain DN_MAX_OBJECT dnodes. Note 997 * that in order to ensure that we do not overflow 998 * 64 bits, there has to be a nlevels that gives us a 999 * number of blocks > DN_MAX_OBJECT but < 2^64. 1000 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) 1001 * (10) must be less than (64 - log2(DN_MAX_OBJECT)) 1002 * (16). 1003 */ 1004 while ((uint64_t)mdn->dn_nblkptr << 1005 (mdn->dn_datablkshift - DNODE_SHIFT + (levels - 1) * 1006 (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 1007 DN_MAX_OBJECT) 1008 levels++; 1009 } 1010 1011 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 1012 mdn->dn_nlevels = levels; 1013 } 1014 1015 ASSERT(type != DMU_OST_NONE); 1016 ASSERT(type != DMU_OST_ANY); 1017 ASSERT(type < DMU_OST_NUMTYPES); 1018 os->os_phys->os_type = type; 1019 1020 /* 1021 * Enable user accounting if it is enabled and this is not an 1022 * encrypted receive. 1023 */ 1024 if (dmu_objset_userused_enabled(os) && 1025 (!os->os_encrypted || !dmu_objset_is_receiving(os))) { 1026 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 1027 os->os_flags = os->os_phys->os_flags; 1028 } 1029 1030 dsl_dataset_dirty(ds, tx); 1031 1032 return (os); 1033 } 1034 1035 /* called from dsl for meta-objset */ 1036 objset_t * 1037 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 1038 dmu_objset_type_t type, dmu_tx_t *tx) 1039 { 1040 return (dmu_objset_create_impl_dnstats(spa, ds, bp, type, 0, 0, 0, tx)); 1041 } 1042 1043 typedef struct dmu_objset_create_arg { 1044 const char *doca_name; 1045 cred_t *doca_cred; 1046 void (*doca_userfunc)(objset_t *os, void *arg, 1047 cred_t *cr, dmu_tx_t *tx); 1048 void *doca_userarg; 1049 dmu_objset_type_t doca_type; 1050 uint64_t doca_flags; 1051 dsl_crypto_params_t *doca_dcp; 1052 } dmu_objset_create_arg_t; 1053 1054 /*ARGSUSED*/ 1055 static int 1056 dmu_objset_create_check(void *arg, dmu_tx_t *tx) 1057 { 1058 dmu_objset_create_arg_t *doca = arg; 1059 dsl_pool_t *dp = dmu_tx_pool(tx); 1060 dsl_dir_t *pdd; 1061 const char *tail; 1062 int error; 1063 1064 if (strchr(doca->doca_name, '@') != NULL) 1065 return (SET_ERROR(EINVAL)); 1066 1067 if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN) 1068 return (SET_ERROR(ENAMETOOLONG)); 1069 1070 if (dataset_nestcheck(doca->doca_name) != 0) 1071 return (SET_ERROR(ENAMETOOLONG)); 1072 1073 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail); 1074 if (error != 0) 1075 return (error); 1076 if (tail == NULL) { 1077 dsl_dir_rele(pdd, FTAG); 1078 return (SET_ERROR(EEXIST)); 1079 } 1080 1081 error = dmu_objset_create_crypt_check(pdd, doca->doca_dcp, NULL); 1082 if (error != 0) { 1083 dsl_dir_rele(pdd, FTAG); 1084 return (error); 1085 } 1086 1087 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, 1088 doca->doca_cred); 1089 1090 dsl_dir_rele(pdd, FTAG); 1091 1092 return (error); 1093 } 1094 1095 static void 1096 dmu_objset_create_sync(void *arg, dmu_tx_t *tx) 1097 { 1098 dmu_objset_create_arg_t *doca = arg; 1099 dsl_pool_t *dp = dmu_tx_pool(tx); 1100 spa_t *spa = dp->dp_spa; 1101 dsl_dir_t *pdd; 1102 const char *tail; 1103 dsl_dataset_t *ds; 1104 uint64_t obj; 1105 blkptr_t *bp; 1106 objset_t *os; 1107 zio_t *rzio; 1108 1109 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail)); 1110 1111 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags, 1112 doca->doca_cred, doca->doca_dcp, tx); 1113 1114 VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj, 1115 DS_HOLD_FLAG_DECRYPT, FTAG, &ds)); 1116 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1117 bp = dsl_dataset_get_blkptr(ds); 1118 os = dmu_objset_create_impl(spa, ds, bp, doca->doca_type, tx); 1119 rrw_exit(&ds->ds_bp_rwlock, FTAG); 1120 1121 if (doca->doca_userfunc != NULL) { 1122 doca->doca_userfunc(os, doca->doca_userarg, 1123 doca->doca_cred, tx); 1124 } 1125 1126 /* 1127 * The doca_userfunc() may write out some data that needs to be 1128 * encrypted if the dataset is encrypted (specifically the root 1129 * directory). This data must be written out before the encryption 1130 * key mapping is removed by dsl_dataset_rele_flags(). Force the 1131 * I/O to occur immediately by invoking the relevant sections of 1132 * dsl_pool_sync(). 1133 */ 1134 if (os->os_encrypted) { 1135 dsl_dataset_t *tmpds = NULL; 1136 boolean_t need_sync_done = B_FALSE; 1137 1138 mutex_enter(&ds->ds_lock); 1139 ds->ds_owner = FTAG; 1140 mutex_exit(&ds->ds_lock); 1141 1142 rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1143 tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds, 1144 tx->tx_txg); 1145 if (tmpds != NULL) { 1146 dsl_dataset_sync(ds, rzio, tx); 1147 need_sync_done = B_TRUE; 1148 } 1149 VERIFY0(zio_wait(rzio)); 1150 dmu_objset_do_userquota_updates(os, tx); 1151 taskq_wait(dp->dp_sync_taskq); 1152 if (txg_list_member(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 1153 ASSERT3P(ds->ds_key_mapping, !=, NULL); 1154 key_mapping_rele(spa, ds->ds_key_mapping, ds); 1155 } 1156 1157 rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1158 tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds, 1159 tx->tx_txg); 1160 if (tmpds != NULL) { 1161 dmu_buf_rele(ds->ds_dbuf, ds); 1162 dsl_dataset_sync(ds, rzio, tx); 1163 } 1164 VERIFY0(zio_wait(rzio)); 1165 1166 if (need_sync_done) { 1167 ASSERT3P(ds->ds_key_mapping, !=, NULL); 1168 key_mapping_rele(spa, ds->ds_key_mapping, ds); 1169 dsl_dataset_sync_done(ds, tx); 1170 } 1171 1172 mutex_enter(&ds->ds_lock); 1173 ds->ds_owner = NULL; 1174 mutex_exit(&ds->ds_lock); 1175 } 1176 1177 spa_history_log_internal_ds(ds, "create", tx, ""); 1178 dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG); 1179 dsl_dir_rele(pdd, FTAG); 1180 } 1181 1182 int 1183 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 1184 dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg) 1185 { 1186 dmu_objset_create_arg_t doca; 1187 dsl_crypto_params_t tmp_dcp = { 0 }; 1188 1189 doca.doca_name = name; 1190 doca.doca_cred = CRED(); 1191 doca.doca_flags = flags; 1192 doca.doca_userfunc = func; 1193 doca.doca_userarg = arg; 1194 doca.doca_type = type; 1195 1196 /* 1197 * Some callers (mostly for testing) do not provide a dcp on their 1198 * own but various code inside the sync task will require it to be 1199 * allocated. Rather than adding NULL checks throughout this code 1200 * or adding dummy dcp's to all of the callers we simply create a 1201 * dummy one here and use that. This zero dcp will have the same 1202 * effect as asking for inheritence of all encryption params. 1203 */ 1204 doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp; 1205 1206 return (dsl_sync_task(name, 1207 dmu_objset_create_check, dmu_objset_create_sync, &doca, 1208 6, ZFS_SPACE_CHECK_NORMAL)); 1209 } 1210 1211 typedef struct dmu_objset_clone_arg { 1212 const char *doca_clone; 1213 const char *doca_origin; 1214 cred_t *doca_cred; 1215 } dmu_objset_clone_arg_t; 1216 1217 /*ARGSUSED*/ 1218 static int 1219 dmu_objset_clone_check(void *arg, dmu_tx_t *tx) 1220 { 1221 dmu_objset_clone_arg_t *doca = arg; 1222 dsl_dir_t *pdd; 1223 const char *tail; 1224 int error; 1225 dsl_dataset_t *origin; 1226 dsl_pool_t *dp = dmu_tx_pool(tx); 1227 1228 if (strchr(doca->doca_clone, '@') != NULL) 1229 return (SET_ERROR(EINVAL)); 1230 1231 if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN) 1232 return (SET_ERROR(ENAMETOOLONG)); 1233 1234 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail); 1235 if (error != 0) 1236 return (error); 1237 if (tail == NULL) { 1238 dsl_dir_rele(pdd, FTAG); 1239 return (SET_ERROR(EEXIST)); 1240 } 1241 1242 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, 1243 doca->doca_cred); 1244 if (error != 0) { 1245 dsl_dir_rele(pdd, FTAG); 1246 return (SET_ERROR(EDQUOT)); 1247 } 1248 1249 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin); 1250 if (error != 0) { 1251 dsl_dir_rele(pdd, FTAG); 1252 return (error); 1253 } 1254 1255 /* You can only clone snapshots, not the head datasets. */ 1256 if (!origin->ds_is_snapshot) { 1257 dsl_dataset_rele(origin, FTAG); 1258 dsl_dir_rele(pdd, FTAG); 1259 return (SET_ERROR(EINVAL)); 1260 } 1261 1262 error = dmu_objset_clone_crypt_check(pdd, origin->ds_dir); 1263 if (error != 0) { 1264 dsl_dataset_rele(origin, FTAG); 1265 dsl_dir_rele(pdd, FTAG); 1266 return (error); 1267 } 1268 1269 dsl_dataset_rele(origin, FTAG); 1270 dsl_dir_rele(pdd, FTAG); 1271 1272 return (0); 1273 } 1274 1275 static void 1276 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx) 1277 { 1278 dmu_objset_clone_arg_t *doca = arg; 1279 dsl_pool_t *dp = dmu_tx_pool(tx); 1280 dsl_dir_t *pdd; 1281 const char *tail; 1282 dsl_dataset_t *origin, *ds; 1283 uint64_t obj; 1284 char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 1285 1286 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail)); 1287 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin)); 1288 1289 obj = dsl_dataset_create_sync(pdd, tail, origin, 0, 1290 doca->doca_cred, NULL, tx); 1291 1292 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); 1293 dsl_dataset_name(origin, namebuf); 1294 spa_history_log_internal_ds(ds, "clone", tx, 1295 "origin=%s (%llu)", namebuf, origin->ds_object); 1296 dsl_dataset_rele(ds, FTAG); 1297 dsl_dataset_rele(origin, FTAG); 1298 dsl_dir_rele(pdd, FTAG); 1299 } 1300 1301 int 1302 dmu_objset_clone(const char *clone, const char *origin) 1303 { 1304 dmu_objset_clone_arg_t doca; 1305 1306 doca.doca_clone = clone; 1307 doca.doca_origin = origin; 1308 doca.doca_cred = CRED(); 1309 1310 return (dsl_sync_task(clone, 1311 dmu_objset_clone_check, dmu_objset_clone_sync, &doca, 1312 6, ZFS_SPACE_CHECK_NORMAL)); 1313 } 1314 1315 static int 1316 dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg) 1317 { 1318 int error = 0; 1319 uint64_t object = 0; 1320 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 1321 error = dmu_object_remap_indirects(os, object, 1322 last_removed_txg); 1323 /* 1324 * If the ZPL removed the object before we managed to dnode_hold 1325 * it, we would get an ENOENT. If the ZPL declares its intent 1326 * to remove the object (dnode_free) before we manage to 1327 * dnode_hold it, we would get an EEXIST. In either case, we 1328 * want to continue remapping the other objects in the objset; 1329 * in all other cases, we want to break early. 1330 */ 1331 if (error != 0 && error != ENOENT && error != EEXIST) { 1332 break; 1333 } 1334 } 1335 if (error == ESRCH) { 1336 error = 0; 1337 } 1338 return (error); 1339 } 1340 1341 int 1342 dmu_objset_remap_indirects(const char *fsname) 1343 { 1344 int error = 0; 1345 objset_t *os = NULL; 1346 uint64_t last_removed_txg; 1347 uint64_t remap_start_txg; 1348 dsl_dir_t *dd; 1349 1350 error = dmu_objset_hold(fsname, FTAG, &os); 1351 if (error != 0) { 1352 return (error); 1353 } 1354 dd = dmu_objset_ds(os)->ds_dir; 1355 1356 if (!spa_feature_is_enabled(dmu_objset_spa(os), 1357 SPA_FEATURE_OBSOLETE_COUNTS)) { 1358 dmu_objset_rele(os, FTAG); 1359 return (SET_ERROR(ENOTSUP)); 1360 } 1361 1362 if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) { 1363 dmu_objset_rele(os, FTAG); 1364 return (SET_ERROR(EINVAL)); 1365 } 1366 1367 /* 1368 * If there has not been a removal, we're done. 1369 */ 1370 last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os)); 1371 if (last_removed_txg == -1ULL) { 1372 dmu_objset_rele(os, FTAG); 1373 return (0); 1374 } 1375 1376 /* 1377 * If we have remapped since the last removal, we're done. 1378 */ 1379 if (dsl_dir_is_zapified(dd)) { 1380 uint64_t last_remap_txg; 1381 if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)), 1382 dd->dd_object, DD_FIELD_LAST_REMAP_TXG, 1383 sizeof (last_remap_txg), 1, &last_remap_txg) == 0 && 1384 last_remap_txg > last_removed_txg) { 1385 dmu_objset_rele(os, FTAG); 1386 return (0); 1387 } 1388 } 1389 1390 dsl_dataset_long_hold(dmu_objset_ds(os), FTAG); 1391 dsl_pool_rele(dmu_objset_pool(os), FTAG); 1392 1393 remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os)); 1394 error = dmu_objset_remap_indirects_impl(os, last_removed_txg); 1395 if (error == 0) { 1396 /* 1397 * We update the last_remap_txg to be the start txg so that 1398 * we can guarantee that every block older than last_remap_txg 1399 * that can be remapped has been remapped. 1400 */ 1401 error = dsl_dir_update_last_remap_txg(dd, remap_start_txg); 1402 } 1403 1404 dsl_dataset_long_rele(dmu_objset_ds(os), FTAG); 1405 dsl_dataset_rele(dmu_objset_ds(os), FTAG); 1406 1407 return (error); 1408 } 1409 1410 int 1411 dmu_objset_snapshot_one(const char *fsname, const char *snapname) 1412 { 1413 int err; 1414 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname); 1415 nvlist_t *snaps = fnvlist_alloc(); 1416 1417 fnvlist_add_boolean(snaps, longsnap); 1418 strfree(longsnap); 1419 err = dsl_dataset_snapshot(snaps, NULL, NULL); 1420 fnvlist_free(snaps); 1421 return (err); 1422 } 1423 1424 static void 1425 dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx) 1426 { 1427 dnode_t *dn; 1428 1429 while ((dn = multilist_sublist_head(list)) != NULL) { 1430 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 1431 ASSERT(dn->dn_dbuf->db_data_pending); 1432 /* 1433 * Initialize dn_zio outside dnode_sync() because the 1434 * meta-dnode needs to set it ouside dnode_sync(). 1435 */ 1436 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 1437 ASSERT(dn->dn_zio); 1438 1439 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 1440 multilist_sublist_remove(list, dn); 1441 1442 /* 1443 * If we are not doing useraccounting (os_synced_dnodes == NULL) 1444 * we are done with this dnode for this txg. Unset dn_dirty_txg 1445 * if later txgs aren't dirtying it so that future holders do 1446 * not get a stale value. Otherwise, we will do this in 1447 * userquota_updates_task() when processing has completely 1448 * finished for this txg. 1449 */ 1450 multilist_t *newlist = dn->dn_objset->os_synced_dnodes; 1451 if (newlist != NULL) { 1452 (void) dnode_add_ref(dn, newlist); 1453 multilist_insert(newlist, dn); 1454 } else { 1455 mutex_enter(&dn->dn_mtx); 1456 if (dn->dn_dirty_txg == tx->tx_txg) 1457 dn->dn_dirty_txg = 0; 1458 mutex_exit(&dn->dn_mtx); 1459 } 1460 1461 dnode_sync(dn, tx); 1462 } 1463 } 1464 1465 /* ARGSUSED */ 1466 static void 1467 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 1468 { 1469 blkptr_t *bp = zio->io_bp; 1470 objset_t *os = arg; 1471 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 1472 uint64_t fill = 0; 1473 1474 ASSERT(!BP_IS_EMBEDDED(bp)); 1475 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET); 1476 1477 /* 1478 * Update rootbp fill count: it should be the number of objects 1479 * allocated in the object set (not counting the "special" 1480 * objects that are stored in the objset_phys_t -- the meta 1481 * dnode and user/group accounting objects). 1482 */ 1483 for (int i = 0; i < dnp->dn_nblkptr; i++) 1484 fill += BP_GET_FILL(&dnp->dn_blkptr[i]); 1485 1486 BP_SET_FILL(bp, fill); 1487 1488 if (os->os_dsl_dataset != NULL) 1489 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG); 1490 *os->os_rootbp = *bp; 1491 if (os->os_dsl_dataset != NULL) 1492 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG); 1493 } 1494 1495 /* ARGSUSED */ 1496 static void 1497 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 1498 { 1499 blkptr_t *bp = zio->io_bp; 1500 blkptr_t *bp_orig = &zio->io_bp_orig; 1501 objset_t *os = arg; 1502 1503 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 1504 ASSERT(BP_EQUAL(bp, bp_orig)); 1505 } else { 1506 dsl_dataset_t *ds = os->os_dsl_dataset; 1507 dmu_tx_t *tx = os->os_synctx; 1508 1509 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 1510 dsl_dataset_block_born(ds, bp, tx); 1511 } 1512 kmem_free(bp, sizeof (*bp)); 1513 } 1514 1515 typedef struct sync_dnodes_arg { 1516 multilist_t *sda_list; 1517 int sda_sublist_idx; 1518 multilist_t *sda_newlist; 1519 dmu_tx_t *sda_tx; 1520 } sync_dnodes_arg_t; 1521 1522 static void 1523 sync_dnodes_task(void *arg) 1524 { 1525 sync_dnodes_arg_t *sda = arg; 1526 1527 multilist_sublist_t *ms = 1528 multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx); 1529 1530 dmu_objset_sync_dnodes(ms, sda->sda_tx); 1531 1532 multilist_sublist_unlock(ms); 1533 1534 kmem_free(sda, sizeof (*sda)); 1535 } 1536 1537 1538 /* called from dsl */ 1539 void 1540 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 1541 { 1542 int txgoff; 1543 zbookmark_phys_t zb; 1544 zio_prop_t zp; 1545 zio_t *zio; 1546 list_t *list; 1547 dbuf_dirty_record_t *dr; 1548 blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP); 1549 *blkptr_copy = *os->os_rootbp; 1550 1551 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1552 1553 ASSERT(dmu_tx_is_syncing(tx)); 1554 /* XXX the write_done callback should really give us the tx... */ 1555 os->os_synctx = tx; 1556 1557 if (os->os_dsl_dataset == NULL) { 1558 /* 1559 * This is the MOS. If we have upgraded, 1560 * spa_max_replication() could change, so reset 1561 * os_copies here. 1562 */ 1563 os->os_copies = spa_max_replication(os->os_spa); 1564 } 1565 1566 /* 1567 * Create the root block IO 1568 */ 1569 SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1570 os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1571 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 1572 arc_release(os->os_phys_buf, &os->os_phys_buf); 1573 1574 dmu_write_policy(os, NULL, 0, 0, &zp); 1575 1576 /* 1577 * If we are either claiming the ZIL or doing a raw receive, write 1578 * out the os_phys_buf raw. Neither of these actions will effect the 1579 * MAC at this point. 1580 */ 1581 if (os->os_raw_receive || 1582 os->os_next_write_raw[tx->tx_txg & TXG_MASK]) { 1583 ASSERT(os->os_encrypted); 1584 arc_convert_to_raw(os->os_phys_buf, 1585 os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER, 1586 DMU_OT_OBJSET, NULL, NULL, NULL); 1587 } 1588 1589 zio = arc_write(pio, os->os_spa, tx->tx_txg, 1590 blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), 1591 &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done, 1592 os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1593 1594 /* 1595 * Sync special dnodes - the parent IO for the sync is the root block 1596 */ 1597 DMU_META_DNODE(os)->dn_zio = zio; 1598 dnode_sync(DMU_META_DNODE(os), tx); 1599 1600 os->os_phys->os_flags = os->os_flags; 1601 1602 if (DMU_USERUSED_DNODE(os) && 1603 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1604 DMU_USERUSED_DNODE(os)->dn_zio = zio; 1605 dnode_sync(DMU_USERUSED_DNODE(os), tx); 1606 DMU_GROUPUSED_DNODE(os)->dn_zio = zio; 1607 dnode_sync(DMU_GROUPUSED_DNODE(os), tx); 1608 } 1609 1610 txgoff = tx->tx_txg & TXG_MASK; 1611 1612 if (dmu_objset_userused_enabled(os) && 1613 (!os->os_encrypted || !dmu_objset_is_receiving(os))) { 1614 /* 1615 * We must create the list here because it uses the 1616 * dn_dirty_link[] of this txg. But it may already 1617 * exist because we call dsl_dataset_sync() twice per txg. 1618 */ 1619 if (os->os_synced_dnodes == NULL) { 1620 os->os_synced_dnodes = 1621 multilist_create(sizeof (dnode_t), 1622 offsetof(dnode_t, dn_dirty_link[txgoff]), 1623 dnode_multilist_index_func); 1624 } else { 1625 ASSERT3U(os->os_synced_dnodes->ml_offset, ==, 1626 offsetof(dnode_t, dn_dirty_link[txgoff])); 1627 } 1628 } 1629 1630 for (int i = 0; 1631 i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) { 1632 sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP); 1633 sda->sda_list = os->os_dirty_dnodes[txgoff]; 1634 sda->sda_sublist_idx = i; 1635 sda->sda_tx = tx; 1636 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq, 1637 sync_dnodes_task, sda, 0); 1638 /* callback frees sda */ 1639 } 1640 taskq_wait(dmu_objset_pool(os)->dp_sync_taskq); 1641 1642 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; 1643 while ((dr = list_head(list)) != NULL) { 1644 ASSERT0(dr->dr_dbuf->db_level); 1645 list_remove(list, dr); 1646 if (dr->dr_zio) 1647 zio_nowait(dr->dr_zio); 1648 } 1649 1650 /* Enable dnode backfill if enough objects have been freed. */ 1651 if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) { 1652 os->os_rescan_dnodes = B_TRUE; 1653 os->os_freed_dnodes = 0; 1654 } 1655 1656 /* 1657 * Free intent log blocks up to this tx. 1658 */ 1659 zil_sync(os->os_zil, tx); 1660 os->os_phys->os_zil_header = os->os_zil_header; 1661 zio_nowait(zio); 1662 } 1663 1664 boolean_t 1665 dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1666 { 1667 return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK])); 1668 } 1669 1670 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 1671 1672 void 1673 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 1674 { 1675 used_cbs[ost] = cb; 1676 } 1677 1678 boolean_t 1679 dmu_objset_userused_enabled(objset_t *os) 1680 { 1681 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 1682 used_cbs[os->os_phys->os_type] != NULL && 1683 DMU_USERUSED_DNODE(os) != NULL); 1684 } 1685 1686 typedef struct userquota_node { 1687 uint64_t uqn_id; 1688 int64_t uqn_delta; 1689 avl_node_t uqn_node; 1690 } userquota_node_t; 1691 1692 typedef struct userquota_cache { 1693 avl_tree_t uqc_user_deltas; 1694 avl_tree_t uqc_group_deltas; 1695 } userquota_cache_t; 1696 1697 static int 1698 userquota_compare(const void *l, const void *r) 1699 { 1700 const userquota_node_t *luqn = l; 1701 const userquota_node_t *ruqn = r; 1702 1703 if (luqn->uqn_id < ruqn->uqn_id) 1704 return (-1); 1705 if (luqn->uqn_id > ruqn->uqn_id) 1706 return (1); 1707 return (0); 1708 } 1709 1710 static void 1711 do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx) 1712 { 1713 void *cookie; 1714 userquota_node_t *uqn; 1715 1716 ASSERT(dmu_tx_is_syncing(tx)); 1717 1718 cookie = NULL; 1719 while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas, 1720 &cookie)) != NULL) { 1721 /* 1722 * os_userused_lock protects against concurrent calls to 1723 * zap_increment_int(). It's needed because zap_increment_int() 1724 * is not thread-safe (i.e. not atomic). 1725 */ 1726 mutex_enter(&os->os_userused_lock); 1727 VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT, 1728 uqn->uqn_id, uqn->uqn_delta, tx)); 1729 mutex_exit(&os->os_userused_lock); 1730 kmem_free(uqn, sizeof (*uqn)); 1731 } 1732 avl_destroy(&cache->uqc_user_deltas); 1733 1734 cookie = NULL; 1735 while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas, 1736 &cookie)) != NULL) { 1737 mutex_enter(&os->os_userused_lock); 1738 VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT, 1739 uqn->uqn_id, uqn->uqn_delta, tx)); 1740 mutex_exit(&os->os_userused_lock); 1741 kmem_free(uqn, sizeof (*uqn)); 1742 } 1743 avl_destroy(&cache->uqc_group_deltas); 1744 } 1745 1746 static void 1747 userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta) 1748 { 1749 userquota_node_t search = { .uqn_id = id }; 1750 avl_index_t idx; 1751 1752 userquota_node_t *uqn = avl_find(avl, &search, &idx); 1753 if (uqn == NULL) { 1754 uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP); 1755 uqn->uqn_id = id; 1756 avl_insert(avl, uqn, idx); 1757 } 1758 uqn->uqn_delta += delta; 1759 } 1760 1761 static void 1762 do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags, 1763 uint64_t user, uint64_t group, boolean_t subtract) 1764 { 1765 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 1766 int64_t delta = DNODE_MIN_SIZE + used; 1767 if (subtract) 1768 delta = -delta; 1769 1770 userquota_update_cache(&cache->uqc_user_deltas, user, delta); 1771 userquota_update_cache(&cache->uqc_group_deltas, group, delta); 1772 } 1773 } 1774 1775 typedef struct userquota_updates_arg { 1776 objset_t *uua_os; 1777 int uua_sublist_idx; 1778 dmu_tx_t *uua_tx; 1779 } userquota_updates_arg_t; 1780 1781 static void 1782 userquota_updates_task(void *arg) 1783 { 1784 userquota_updates_arg_t *uua = arg; 1785 objset_t *os = uua->uua_os; 1786 dmu_tx_t *tx = uua->uua_tx; 1787 dnode_t *dn; 1788 userquota_cache_t cache = { 0 }; 1789 1790 multilist_sublist_t *list = 1791 multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx); 1792 1793 ASSERT(multilist_sublist_head(list) == NULL || 1794 dmu_objset_userused_enabled(os)); 1795 avl_create(&cache.uqc_user_deltas, userquota_compare, 1796 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node)); 1797 avl_create(&cache.uqc_group_deltas, userquota_compare, 1798 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node)); 1799 1800 while ((dn = multilist_sublist_head(list)) != NULL) { 1801 int flags; 1802 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 1803 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 1804 dn->dn_phys->dn_flags & 1805 DNODE_FLAG_USERUSED_ACCOUNTED); 1806 1807 flags = dn->dn_id_flags; 1808 ASSERT(flags); 1809 if (flags & DN_ID_OLD_EXIST) { 1810 do_userquota_update(&cache, 1811 dn->dn_oldused, dn->dn_oldflags, 1812 dn->dn_olduid, dn->dn_oldgid, B_TRUE); 1813 } 1814 if (flags & DN_ID_NEW_EXIST) { 1815 do_userquota_update(&cache, 1816 DN_USED_BYTES(dn->dn_phys), 1817 dn->dn_phys->dn_flags, dn->dn_newuid, 1818 dn->dn_newgid, B_FALSE); 1819 } 1820 1821 mutex_enter(&dn->dn_mtx); 1822 dn->dn_oldused = 0; 1823 dn->dn_oldflags = 0; 1824 if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 1825 dn->dn_olduid = dn->dn_newuid; 1826 dn->dn_oldgid = dn->dn_newgid; 1827 dn->dn_id_flags |= DN_ID_OLD_EXIST; 1828 if (dn->dn_bonuslen == 0) 1829 dn->dn_id_flags |= DN_ID_CHKED_SPILL; 1830 else 1831 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 1832 } 1833 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); 1834 if (dn->dn_dirty_txg == spa_syncing_txg(os->os_spa)) 1835 dn->dn_dirty_txg = 0; 1836 mutex_exit(&dn->dn_mtx); 1837 1838 multilist_sublist_remove(list, dn); 1839 dnode_rele(dn, os->os_synced_dnodes); 1840 } 1841 do_userquota_cacheflush(os, &cache, tx); 1842 multilist_sublist_unlock(list); 1843 kmem_free(uua, sizeof (*uua)); 1844 } 1845 1846 void 1847 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 1848 { 1849 if (!dmu_objset_userused_enabled(os)) 1850 return; 1851 1852 /* if this is a raw receive just return and handle accounting later */ 1853 if (os->os_encrypted && dmu_objset_is_receiving(os)) 1854 return; 1855 1856 /* Allocate the user/groupused objects if necessary. */ 1857 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 1858 VERIFY0(zap_create_claim(os, 1859 DMU_USERUSED_OBJECT, 1860 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1861 VERIFY0(zap_create_claim(os, 1862 DMU_GROUPUSED_OBJECT, 1863 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1864 } 1865 1866 for (int i = 0; 1867 i < multilist_get_num_sublists(os->os_synced_dnodes); i++) { 1868 userquota_updates_arg_t *uua = 1869 kmem_alloc(sizeof (*uua), KM_SLEEP); 1870 uua->uua_os = os; 1871 uua->uua_sublist_idx = i; 1872 uua->uua_tx = tx; 1873 /* note: caller does taskq_wait() */ 1874 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq, 1875 userquota_updates_task, uua, 0); 1876 /* callback frees uua */ 1877 } 1878 } 1879 1880 /* 1881 * Returns a pointer to data to find uid/gid from 1882 * 1883 * If a dirty record for transaction group that is syncing can't 1884 * be found then NULL is returned. In the NULL case it is assumed 1885 * the uid/gid aren't changing. 1886 */ 1887 static void * 1888 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 1889 { 1890 dbuf_dirty_record_t *dr, **drp; 1891 void *data; 1892 1893 if (db->db_dirtycnt == 0) 1894 return (db->db.db_data); /* Nothing is changing */ 1895 1896 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 1897 if (dr->dr_txg == tx->tx_txg) 1898 break; 1899 1900 if (dr == NULL) { 1901 data = NULL; 1902 } else { 1903 dnode_t *dn; 1904 1905 DB_DNODE_ENTER(dr->dr_dbuf); 1906 dn = DB_DNODE(dr->dr_dbuf); 1907 1908 if (dn->dn_bonuslen == 0 && 1909 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 1910 data = dr->dt.dl.dr_data->b_data; 1911 else 1912 data = dr->dt.dl.dr_data; 1913 1914 DB_DNODE_EXIT(dr->dr_dbuf); 1915 } 1916 1917 return (data); 1918 } 1919 1920 void 1921 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 1922 { 1923 objset_t *os = dn->dn_objset; 1924 void *data = NULL; 1925 dmu_buf_impl_t *db = NULL; 1926 uint64_t *user = NULL; 1927 uint64_t *group = NULL; 1928 int flags = dn->dn_id_flags; 1929 int error; 1930 boolean_t have_spill = B_FALSE; 1931 1932 if (!dmu_objset_userused_enabled(dn->dn_objset)) 1933 return; 1934 1935 /* 1936 * Raw receives introduce a problem with user accounting. Raw 1937 * receives cannot update the user accounting info because the 1938 * user ids and the sizes are encrypted. To guarantee that we 1939 * never end up with bad user accounting, we simply disable it 1940 * during raw receives. We also disable this for normal receives 1941 * so that an incremental raw receive may be done on top of an 1942 * existing non-raw receive. 1943 */ 1944 if (os->os_encrypted && dmu_objset_is_receiving(os)) 1945 return; 1946 1947 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 1948 DN_ID_CHKED_SPILL))) 1949 return; 1950 1951 if (before && dn->dn_bonuslen != 0) 1952 data = DN_BONUS(dn->dn_phys); 1953 else if (!before && dn->dn_bonuslen != 0) { 1954 if (dn->dn_bonus) { 1955 db = dn->dn_bonus; 1956 mutex_enter(&db->db_mtx); 1957 data = dmu_objset_userquota_find_data(db, tx); 1958 } else { 1959 data = DN_BONUS(dn->dn_phys); 1960 } 1961 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 1962 int rf = 0; 1963 1964 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 1965 rf |= DB_RF_HAVESTRUCT; 1966 error = dmu_spill_hold_by_dnode(dn, 1967 rf | DB_RF_MUST_SUCCEED, 1968 FTAG, (dmu_buf_t **)&db); 1969 ASSERT(error == 0); 1970 mutex_enter(&db->db_mtx); 1971 data = (before) ? db->db.db_data : 1972 dmu_objset_userquota_find_data(db, tx); 1973 have_spill = B_TRUE; 1974 } else { 1975 mutex_enter(&dn->dn_mtx); 1976 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 1977 mutex_exit(&dn->dn_mtx); 1978 return; 1979 } 1980 1981 if (before) { 1982 ASSERT(data); 1983 user = &dn->dn_olduid; 1984 group = &dn->dn_oldgid; 1985 } else if (data) { 1986 user = &dn->dn_newuid; 1987 group = &dn->dn_newgid; 1988 } 1989 1990 /* 1991 * Must always call the callback in case the object 1992 * type has changed and that type isn't an object type to track 1993 */ 1994 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 1995 user, group); 1996 1997 /* 1998 * Preserve existing uid/gid when the callback can't determine 1999 * what the new uid/gid are and the callback returned EEXIST. 2000 * The EEXIST error tells us to just use the existing uid/gid. 2001 * If we don't know what the old values are then just assign 2002 * them to 0, since that is a new file being created. 2003 */ 2004 if (!before && data == NULL && error == EEXIST) { 2005 if (flags & DN_ID_OLD_EXIST) { 2006 dn->dn_newuid = dn->dn_olduid; 2007 dn->dn_newgid = dn->dn_oldgid; 2008 } else { 2009 dn->dn_newuid = 0; 2010 dn->dn_newgid = 0; 2011 } 2012 error = 0; 2013 } 2014 2015 if (db) 2016 mutex_exit(&db->db_mtx); 2017 2018 mutex_enter(&dn->dn_mtx); 2019 if (error == 0 && before) 2020 dn->dn_id_flags |= DN_ID_OLD_EXIST; 2021 if (error == 0 && !before) 2022 dn->dn_id_flags |= DN_ID_NEW_EXIST; 2023 2024 if (have_spill) { 2025 dn->dn_id_flags |= DN_ID_CHKED_SPILL; 2026 } else { 2027 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 2028 } 2029 mutex_exit(&dn->dn_mtx); 2030 if (have_spill) 2031 dmu_buf_rele((dmu_buf_t *)db, FTAG); 2032 } 2033 2034 boolean_t 2035 dmu_objset_userspace_present(objset_t *os) 2036 { 2037 return (os->os_phys->os_flags & 2038 OBJSET_FLAG_USERACCOUNTING_COMPLETE); 2039 } 2040 2041 int 2042 dmu_objset_userspace_upgrade(objset_t *os) 2043 { 2044 uint64_t obj; 2045 int err = 0; 2046 2047 if (dmu_objset_userspace_present(os)) 2048 return (0); 2049 if (!dmu_objset_userused_enabled(os)) 2050 return (SET_ERROR(ENOTSUP)); 2051 if (dmu_objset_is_snapshot(os)) 2052 return (SET_ERROR(EINVAL)); 2053 2054 /* 2055 * We simply need to mark every object dirty, so that it will be 2056 * synced out and now accounted. If this is called 2057 * concurrently, or if we already did some work before crashing, 2058 * that's fine, since we track each object's accounted state 2059 * independently. 2060 */ 2061 2062 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 2063 dmu_tx_t *tx; 2064 dmu_buf_t *db; 2065 int objerr; 2066 2067 if (issig(JUSTLOOKING) && issig(FORREAL)) 2068 return (SET_ERROR(EINTR)); 2069 2070 objerr = dmu_bonus_hold(os, obj, FTAG, &db); 2071 if (objerr != 0) 2072 continue; 2073 tx = dmu_tx_create(os); 2074 dmu_tx_hold_bonus(tx, obj); 2075 objerr = dmu_tx_assign(tx, TXG_WAIT); 2076 if (objerr != 0) { 2077 dmu_tx_abort(tx); 2078 continue; 2079 } 2080 dmu_buf_will_dirty(db, tx); 2081 dmu_buf_rele(db, FTAG); 2082 dmu_tx_commit(tx); 2083 } 2084 2085 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 2086 txg_wait_synced(dmu_objset_pool(os), 0); 2087 return (0); 2088 } 2089 2090 void 2091 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 2092 uint64_t *usedobjsp, uint64_t *availobjsp) 2093 { 2094 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 2095 usedobjsp, availobjsp); 2096 } 2097 2098 uint64_t 2099 dmu_objset_fsid_guid(objset_t *os) 2100 { 2101 return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 2102 } 2103 2104 void 2105 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 2106 { 2107 stat->dds_type = os->os_phys->os_type; 2108 if (os->os_dsl_dataset) 2109 dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 2110 } 2111 2112 void 2113 dmu_objset_stats(objset_t *os, nvlist_t *nv) 2114 { 2115 ASSERT(os->os_dsl_dataset || 2116 os->os_phys->os_type == DMU_OST_META); 2117 2118 if (os->os_dsl_dataset != NULL) 2119 dsl_dataset_stats(os->os_dsl_dataset, nv); 2120 2121 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 2122 os->os_phys->os_type); 2123 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 2124 dmu_objset_userspace_present(os)); 2125 } 2126 2127 int 2128 dmu_objset_is_snapshot(objset_t *os) 2129 { 2130 if (os->os_dsl_dataset != NULL) 2131 return (os->os_dsl_dataset->ds_is_snapshot); 2132 else 2133 return (B_FALSE); 2134 } 2135 2136 int 2137 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 2138 boolean_t *conflict) 2139 { 2140 dsl_dataset_t *ds = os->os_dsl_dataset; 2141 uint64_t ignored; 2142 2143 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) 2144 return (SET_ERROR(ENOENT)); 2145 2146 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 2147 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored, 2148 MT_NORMALIZE, real, maxlen, conflict)); 2149 } 2150 2151 int 2152 dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 2153 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 2154 { 2155 dsl_dataset_t *ds = os->os_dsl_dataset; 2156 zap_cursor_t cursor; 2157 zap_attribute_t attr; 2158 2159 ASSERT(dsl_pool_config_held(dmu_objset_pool(os))); 2160 2161 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) 2162 return (SET_ERROR(ENOENT)); 2163 2164 zap_cursor_init_serialized(&cursor, 2165 ds->ds_dir->dd_pool->dp_meta_objset, 2166 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp); 2167 2168 if (zap_cursor_retrieve(&cursor, &attr) != 0) { 2169 zap_cursor_fini(&cursor); 2170 return (SET_ERROR(ENOENT)); 2171 } 2172 2173 if (strlen(attr.za_name) + 1 > namelen) { 2174 zap_cursor_fini(&cursor); 2175 return (SET_ERROR(ENAMETOOLONG)); 2176 } 2177 2178 (void) strcpy(name, attr.za_name); 2179 if (idp) 2180 *idp = attr.za_first_integer; 2181 if (case_conflict) 2182 *case_conflict = attr.za_normalization_conflict; 2183 zap_cursor_advance(&cursor); 2184 *offp = zap_cursor_serialize(&cursor); 2185 zap_cursor_fini(&cursor); 2186 2187 return (0); 2188 } 2189 2190 int 2191 dmu_dir_list_next(objset_t *os, int namelen, char *name, 2192 uint64_t *idp, uint64_t *offp) 2193 { 2194 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 2195 zap_cursor_t cursor; 2196 zap_attribute_t attr; 2197 2198 /* there is no next dir on a snapshot! */ 2199 if (os->os_dsl_dataset->ds_object != 2200 dsl_dir_phys(dd)->dd_head_dataset_obj) 2201 return (SET_ERROR(ENOENT)); 2202 2203 zap_cursor_init_serialized(&cursor, 2204 dd->dd_pool->dp_meta_objset, 2205 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp); 2206 2207 if (zap_cursor_retrieve(&cursor, &attr) != 0) { 2208 zap_cursor_fini(&cursor); 2209 return (SET_ERROR(ENOENT)); 2210 } 2211 2212 if (strlen(attr.za_name) + 1 > namelen) { 2213 zap_cursor_fini(&cursor); 2214 return (SET_ERROR(ENAMETOOLONG)); 2215 } 2216 2217 (void) strcpy(name, attr.za_name); 2218 if (idp) 2219 *idp = attr.za_first_integer; 2220 zap_cursor_advance(&cursor); 2221 *offp = zap_cursor_serialize(&cursor); 2222 zap_cursor_fini(&cursor); 2223 2224 return (0); 2225 } 2226 2227 typedef struct dmu_objset_find_ctx { 2228 taskq_t *dc_tq; 2229 dsl_pool_t *dc_dp; 2230 uint64_t dc_ddobj; 2231 char *dc_ddname; /* last component of ddobj's name */ 2232 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *); 2233 void *dc_arg; 2234 int dc_flags; 2235 kmutex_t *dc_error_lock; 2236 int *dc_error; 2237 } dmu_objset_find_ctx_t; 2238 2239 static void 2240 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp) 2241 { 2242 dsl_pool_t *dp = dcp->dc_dp; 2243 dsl_dir_t *dd; 2244 dsl_dataset_t *ds; 2245 zap_cursor_t zc; 2246 zap_attribute_t *attr; 2247 uint64_t thisobj; 2248 int err = 0; 2249 2250 /* don't process if there already was an error */ 2251 if (*dcp->dc_error != 0) 2252 goto out; 2253 2254 /* 2255 * Note: passing the name (dc_ddname) here is optional, but it 2256 * improves performance because we don't need to call 2257 * zap_value_search() to determine the name. 2258 */ 2259 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd); 2260 if (err != 0) 2261 goto out; 2262 2263 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 2264 if (dd->dd_myname[0] == '$') { 2265 dsl_dir_rele(dd, FTAG); 2266 goto out; 2267 } 2268 2269 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; 2270 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 2271 2272 /* 2273 * Iterate over all children. 2274 */ 2275 if (dcp->dc_flags & DS_FIND_CHILDREN) { 2276 for (zap_cursor_init(&zc, dp->dp_meta_objset, 2277 dsl_dir_phys(dd)->dd_child_dir_zapobj); 2278 zap_cursor_retrieve(&zc, attr) == 0; 2279 (void) zap_cursor_advance(&zc)) { 2280 ASSERT3U(attr->za_integer_length, ==, 2281 sizeof (uint64_t)); 2282 ASSERT3U(attr->za_num_integers, ==, 1); 2283 2284 dmu_objset_find_ctx_t *child_dcp = 2285 kmem_alloc(sizeof (*child_dcp), KM_SLEEP); 2286 *child_dcp = *dcp; 2287 child_dcp->dc_ddobj = attr->za_first_integer; 2288 child_dcp->dc_ddname = spa_strdup(attr->za_name); 2289 if (dcp->dc_tq != NULL) 2290 (void) taskq_dispatch(dcp->dc_tq, 2291 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP); 2292 else 2293 dmu_objset_find_dp_impl(child_dcp); 2294 } 2295 zap_cursor_fini(&zc); 2296 } 2297 2298 /* 2299 * Iterate over all snapshots. 2300 */ 2301 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) { 2302 dsl_dataset_t *ds; 2303 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 2304 2305 if (err == 0) { 2306 uint64_t snapobj; 2307 2308 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 2309 dsl_dataset_rele(ds, FTAG); 2310 2311 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 2312 zap_cursor_retrieve(&zc, attr) == 0; 2313 (void) zap_cursor_advance(&zc)) { 2314 ASSERT3U(attr->za_integer_length, ==, 2315 sizeof (uint64_t)); 2316 ASSERT3U(attr->za_num_integers, ==, 1); 2317 2318 err = dsl_dataset_hold_obj(dp, 2319 attr->za_first_integer, FTAG, &ds); 2320 if (err != 0) 2321 break; 2322 err = dcp->dc_func(dp, ds, dcp->dc_arg); 2323 dsl_dataset_rele(ds, FTAG); 2324 if (err != 0) 2325 break; 2326 } 2327 zap_cursor_fini(&zc); 2328 } 2329 } 2330 2331 kmem_free(attr, sizeof (zap_attribute_t)); 2332 2333 if (err != 0) { 2334 dsl_dir_rele(dd, FTAG); 2335 goto out; 2336 } 2337 2338 /* 2339 * Apply to self. 2340 */ 2341 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 2342 2343 /* 2344 * Note: we hold the dir while calling dsl_dataset_hold_obj() so 2345 * that the dir will remain cached, and we won't have to re-instantiate 2346 * it (which could be expensive due to finding its name via 2347 * zap_value_search()). 2348 */ 2349 dsl_dir_rele(dd, FTAG); 2350 if (err != 0) 2351 goto out; 2352 err = dcp->dc_func(dp, ds, dcp->dc_arg); 2353 dsl_dataset_rele(ds, FTAG); 2354 2355 out: 2356 if (err != 0) { 2357 mutex_enter(dcp->dc_error_lock); 2358 /* only keep first error */ 2359 if (*dcp->dc_error == 0) 2360 *dcp->dc_error = err; 2361 mutex_exit(dcp->dc_error_lock); 2362 } 2363 2364 if (dcp->dc_ddname != NULL) 2365 spa_strfree(dcp->dc_ddname); 2366 kmem_free(dcp, sizeof (*dcp)); 2367 } 2368 2369 static void 2370 dmu_objset_find_dp_cb(void *arg) 2371 { 2372 dmu_objset_find_ctx_t *dcp = arg; 2373 dsl_pool_t *dp = dcp->dc_dp; 2374 2375 /* 2376 * We need to get a pool_config_lock here, as there are several 2377 * asssert(pool_config_held) down the stack. Getting a lock via 2378 * dsl_pool_config_enter is risky, as it might be stalled by a 2379 * pending writer. This would deadlock, as the write lock can 2380 * only be granted when our parent thread gives up the lock. 2381 * The _prio interface gives us priority over a pending writer. 2382 */ 2383 dsl_pool_config_enter_prio(dp, FTAG); 2384 2385 dmu_objset_find_dp_impl(dcp); 2386 2387 dsl_pool_config_exit(dp, FTAG); 2388 } 2389 2390 /* 2391 * Find objsets under and including ddobj, call func(ds) on each. 2392 * The order for the enumeration is completely undefined. 2393 * func is called with dsl_pool_config held. 2394 */ 2395 int 2396 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj, 2397 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags) 2398 { 2399 int error = 0; 2400 taskq_t *tq = NULL; 2401 int ntasks; 2402 dmu_objset_find_ctx_t *dcp; 2403 kmutex_t err_lock; 2404 2405 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL); 2406 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP); 2407 dcp->dc_tq = NULL; 2408 dcp->dc_dp = dp; 2409 dcp->dc_ddobj = ddobj; 2410 dcp->dc_ddname = NULL; 2411 dcp->dc_func = func; 2412 dcp->dc_arg = arg; 2413 dcp->dc_flags = flags; 2414 dcp->dc_error_lock = &err_lock; 2415 dcp->dc_error = &error; 2416 2417 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) { 2418 /* 2419 * In case a write lock is held we can't make use of 2420 * parallelism, as down the stack of the worker threads 2421 * the lock is asserted via dsl_pool_config_held. 2422 * In case of a read lock this is solved by getting a read 2423 * lock in each worker thread, which isn't possible in case 2424 * of a writer lock. So we fall back to the synchronous path 2425 * here. 2426 * In the future it might be possible to get some magic into 2427 * dsl_pool_config_held in a way that it returns true for 2428 * the worker threads so that a single lock held from this 2429 * thread suffices. For now, stay single threaded. 2430 */ 2431 dmu_objset_find_dp_impl(dcp); 2432 mutex_destroy(&err_lock); 2433 2434 return (error); 2435 } 2436 2437 ntasks = dmu_find_threads; 2438 if (ntasks == 0) 2439 ntasks = vdev_count_leaves(dp->dp_spa) * 4; 2440 tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks, 2441 INT_MAX, 0); 2442 if (tq == NULL) { 2443 kmem_free(dcp, sizeof (*dcp)); 2444 mutex_destroy(&err_lock); 2445 2446 return (SET_ERROR(ENOMEM)); 2447 } 2448 dcp->dc_tq = tq; 2449 2450 /* dcp will be freed by task */ 2451 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP); 2452 2453 /* 2454 * PORTING: this code relies on the property of taskq_wait to wait 2455 * until no more tasks are queued and no more tasks are active. As 2456 * we always queue new tasks from within other tasks, task_wait 2457 * reliably waits for the full recursion to finish, even though we 2458 * enqueue new tasks after taskq_wait has been called. 2459 * On platforms other than illumos, taskq_wait may not have this 2460 * property. 2461 */ 2462 taskq_wait(tq); 2463 taskq_destroy(tq); 2464 mutex_destroy(&err_lock); 2465 2466 return (error); 2467 } 2468 2469 /* 2470 * Find all objsets under name, and for each, call 'func(child_name, arg)'. 2471 * The dp_config_rwlock must not be held when this is called, and it 2472 * will not be held when the callback is called. 2473 * Therefore this function should only be used when the pool is not changing 2474 * (e.g. in syncing context), or the callback can deal with the possible races. 2475 */ 2476 static int 2477 dmu_objset_find_impl(spa_t *spa, const char *name, 2478 int func(const char *, void *), void *arg, int flags) 2479 { 2480 dsl_dir_t *dd; 2481 dsl_pool_t *dp = spa_get_dsl(spa); 2482 dsl_dataset_t *ds; 2483 zap_cursor_t zc; 2484 zap_attribute_t *attr; 2485 char *child; 2486 uint64_t thisobj; 2487 int err; 2488 2489 dsl_pool_config_enter(dp, FTAG); 2490 2491 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL); 2492 if (err != 0) { 2493 dsl_pool_config_exit(dp, FTAG); 2494 return (err); 2495 } 2496 2497 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 2498 if (dd->dd_myname[0] == '$') { 2499 dsl_dir_rele(dd, FTAG); 2500 dsl_pool_config_exit(dp, FTAG); 2501 return (0); 2502 } 2503 2504 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; 2505 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 2506 2507 /* 2508 * Iterate over all children. 2509 */ 2510 if (flags & DS_FIND_CHILDREN) { 2511 for (zap_cursor_init(&zc, dp->dp_meta_objset, 2512 dsl_dir_phys(dd)->dd_child_dir_zapobj); 2513 zap_cursor_retrieve(&zc, attr) == 0; 2514 (void) zap_cursor_advance(&zc)) { 2515 ASSERT3U(attr->za_integer_length, ==, 2516 sizeof (uint64_t)); 2517 ASSERT3U(attr->za_num_integers, ==, 1); 2518 2519 child = kmem_asprintf("%s/%s", name, attr->za_name); 2520 dsl_pool_config_exit(dp, FTAG); 2521 err = dmu_objset_find_impl(spa, child, 2522 func, arg, flags); 2523 dsl_pool_config_enter(dp, FTAG); 2524 strfree(child); 2525 if (err != 0) 2526 break; 2527 } 2528 zap_cursor_fini(&zc); 2529 2530 if (err != 0) { 2531 dsl_dir_rele(dd, FTAG); 2532 dsl_pool_config_exit(dp, FTAG); 2533 kmem_free(attr, sizeof (zap_attribute_t)); 2534 return (err); 2535 } 2536 } 2537 2538 /* 2539 * Iterate over all snapshots. 2540 */ 2541 if (flags & DS_FIND_SNAPSHOTS) { 2542 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 2543 2544 if (err == 0) { 2545 uint64_t snapobj; 2546 2547 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 2548 dsl_dataset_rele(ds, FTAG); 2549 2550 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 2551 zap_cursor_retrieve(&zc, attr) == 0; 2552 (void) zap_cursor_advance(&zc)) { 2553 ASSERT3U(attr->za_integer_length, ==, 2554 sizeof (uint64_t)); 2555 ASSERT3U(attr->za_num_integers, ==, 1); 2556 2557 child = kmem_asprintf("%s@%s", 2558 name, attr->za_name); 2559 dsl_pool_config_exit(dp, FTAG); 2560 err = func(child, arg); 2561 dsl_pool_config_enter(dp, FTAG); 2562 strfree(child); 2563 if (err != 0) 2564 break; 2565 } 2566 zap_cursor_fini(&zc); 2567 } 2568 } 2569 2570 dsl_dir_rele(dd, FTAG); 2571 kmem_free(attr, sizeof (zap_attribute_t)); 2572 dsl_pool_config_exit(dp, FTAG); 2573 2574 if (err != 0) 2575 return (err); 2576 2577 /* Apply to self. */ 2578 return (func(name, arg)); 2579 } 2580 2581 /* 2582 * See comment above dmu_objset_find_impl(). 2583 */ 2584 int 2585 dmu_objset_find(char *name, int func(const char *, void *), void *arg, 2586 int flags) 2587 { 2588 spa_t *spa; 2589 int error; 2590 2591 error = spa_open(name, &spa, FTAG); 2592 if (error != 0) 2593 return (error); 2594 error = dmu_objset_find_impl(spa, name, func, arg, flags); 2595 spa_close(spa, FTAG); 2596 return (error); 2597 } 2598 2599 boolean_t 2600 dmu_objset_incompatible_encryption_version(objset_t *os) 2601 { 2602 return (dsl_dir_incompatible_encryption_version( 2603 os->os_dsl_dataset->ds_dir)); 2604 } 2605 2606 void 2607 dmu_objset_set_user(objset_t *os, void *user_ptr) 2608 { 2609 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 2610 os->os_user_ptr = user_ptr; 2611 } 2612 2613 void * 2614 dmu_objset_get_user(objset_t *os) 2615 { 2616 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 2617 return (os->os_user_ptr); 2618 } 2619 2620 /* 2621 * Determine name of filesystem, given name of snapshot. 2622 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes 2623 */ 2624 int 2625 dmu_fsname(const char *snapname, char *buf) 2626 { 2627 char *atp = strchr(snapname, '@'); 2628 if (atp == NULL) 2629 return (SET_ERROR(EINVAL)); 2630 if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN) 2631 return (SET_ERROR(ENAMETOOLONG)); 2632 (void) strlcpy(buf, snapname, atp - snapname + 1); 2633 return (0); 2634 } 2635 2636 /* 2637 * Call when we think we're going to write/free space in open context to track 2638 * the amount of dirty data in the open txg, which is also the amount 2639 * of memory that can not be evicted until this txg syncs. 2640 */ 2641 void 2642 dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx) 2643 { 2644 dsl_dataset_t *ds = os->os_dsl_dataset; 2645 int64_t aspace = spa_get_worst_case_asize(os->os_spa, space); 2646 2647 if (ds != NULL) { 2648 dsl_dir_willuse_space(ds->ds_dir, aspace, tx); 2649 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx); 2650 } 2651 } 2652