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