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