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