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