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