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