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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/dmu_objset.h> 29 #include <sys/dsl_dataset.h> 30 #include <sys/dsl_dir.h> 31 #include <sys/dsl_prop.h> 32 #include <sys/dsl_synctask.h> 33 #include <sys/dmu_traverse.h> 34 #include <sys/dmu_tx.h> 35 #include <sys/arc.h> 36 #include <sys/zio.h> 37 #include <sys/zap.h> 38 #include <sys/unique.h> 39 #include <sys/zfs_context.h> 40 #include <sys/zfs_ioctl.h> 41 #include <sys/spa.h> 42 #include <sys/sunddi.h> 43 44 static dsl_checkfunc_t dsl_dataset_destroy_begin_check; 45 static dsl_syncfunc_t dsl_dataset_destroy_begin_sync; 46 static dsl_checkfunc_t dsl_dataset_rollback_check; 47 static dsl_syncfunc_t dsl_dataset_rollback_sync; 48 static dsl_checkfunc_t dsl_dataset_destroy_check; 49 static dsl_syncfunc_t dsl_dataset_destroy_sync; 50 51 #define DS_REF_MAX (1ULL << 62) 52 53 #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 54 55 /* 56 * We use weighted reference counts to express the various forms of exclusion 57 * between different open modes. A STANDARD open is 1 point, an EXCLUSIVE open 58 * is DS_REF_MAX, and a PRIMARY open is little more than half of an EXCLUSIVE. 59 * This makes the exclusion logic simple: the total refcnt for all opens cannot 60 * exceed DS_REF_MAX. For example, EXCLUSIVE opens are exclusive because their 61 * weight (DS_REF_MAX) consumes the entire refcnt space. PRIMARY opens consume 62 * just over half of the refcnt space, so there can't be more than one, but it 63 * can peacefully coexist with any number of STANDARD opens. 64 */ 65 static uint64_t ds_refcnt_weight[DS_MODE_LEVELS] = { 66 0, /* DS_MODE_NONE - invalid */ 67 1, /* DS_MODE_STANDARD - unlimited number */ 68 (DS_REF_MAX >> 1) + 1, /* DS_MODE_PRIMARY - only one of these */ 69 DS_REF_MAX /* DS_MODE_EXCLUSIVE - no other opens */ 70 }; 71 72 73 void 74 dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 75 { 76 int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 77 int compressed = BP_GET_PSIZE(bp); 78 int uncompressed = BP_GET_UCSIZE(bp); 79 80 dprintf_bp(bp, "born, ds=%p\n", ds); 81 82 ASSERT(dmu_tx_is_syncing(tx)); 83 /* It could have been compressed away to nothing */ 84 if (BP_IS_HOLE(bp)) 85 return; 86 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 87 ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES); 88 if (ds == NULL) { 89 /* 90 * Account for the meta-objset space in its placeholder 91 * dsl_dir. 92 */ 93 ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */ 94 dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, 95 used, compressed, uncompressed, tx); 96 dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 97 return; 98 } 99 dmu_buf_will_dirty(ds->ds_dbuf, tx); 100 mutex_enter(&ds->ds_lock); 101 ds->ds_phys->ds_used_bytes += used; 102 ds->ds_phys->ds_compressed_bytes += compressed; 103 ds->ds_phys->ds_uncompressed_bytes += uncompressed; 104 ds->ds_phys->ds_unique_bytes += used; 105 mutex_exit(&ds->ds_lock); 106 dsl_dir_diduse_space(ds->ds_dir, 107 used, compressed, uncompressed, tx); 108 } 109 110 void 111 dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio, 112 dmu_tx_t *tx) 113 { 114 int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 115 int compressed = BP_GET_PSIZE(bp); 116 int uncompressed = BP_GET_UCSIZE(bp); 117 118 ASSERT(dmu_tx_is_syncing(tx)); 119 /* No block pointer => nothing to free */ 120 if (BP_IS_HOLE(bp)) 121 return; 122 123 ASSERT(used > 0); 124 if (ds == NULL) { 125 int err; 126 /* 127 * Account for the meta-objset space in its placeholder 128 * dataset. 129 */ 130 err = arc_free(pio, tx->tx_pool->dp_spa, 131 tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT); 132 ASSERT(err == 0); 133 134 dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, 135 -used, -compressed, -uncompressed, tx); 136 dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 137 return; 138 } 139 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 140 141 dmu_buf_will_dirty(ds->ds_dbuf, tx); 142 143 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 144 int err; 145 146 dprintf_bp(bp, "freeing: %s", ""); 147 err = arc_free(pio, tx->tx_pool->dp_spa, 148 tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT); 149 ASSERT(err == 0); 150 151 mutex_enter(&ds->ds_lock); 152 /* XXX unique_bytes is not accurate for head datasets */ 153 /* ASSERT3U(ds->ds_phys->ds_unique_bytes, >=, used); */ 154 ds->ds_phys->ds_unique_bytes -= used; 155 mutex_exit(&ds->ds_lock); 156 dsl_dir_diduse_space(ds->ds_dir, 157 -used, -compressed, -uncompressed, tx); 158 } else { 159 dprintf_bp(bp, "putting on dead list: %s", ""); 160 VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx)); 161 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 162 if (ds->ds_phys->ds_prev_snap_obj != 0) { 163 ASSERT3U(ds->ds_prev->ds_object, ==, 164 ds->ds_phys->ds_prev_snap_obj); 165 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 166 if (ds->ds_prev->ds_phys->ds_next_snap_obj == 167 ds->ds_object && bp->blk_birth > 168 ds->ds_prev->ds_phys->ds_prev_snap_txg) { 169 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 170 mutex_enter(&ds->ds_prev->ds_lock); 171 ds->ds_prev->ds_phys->ds_unique_bytes += 172 used; 173 mutex_exit(&ds->ds_prev->ds_lock); 174 } 175 } 176 } 177 mutex_enter(&ds->ds_lock); 178 ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 179 ds->ds_phys->ds_used_bytes -= used; 180 ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 181 ds->ds_phys->ds_compressed_bytes -= compressed; 182 ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 183 ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 184 mutex_exit(&ds->ds_lock); 185 } 186 187 uint64_t 188 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 189 { 190 uint64_t trysnap = 0; 191 192 if (ds == NULL) 193 return (0); 194 /* 195 * The snapshot creation could fail, but that would cause an 196 * incorrect FALSE return, which would only result in an 197 * overestimation of the amount of space that an operation would 198 * consume, which is OK. 199 * 200 * There's also a small window where we could miss a pending 201 * snapshot, because we could set the sync task in the quiescing 202 * phase. So this should only be used as a guess. 203 */ 204 if (ds->ds_trysnap_txg > 205 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 206 trysnap = ds->ds_trysnap_txg; 207 return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 208 } 209 210 int 211 dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth) 212 { 213 return (blk_birth > dsl_dataset_prev_snap_txg(ds)); 214 } 215 216 /* ARGSUSED */ 217 static void 218 dsl_dataset_evict(dmu_buf_t *db, void *dsv) 219 { 220 dsl_dataset_t *ds = dsv; 221 dsl_pool_t *dp = ds->ds_dir->dd_pool; 222 223 /* open_refcount == DS_REF_MAX when deleting */ 224 ASSERT(ds->ds_open_refcount == 0 || 225 ds->ds_open_refcount == DS_REF_MAX); 226 227 dprintf_ds(ds, "evicting %s\n", ""); 228 229 unique_remove(ds->ds_phys->ds_fsid_guid); 230 231 if (ds->ds_user_ptr != NULL) 232 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 233 234 if (ds->ds_prev) { 235 dsl_dataset_close(ds->ds_prev, DS_MODE_NONE, ds); 236 ds->ds_prev = NULL; 237 } 238 239 bplist_close(&ds->ds_deadlist); 240 dsl_dir_close(ds->ds_dir, ds); 241 242 if (list_link_active(&ds->ds_synced_link)) 243 list_remove(&dp->dp_synced_objsets, ds); 244 245 mutex_destroy(&ds->ds_lock); 246 mutex_destroy(&ds->ds_deadlist.bpl_lock); 247 248 kmem_free(ds, sizeof (dsl_dataset_t)); 249 } 250 251 static int 252 dsl_dataset_get_snapname(dsl_dataset_t *ds) 253 { 254 dsl_dataset_phys_t *headphys; 255 int err; 256 dmu_buf_t *headdbuf; 257 dsl_pool_t *dp = ds->ds_dir->dd_pool; 258 objset_t *mos = dp->dp_meta_objset; 259 260 if (ds->ds_snapname[0]) 261 return (0); 262 if (ds->ds_phys->ds_next_snap_obj == 0) 263 return (0); 264 265 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 266 FTAG, &headdbuf); 267 if (err) 268 return (err); 269 headphys = headdbuf->db_data; 270 err = zap_value_search(dp->dp_meta_objset, 271 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 272 dmu_buf_rele(headdbuf, FTAG); 273 return (err); 274 } 275 276 int 277 dsl_dataset_open_obj(dsl_pool_t *dp, uint64_t dsobj, const char *snapname, 278 int mode, void *tag, dsl_dataset_t **dsp) 279 { 280 uint64_t weight = ds_refcnt_weight[DS_MODE_LEVEL(mode)]; 281 objset_t *mos = dp->dp_meta_objset; 282 dmu_buf_t *dbuf; 283 dsl_dataset_t *ds; 284 int err; 285 286 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 287 dsl_pool_sync_context(dp)); 288 289 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 290 if (err) 291 return (err); 292 ds = dmu_buf_get_user(dbuf); 293 if (ds == NULL) { 294 dsl_dataset_t *winner; 295 296 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 297 ds->ds_dbuf = dbuf; 298 ds->ds_object = dsobj; 299 ds->ds_phys = dbuf->db_data; 300 301 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 302 mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT, 303 NULL); 304 305 err = bplist_open(&ds->ds_deadlist, 306 mos, ds->ds_phys->ds_deadlist_obj); 307 if (err == 0) { 308 err = dsl_dir_open_obj(dp, 309 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 310 } 311 if (err) { 312 /* 313 * we don't really need to close the blist if we 314 * just opened it. 315 */ 316 mutex_destroy(&ds->ds_lock); 317 mutex_destroy(&ds->ds_deadlist.bpl_lock); 318 kmem_free(ds, sizeof (dsl_dataset_t)); 319 dmu_buf_rele(dbuf, tag); 320 return (err); 321 } 322 323 if (ds->ds_dir->dd_phys->dd_head_dataset_obj == dsobj) { 324 ds->ds_snapname[0] = '\0'; 325 if (ds->ds_phys->ds_prev_snap_obj) { 326 err = dsl_dataset_open_obj(dp, 327 ds->ds_phys->ds_prev_snap_obj, NULL, 328 DS_MODE_NONE, ds, &ds->ds_prev); 329 } 330 } else { 331 if (snapname) { 332 #ifdef ZFS_DEBUG 333 dsl_dataset_phys_t *headphys; 334 dmu_buf_t *headdbuf; 335 err = dmu_bonus_hold(mos, 336 ds->ds_dir->dd_phys->dd_head_dataset_obj, 337 FTAG, &headdbuf); 338 if (err == 0) { 339 headphys = headdbuf->db_data; 340 uint64_t foundobj; 341 err = zap_lookup(dp->dp_meta_objset, 342 headphys->ds_snapnames_zapobj, 343 snapname, sizeof (foundobj), 1, 344 &foundobj); 345 ASSERT3U(foundobj, ==, dsobj); 346 dmu_buf_rele(headdbuf, FTAG); 347 } 348 #endif 349 (void) strcat(ds->ds_snapname, snapname); 350 } else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) { 351 err = dsl_dataset_get_snapname(ds); 352 } 353 } 354 355 if (err == 0) { 356 winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 357 dsl_dataset_evict); 358 } 359 if (err || winner) { 360 bplist_close(&ds->ds_deadlist); 361 if (ds->ds_prev) { 362 dsl_dataset_close(ds->ds_prev, 363 DS_MODE_NONE, ds); 364 } 365 dsl_dir_close(ds->ds_dir, ds); 366 mutex_destroy(&ds->ds_lock); 367 mutex_destroy(&ds->ds_deadlist.bpl_lock); 368 kmem_free(ds, sizeof (dsl_dataset_t)); 369 if (err) { 370 dmu_buf_rele(dbuf, tag); 371 return (err); 372 } 373 ds = winner; 374 } else { 375 uint64_t new = 376 unique_insert(ds->ds_phys->ds_fsid_guid); 377 if (new != ds->ds_phys->ds_fsid_guid) { 378 /* XXX it won't necessarily be synced... */ 379 ds->ds_phys->ds_fsid_guid = new; 380 } 381 } 382 } 383 ASSERT3P(ds->ds_dbuf, ==, dbuf); 384 ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 385 386 mutex_enter(&ds->ds_lock); 387 if ((DS_MODE_LEVEL(mode) == DS_MODE_PRIMARY && 388 (ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) && 389 !DS_MODE_IS_INCONSISTENT(mode)) || 390 (ds->ds_open_refcount + weight > DS_REF_MAX)) { 391 mutex_exit(&ds->ds_lock); 392 dsl_dataset_close(ds, DS_MODE_NONE, tag); 393 return (EBUSY); 394 } 395 ds->ds_open_refcount += weight; 396 mutex_exit(&ds->ds_lock); 397 398 *dsp = ds; 399 return (0); 400 } 401 402 int 403 dsl_dataset_open_spa(spa_t *spa, const char *name, int mode, 404 void *tag, dsl_dataset_t **dsp) 405 { 406 dsl_dir_t *dd; 407 dsl_pool_t *dp; 408 const char *tail; 409 uint64_t obj; 410 dsl_dataset_t *ds = NULL; 411 int err = 0; 412 413 err = dsl_dir_open_spa(spa, name, FTAG, &dd, &tail); 414 if (err) 415 return (err); 416 417 dp = dd->dd_pool; 418 obj = dd->dd_phys->dd_head_dataset_obj; 419 rw_enter(&dp->dp_config_rwlock, RW_READER); 420 if (obj == 0) { 421 /* A dataset with no associated objset */ 422 err = ENOENT; 423 goto out; 424 } 425 426 if (tail != NULL) { 427 objset_t *mos = dp->dp_meta_objset; 428 429 err = dsl_dataset_open_obj(dp, obj, NULL, 430 DS_MODE_NONE, tag, &ds); 431 if (err) 432 goto out; 433 obj = ds->ds_phys->ds_snapnames_zapobj; 434 dsl_dataset_close(ds, DS_MODE_NONE, tag); 435 ds = NULL; 436 437 if (tail[0] != '@') { 438 err = ENOENT; 439 goto out; 440 } 441 tail++; 442 443 /* Look for a snapshot */ 444 if (!DS_MODE_IS_READONLY(mode)) { 445 err = EROFS; 446 goto out; 447 } 448 dprintf("looking for snapshot '%s'\n", tail); 449 err = zap_lookup(mos, obj, tail, 8, 1, &obj); 450 if (err) 451 goto out; 452 } 453 err = dsl_dataset_open_obj(dp, obj, tail, mode, tag, &ds); 454 455 out: 456 rw_exit(&dp->dp_config_rwlock); 457 dsl_dir_close(dd, FTAG); 458 459 ASSERT3U((err == 0), ==, (ds != NULL)); 460 /* ASSERT(ds == NULL || strcmp(name, ds->ds_name) == 0); */ 461 462 *dsp = ds; 463 return (err); 464 } 465 466 int 467 dsl_dataset_open(const char *name, int mode, void *tag, dsl_dataset_t **dsp) 468 { 469 return (dsl_dataset_open_spa(NULL, name, mode, tag, dsp)); 470 } 471 472 void 473 dsl_dataset_name(dsl_dataset_t *ds, char *name) 474 { 475 if (ds == NULL) { 476 (void) strcpy(name, "mos"); 477 } else { 478 dsl_dir_name(ds->ds_dir, name); 479 VERIFY(0 == dsl_dataset_get_snapname(ds)); 480 if (ds->ds_snapname[0]) { 481 (void) strcat(name, "@"); 482 if (!MUTEX_HELD(&ds->ds_lock)) { 483 /* 484 * We use a "recursive" mutex so that we 485 * can call dprintf_ds() with ds_lock held. 486 */ 487 mutex_enter(&ds->ds_lock); 488 (void) strcat(name, ds->ds_snapname); 489 mutex_exit(&ds->ds_lock); 490 } else { 491 (void) strcat(name, ds->ds_snapname); 492 } 493 } 494 } 495 } 496 497 static int 498 dsl_dataset_namelen(dsl_dataset_t *ds) 499 { 500 int result; 501 502 if (ds == NULL) { 503 result = 3; /* "mos" */ 504 } else { 505 result = dsl_dir_namelen(ds->ds_dir); 506 VERIFY(0 == dsl_dataset_get_snapname(ds)); 507 if (ds->ds_snapname[0]) { 508 ++result; /* adding one for the @-sign */ 509 if (!MUTEX_HELD(&ds->ds_lock)) { 510 /* see dsl_datset_name */ 511 mutex_enter(&ds->ds_lock); 512 result += strlen(ds->ds_snapname); 513 mutex_exit(&ds->ds_lock); 514 } else { 515 result += strlen(ds->ds_snapname); 516 } 517 } 518 } 519 520 return (result); 521 } 522 523 void 524 dsl_dataset_close(dsl_dataset_t *ds, int mode, void *tag) 525 { 526 uint64_t weight = ds_refcnt_weight[DS_MODE_LEVEL(mode)]; 527 mutex_enter(&ds->ds_lock); 528 ASSERT3U(ds->ds_open_refcount, >=, weight); 529 ds->ds_open_refcount -= weight; 530 dprintf_ds(ds, "closing mode %u refcount now 0x%llx\n", 531 mode, ds->ds_open_refcount); 532 mutex_exit(&ds->ds_lock); 533 534 dmu_buf_rele(ds->ds_dbuf, tag); 535 } 536 537 void 538 dsl_dataset_create_root(dsl_pool_t *dp, uint64_t *ddobjp, dmu_tx_t *tx) 539 { 540 objset_t *mos = dp->dp_meta_objset; 541 dmu_buf_t *dbuf; 542 dsl_dataset_phys_t *dsphys; 543 dsl_dataset_t *ds; 544 uint64_t dsobj; 545 dsl_dir_t *dd; 546 547 dsl_dir_create_root(mos, ddobjp, tx); 548 VERIFY(0 == dsl_dir_open_obj(dp, *ddobjp, NULL, FTAG, &dd)); 549 550 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 551 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 552 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 553 dmu_buf_will_dirty(dbuf, tx); 554 dsphys = dbuf->db_data; 555 dsphys->ds_dir_obj = dd->dd_object; 556 dsphys->ds_fsid_guid = unique_create(); 557 unique_remove(dsphys->ds_fsid_guid); /* it isn't open yet */ 558 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 559 sizeof (dsphys->ds_guid)); 560 dsphys->ds_snapnames_zapobj = 561 zap_create(mos, DMU_OT_DSL_DS_SNAP_MAP, DMU_OT_NONE, 0, tx); 562 dsphys->ds_creation_time = gethrestime_sec(); 563 dsphys->ds_creation_txg = tx->tx_txg; 564 dsphys->ds_deadlist_obj = 565 bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 566 dmu_buf_rele(dbuf, FTAG); 567 568 dmu_buf_will_dirty(dd->dd_dbuf, tx); 569 dd->dd_phys->dd_head_dataset_obj = dsobj; 570 dsl_dir_close(dd, FTAG); 571 572 VERIFY(0 == 573 dsl_dataset_open_obj(dp, dsobj, NULL, DS_MODE_NONE, FTAG, &ds)); 574 (void) dmu_objset_create_impl(dp->dp_spa, ds, 575 &ds->ds_phys->ds_bp, DMU_OST_ZFS, tx); 576 dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 577 } 578 579 uint64_t 580 dsl_dataset_create_sync(dsl_dir_t *pdd, 581 const char *lastname, dsl_dataset_t *clone_parent, dmu_tx_t *tx) 582 { 583 dsl_pool_t *dp = pdd->dd_pool; 584 dmu_buf_t *dbuf; 585 dsl_dataset_phys_t *dsphys; 586 uint64_t dsobj, ddobj; 587 objset_t *mos = dp->dp_meta_objset; 588 dsl_dir_t *dd; 589 590 ASSERT(clone_parent == NULL || clone_parent->ds_dir->dd_pool == dp); 591 ASSERT(clone_parent == NULL || 592 clone_parent->ds_phys->ds_num_children > 0); 593 ASSERT(lastname[0] != '@'); 594 ASSERT(dmu_tx_is_syncing(tx)); 595 596 ddobj = dsl_dir_create_sync(pdd, lastname, tx); 597 VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 598 599 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 600 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 601 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 602 dmu_buf_will_dirty(dbuf, tx); 603 dsphys = dbuf->db_data; 604 dsphys->ds_dir_obj = dd->dd_object; 605 dsphys->ds_fsid_guid = unique_create(); 606 unique_remove(dsphys->ds_fsid_guid); /* it isn't open yet */ 607 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 608 sizeof (dsphys->ds_guid)); 609 dsphys->ds_snapnames_zapobj = 610 zap_create(mos, DMU_OT_DSL_DS_SNAP_MAP, DMU_OT_NONE, 0, tx); 611 dsphys->ds_creation_time = gethrestime_sec(); 612 dsphys->ds_creation_txg = tx->tx_txg; 613 dsphys->ds_deadlist_obj = 614 bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 615 if (clone_parent) { 616 dsphys->ds_prev_snap_obj = clone_parent->ds_object; 617 dsphys->ds_prev_snap_txg = 618 clone_parent->ds_phys->ds_creation_txg; 619 dsphys->ds_used_bytes = 620 clone_parent->ds_phys->ds_used_bytes; 621 dsphys->ds_compressed_bytes = 622 clone_parent->ds_phys->ds_compressed_bytes; 623 dsphys->ds_uncompressed_bytes = 624 clone_parent->ds_phys->ds_uncompressed_bytes; 625 dsphys->ds_bp = clone_parent->ds_phys->ds_bp; 626 627 dmu_buf_will_dirty(clone_parent->ds_dbuf, tx); 628 clone_parent->ds_phys->ds_num_children++; 629 630 dmu_buf_will_dirty(dd->dd_dbuf, tx); 631 dd->dd_phys->dd_clone_parent_obj = clone_parent->ds_object; 632 } 633 dmu_buf_rele(dbuf, FTAG); 634 635 dmu_buf_will_dirty(dd->dd_dbuf, tx); 636 dd->dd_phys->dd_head_dataset_obj = dsobj; 637 dsl_dir_close(dd, FTAG); 638 639 return (dsobj); 640 } 641 642 struct destroyarg { 643 dsl_sync_task_group_t *dstg; 644 char *snapname; 645 char *failed; 646 }; 647 648 static int 649 dsl_snapshot_destroy_one(char *name, void *arg) 650 { 651 struct destroyarg *da = arg; 652 dsl_dataset_t *ds; 653 char *cp; 654 int err; 655 656 (void) strcat(name, "@"); 657 (void) strcat(name, da->snapname); 658 err = dsl_dataset_open(name, 659 DS_MODE_EXCLUSIVE | DS_MODE_READONLY | DS_MODE_INCONSISTENT, 660 da->dstg, &ds); 661 cp = strchr(name, '@'); 662 *cp = '\0'; 663 if (err == ENOENT) 664 return (0); 665 if (err) { 666 (void) strcpy(da->failed, name); 667 return (err); 668 } 669 670 dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 671 dsl_dataset_destroy_sync, ds, da->dstg, 0); 672 return (0); 673 } 674 675 /* 676 * Destroy 'snapname' in all descendants of 'fsname'. 677 */ 678 #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 679 int 680 dsl_snapshots_destroy(char *fsname, char *snapname) 681 { 682 int err; 683 struct destroyarg da; 684 dsl_sync_task_t *dst; 685 spa_t *spa; 686 687 err = spa_open(fsname, &spa, FTAG); 688 if (err) 689 return (err); 690 da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 691 da.snapname = snapname; 692 da.failed = fsname; 693 694 err = dmu_objset_find(fsname, 695 dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 696 697 if (err == 0) 698 err = dsl_sync_task_group_wait(da.dstg); 699 700 for (dst = list_head(&da.dstg->dstg_tasks); dst; 701 dst = list_next(&da.dstg->dstg_tasks, dst)) { 702 dsl_dataset_t *ds = dst->dst_arg1; 703 if (dst->dst_err) { 704 dsl_dataset_name(ds, fsname); 705 *strchr(fsname, '@') = '\0'; 706 } 707 /* 708 * If it was successful, destroy_sync would have 709 * closed the ds 710 */ 711 if (err) 712 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, da.dstg); 713 } 714 715 dsl_sync_task_group_destroy(da.dstg); 716 spa_close(spa, FTAG); 717 return (err); 718 } 719 720 int 721 dsl_dataset_destroy(const char *name) 722 { 723 int err; 724 dsl_sync_task_group_t *dstg; 725 objset_t *os; 726 dsl_dataset_t *ds; 727 dsl_dir_t *dd; 728 uint64_t obj; 729 730 if (strchr(name, '@')) { 731 /* Destroying a snapshot is simpler */ 732 err = dsl_dataset_open(name, 733 DS_MODE_EXCLUSIVE | DS_MODE_READONLY | DS_MODE_INCONSISTENT, 734 FTAG, &ds); 735 if (err) 736 return (err); 737 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 738 dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 739 ds, FTAG, 0); 740 if (err) 741 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG); 742 return (err); 743 } 744 745 err = dmu_objset_open(name, DMU_OST_ANY, 746 DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT, &os); 747 if (err) 748 return (err); 749 ds = os->os->os_dsl_dataset; 750 dd = ds->ds_dir; 751 752 /* 753 * Check for errors and mark this ds as inconsistent, in 754 * case we crash while freeing the objects. 755 */ 756 err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 757 dsl_dataset_destroy_begin_sync, ds, NULL, 0); 758 if (err) { 759 dmu_objset_close(os); 760 return (err); 761 } 762 763 /* 764 * remove the objects in open context, so that we won't 765 * have too much to do in syncing context. 766 */ 767 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 768 ds->ds_phys->ds_prev_snap_txg)) { 769 dmu_tx_t *tx = dmu_tx_create(os); 770 dmu_tx_hold_free(tx, obj, 0, DMU_OBJECT_END); 771 dmu_tx_hold_bonus(tx, obj); 772 err = dmu_tx_assign(tx, TXG_WAIT); 773 if (err) { 774 /* 775 * Perhaps there is not enough disk 776 * space. Just deal with it from 777 * dsl_dataset_destroy_sync(). 778 */ 779 dmu_tx_abort(tx); 780 continue; 781 } 782 VERIFY(0 == dmu_object_free(os, obj, tx)); 783 dmu_tx_commit(tx); 784 } 785 /* Make sure it's not dirty before we finish destroying it. */ 786 txg_wait_synced(dd->dd_pool, 0); 787 788 dmu_objset_close(os); 789 if (err != ESRCH) 790 return (err); 791 792 err = dsl_dataset_open(name, 793 DS_MODE_EXCLUSIVE | DS_MODE_READONLY | DS_MODE_INCONSISTENT, 794 FTAG, &ds); 795 if (err) 796 return (err); 797 798 err = dsl_dir_open(name, FTAG, &dd, NULL); 799 if (err) { 800 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG); 801 return (err); 802 } 803 804 /* 805 * Blow away the dsl_dir + head dataset. 806 */ 807 dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 808 dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 809 dsl_dataset_destroy_sync, ds, FTAG, 0); 810 dsl_sync_task_create(dstg, dsl_dir_destroy_check, 811 dsl_dir_destroy_sync, dd, FTAG, 0); 812 err = dsl_sync_task_group_wait(dstg); 813 dsl_sync_task_group_destroy(dstg); 814 /* if it is successful, *destroy_sync will close the ds+dd */ 815 if (err) { 816 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG); 817 dsl_dir_close(dd, FTAG); 818 } 819 return (err); 820 } 821 822 int 823 dsl_dataset_rollback(dsl_dataset_t *ds) 824 { 825 ASSERT3U(ds->ds_open_refcount, ==, DS_REF_MAX); 826 return (dsl_sync_task_do(ds->ds_dir->dd_pool, 827 dsl_dataset_rollback_check, dsl_dataset_rollback_sync, 828 ds, NULL, 0)); 829 } 830 831 void * 832 dsl_dataset_set_user_ptr(dsl_dataset_t *ds, 833 void *p, dsl_dataset_evict_func_t func) 834 { 835 void *old; 836 837 mutex_enter(&ds->ds_lock); 838 old = ds->ds_user_ptr; 839 if (old == NULL) { 840 ds->ds_user_ptr = p; 841 ds->ds_user_evict_func = func; 842 } 843 mutex_exit(&ds->ds_lock); 844 return (old); 845 } 846 847 void * 848 dsl_dataset_get_user_ptr(dsl_dataset_t *ds) 849 { 850 return (ds->ds_user_ptr); 851 } 852 853 854 blkptr_t * 855 dsl_dataset_get_blkptr(dsl_dataset_t *ds) 856 { 857 return (&ds->ds_phys->ds_bp); 858 } 859 860 void 861 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 862 { 863 ASSERT(dmu_tx_is_syncing(tx)); 864 /* If it's the meta-objset, set dp_meta_rootbp */ 865 if (ds == NULL) { 866 tx->tx_pool->dp_meta_rootbp = *bp; 867 } else { 868 dmu_buf_will_dirty(ds->ds_dbuf, tx); 869 ds->ds_phys->ds_bp = *bp; 870 } 871 } 872 873 spa_t * 874 dsl_dataset_get_spa(dsl_dataset_t *ds) 875 { 876 return (ds->ds_dir->dd_pool->dp_spa); 877 } 878 879 void 880 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 881 { 882 dsl_pool_t *dp; 883 884 if (ds == NULL) /* this is the meta-objset */ 885 return; 886 887 ASSERT(ds->ds_user_ptr != NULL); 888 889 if (ds->ds_phys->ds_next_snap_obj != 0) 890 panic("dirtying snapshot!"); 891 892 dp = ds->ds_dir->dd_pool; 893 894 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 895 /* up the hold count until we can be written out */ 896 dmu_buf_add_ref(ds->ds_dbuf, ds); 897 } 898 } 899 900 struct killarg { 901 uint64_t *usedp; 902 uint64_t *compressedp; 903 uint64_t *uncompressedp; 904 zio_t *zio; 905 dmu_tx_t *tx; 906 }; 907 908 static int 909 kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg) 910 { 911 struct killarg *ka = arg; 912 blkptr_t *bp = &bc->bc_blkptr; 913 914 ASSERT3U(bc->bc_errno, ==, 0); 915 916 /* 917 * Since this callback is not called concurrently, no lock is 918 * needed on the accounting values. 919 */ 920 *ka->usedp += bp_get_dasize(spa, bp); 921 *ka->compressedp += BP_GET_PSIZE(bp); 922 *ka->uncompressedp += BP_GET_UCSIZE(bp); 923 /* XXX check for EIO? */ 924 (void) arc_free(ka->zio, spa, ka->tx->tx_txg, bp, NULL, NULL, 925 ARC_NOWAIT); 926 return (0); 927 } 928 929 /* ARGSUSED */ 930 static int 931 dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx) 932 { 933 dsl_dataset_t *ds = arg1; 934 935 /* 936 * There must be a previous snapshot. I suppose we could roll 937 * it back to being empty (and re-initialize the upper (ZPL) 938 * layer). But for now there's no way to do this via the user 939 * interface. 940 */ 941 if (ds->ds_phys->ds_prev_snap_txg == 0) 942 return (EINVAL); 943 944 /* 945 * This must not be a snapshot. 946 */ 947 if (ds->ds_phys->ds_next_snap_obj != 0) 948 return (EINVAL); 949 950 /* 951 * If we made changes this txg, traverse_dsl_dataset won't find 952 * them. Try again. 953 */ 954 if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 955 return (EAGAIN); 956 957 return (0); 958 } 959 960 /* ARGSUSED */ 961 static void 962 dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 963 { 964 dsl_dataset_t *ds = arg1; 965 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 966 967 dmu_buf_will_dirty(ds->ds_dbuf, tx); 968 969 /* Zero out the deadlist. */ 970 bplist_close(&ds->ds_deadlist); 971 bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 972 ds->ds_phys->ds_deadlist_obj = 973 bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 974 VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 975 ds->ds_phys->ds_deadlist_obj)); 976 977 { 978 /* Free blkptrs that we gave birth to */ 979 zio_t *zio; 980 uint64_t used = 0, compressed = 0, uncompressed = 0; 981 struct killarg ka; 982 983 zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL, 984 ZIO_FLAG_MUSTSUCCEED); 985 ka.usedp = &used; 986 ka.compressedp = &compressed; 987 ka.uncompressedp = &uncompressed; 988 ka.zio = zio; 989 ka.tx = tx; 990 (void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 991 ADVANCE_POST, kill_blkptr, &ka); 992 (void) zio_wait(zio); 993 994 dsl_dir_diduse_space(ds->ds_dir, 995 -used, -compressed, -uncompressed, tx); 996 } 997 998 /* Change our contents to that of the prev snapshot */ 999 ASSERT3U(ds->ds_prev->ds_object, ==, ds->ds_phys->ds_prev_snap_obj); 1000 ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp; 1001 ds->ds_phys->ds_used_bytes = ds->ds_prev->ds_phys->ds_used_bytes; 1002 ds->ds_phys->ds_compressed_bytes = 1003 ds->ds_prev->ds_phys->ds_compressed_bytes; 1004 ds->ds_phys->ds_uncompressed_bytes = 1005 ds->ds_prev->ds_phys->ds_uncompressed_bytes; 1006 ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags; 1007 ds->ds_phys->ds_unique_bytes = 0; 1008 1009 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 1010 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1011 ds->ds_prev->ds_phys->ds_unique_bytes = 0; 1012 } 1013 1014 spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa, 1015 tx, cr, "dataset = %llu", ds->ds_object); 1016 } 1017 1018 /* ARGSUSED */ 1019 static int 1020 dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 1021 { 1022 dsl_dataset_t *ds = arg1; 1023 1024 /* 1025 * Can't delete a head dataset if there are snapshots of it. 1026 * (Except if the only snapshots are from the branch we cloned 1027 * from.) 1028 */ 1029 if (ds->ds_prev != NULL && 1030 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1031 return (EINVAL); 1032 1033 return (0); 1034 } 1035 1036 /* ARGSUSED */ 1037 static void 1038 dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1039 { 1040 dsl_dataset_t *ds = arg1; 1041 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1042 1043 /* Mark it as inconsistent on-disk, in case we crash */ 1044 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1045 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 1046 1047 spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 1048 cr, "dataset = %llu", ds->ds_object); 1049 } 1050 1051 /* ARGSUSED */ 1052 static int 1053 dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 1054 { 1055 dsl_dataset_t *ds = arg1; 1056 1057 /* Can't delete a branch point. */ 1058 if (ds->ds_phys->ds_num_children > 1) 1059 return (EEXIST); 1060 1061 /* 1062 * Can't delete a head dataset if there are snapshots of it. 1063 * (Except if the only snapshots are from the branch we cloned 1064 * from.) 1065 */ 1066 if (ds->ds_prev != NULL && 1067 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1068 return (EINVAL); 1069 1070 /* 1071 * If we made changes this txg, traverse_dsl_dataset won't find 1072 * them. Try again. 1073 */ 1074 if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1075 return (EAGAIN); 1076 1077 /* XXX we should do some i/o error checking... */ 1078 return (0); 1079 } 1080 1081 static void 1082 dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 1083 { 1084 dsl_dataset_t *ds = arg1; 1085 uint64_t used = 0, compressed = 0, uncompressed = 0; 1086 zio_t *zio; 1087 int err; 1088 int after_branch_point = FALSE; 1089 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1090 objset_t *mos = dp->dp_meta_objset; 1091 dsl_dataset_t *ds_prev = NULL; 1092 uint64_t obj; 1093 1094 ASSERT3U(ds->ds_open_refcount, ==, DS_REF_MAX); 1095 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1); 1096 ASSERT(ds->ds_prev == NULL || 1097 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 1098 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 1099 1100 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1101 1102 obj = ds->ds_object; 1103 1104 if (ds->ds_phys->ds_prev_snap_obj != 0) { 1105 if (ds->ds_prev) { 1106 ds_prev = ds->ds_prev; 1107 } else { 1108 VERIFY(0 == dsl_dataset_open_obj(dp, 1109 ds->ds_phys->ds_prev_snap_obj, NULL, 1110 DS_MODE_NONE, FTAG, &ds_prev)); 1111 } 1112 after_branch_point = 1113 (ds_prev->ds_phys->ds_next_snap_obj != obj); 1114 1115 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1116 if (after_branch_point && 1117 ds->ds_phys->ds_next_snap_obj == 0) { 1118 /* This clone is toast. */ 1119 ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1120 ds_prev->ds_phys->ds_num_children--; 1121 } else if (!after_branch_point) { 1122 ds_prev->ds_phys->ds_next_snap_obj = 1123 ds->ds_phys->ds_next_snap_obj; 1124 } 1125 } 1126 1127 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1128 1129 if (ds->ds_phys->ds_next_snap_obj != 0) { 1130 blkptr_t bp; 1131 dsl_dataset_t *ds_next; 1132 uint64_t itor = 0; 1133 1134 spa_scrub_restart(dp->dp_spa, tx->tx_txg); 1135 1136 VERIFY(0 == dsl_dataset_open_obj(dp, 1137 ds->ds_phys->ds_next_snap_obj, NULL, 1138 DS_MODE_NONE, FTAG, &ds_next)); 1139 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1140 1141 dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1142 ds_next->ds_phys->ds_prev_snap_obj = 1143 ds->ds_phys->ds_prev_snap_obj; 1144 ds_next->ds_phys->ds_prev_snap_txg = 1145 ds->ds_phys->ds_prev_snap_txg; 1146 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1147 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1148 1149 /* 1150 * Transfer to our deadlist (which will become next's 1151 * new deadlist) any entries from next's current 1152 * deadlist which were born before prev, and free the 1153 * other entries. 1154 * 1155 * XXX we're doing this long task with the config lock held 1156 */ 1157 while (bplist_iterate(&ds_next->ds_deadlist, &itor, 1158 &bp) == 0) { 1159 if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 1160 VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 1161 &bp, tx)); 1162 if (ds_prev && !after_branch_point && 1163 bp.blk_birth > 1164 ds_prev->ds_phys->ds_prev_snap_txg) { 1165 ds_prev->ds_phys->ds_unique_bytes += 1166 bp_get_dasize(dp->dp_spa, &bp); 1167 } 1168 } else { 1169 used += bp_get_dasize(dp->dp_spa, &bp); 1170 compressed += BP_GET_PSIZE(&bp); 1171 uncompressed += BP_GET_UCSIZE(&bp); 1172 /* XXX check return value? */ 1173 (void) arc_free(zio, dp->dp_spa, tx->tx_txg, 1174 &bp, NULL, NULL, ARC_NOWAIT); 1175 } 1176 } 1177 1178 /* free next's deadlist */ 1179 bplist_close(&ds_next->ds_deadlist); 1180 bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1181 1182 /* set next's deadlist to our deadlist */ 1183 ds_next->ds_phys->ds_deadlist_obj = 1184 ds->ds_phys->ds_deadlist_obj; 1185 VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 1186 ds_next->ds_phys->ds_deadlist_obj)); 1187 ds->ds_phys->ds_deadlist_obj = 0; 1188 1189 if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1190 /* 1191 * Update next's unique to include blocks which 1192 * were previously shared by only this snapshot 1193 * and it. Those blocks will be born after the 1194 * prev snap and before this snap, and will have 1195 * died after the next snap and before the one 1196 * after that (ie. be on the snap after next's 1197 * deadlist). 1198 * 1199 * XXX we're doing this long task with the 1200 * config lock held 1201 */ 1202 dsl_dataset_t *ds_after_next; 1203 1204 VERIFY(0 == dsl_dataset_open_obj(dp, 1205 ds_next->ds_phys->ds_next_snap_obj, NULL, 1206 DS_MODE_NONE, FTAG, &ds_after_next)); 1207 itor = 0; 1208 while (bplist_iterate(&ds_after_next->ds_deadlist, 1209 &itor, &bp) == 0) { 1210 if (bp.blk_birth > 1211 ds->ds_phys->ds_prev_snap_txg && 1212 bp.blk_birth <= 1213 ds->ds_phys->ds_creation_txg) { 1214 ds_next->ds_phys->ds_unique_bytes += 1215 bp_get_dasize(dp->dp_spa, &bp); 1216 } 1217 } 1218 1219 dsl_dataset_close(ds_after_next, DS_MODE_NONE, FTAG); 1220 ASSERT3P(ds_next->ds_prev, ==, NULL); 1221 } else { 1222 /* 1223 * It would be nice to update the head dataset's 1224 * unique. To do so we would have to traverse 1225 * it for blocks born after ds_prev, which is 1226 * pretty expensive just to maintain something 1227 * for debugging purposes. 1228 */ 1229 ASSERT3P(ds_next->ds_prev, ==, ds); 1230 dsl_dataset_close(ds_next->ds_prev, DS_MODE_NONE, 1231 ds_next); 1232 if (ds_prev) { 1233 VERIFY(0 == dsl_dataset_open_obj(dp, 1234 ds->ds_phys->ds_prev_snap_obj, NULL, 1235 DS_MODE_NONE, ds_next, &ds_next->ds_prev)); 1236 } else { 1237 ds_next->ds_prev = NULL; 1238 } 1239 } 1240 dsl_dataset_close(ds_next, DS_MODE_NONE, FTAG); 1241 1242 /* 1243 * NB: unique_bytes is not accurate for head objsets 1244 * because we don't update it when we delete the most 1245 * recent snapshot -- see above comment. 1246 */ 1247 ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 1248 } else { 1249 /* 1250 * There's no next snapshot, so this is a head dataset. 1251 * Destroy the deadlist. Unless it's a clone, the 1252 * deadlist should be empty. (If it's a clone, it's 1253 * safe to ignore the deadlist contents.) 1254 */ 1255 struct killarg ka; 1256 1257 ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1258 bplist_close(&ds->ds_deadlist); 1259 bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1260 ds->ds_phys->ds_deadlist_obj = 0; 1261 1262 /* 1263 * Free everything that we point to (that's born after 1264 * the previous snapshot, if we are a clone) 1265 * 1266 * XXX we're doing this long task with the config lock held 1267 */ 1268 ka.usedp = &used; 1269 ka.compressedp = &compressed; 1270 ka.uncompressedp = &uncompressed; 1271 ka.zio = zio; 1272 ka.tx = tx; 1273 err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 1274 ADVANCE_POST, kill_blkptr, &ka); 1275 ASSERT3U(err, ==, 0); 1276 } 1277 1278 err = zio_wait(zio); 1279 ASSERT3U(err, ==, 0); 1280 1281 dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx); 1282 1283 if (ds->ds_phys->ds_snapnames_zapobj) { 1284 err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1285 ASSERT(err == 0); 1286 } 1287 1288 if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 1289 /* Erase the link in the dataset */ 1290 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 1291 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 1292 /* 1293 * dsl_dir_sync_destroy() called us, they'll destroy 1294 * the dataset. 1295 */ 1296 } else { 1297 /* remove from snapshot namespace */ 1298 dsl_dataset_t *ds_head; 1299 VERIFY(0 == dsl_dataset_open_obj(dp, 1300 ds->ds_dir->dd_phys->dd_head_dataset_obj, NULL, 1301 DS_MODE_NONE, FTAG, &ds_head)); 1302 VERIFY(0 == dsl_dataset_get_snapname(ds)); 1303 #ifdef ZFS_DEBUG 1304 { 1305 uint64_t val; 1306 err = zap_lookup(mos, 1307 ds_head->ds_phys->ds_snapnames_zapobj, 1308 ds->ds_snapname, 8, 1, &val); 1309 ASSERT3U(err, ==, 0); 1310 ASSERT3U(val, ==, obj); 1311 } 1312 #endif 1313 err = zap_remove(mos, ds_head->ds_phys->ds_snapnames_zapobj, 1314 ds->ds_snapname, tx); 1315 ASSERT(err == 0); 1316 dsl_dataset_close(ds_head, DS_MODE_NONE, FTAG); 1317 } 1318 1319 if (ds_prev && ds->ds_prev != ds_prev) 1320 dsl_dataset_close(ds_prev, DS_MODE_NONE, FTAG); 1321 1322 spa_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 1323 spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 1324 cr, "dataset = %llu", ds->ds_object); 1325 1326 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, tag); 1327 VERIFY(0 == dmu_object_free(mos, obj, tx)); 1328 1329 } 1330 1331 /* ARGSUSED */ 1332 int 1333 dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 1334 { 1335 objset_t *os = arg1; 1336 dsl_dataset_t *ds = os->os->os_dsl_dataset; 1337 const char *snapname = arg2; 1338 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1339 int err; 1340 uint64_t value; 1341 1342 /* 1343 * We don't allow multiple snapshots of the same txg. If there 1344 * is already one, try again. 1345 */ 1346 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 1347 return (EAGAIN); 1348 1349 /* 1350 * Check for conflicting name snapshot name. 1351 */ 1352 err = zap_lookup(mos, ds->ds_phys->ds_snapnames_zapobj, 1353 snapname, 8, 1, &value); 1354 if (err == 0) 1355 return (EEXIST); 1356 if (err != ENOENT) 1357 return (err); 1358 1359 /* 1360 * Check that the dataset's name is not too long. Name consists 1361 * of the dataset's length + 1 for the @-sign + snapshot name's length 1362 */ 1363 if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 1364 return (ENAMETOOLONG); 1365 1366 ds->ds_trysnap_txg = tx->tx_txg; 1367 return (0); 1368 } 1369 1370 void 1371 dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1372 { 1373 objset_t *os = arg1; 1374 dsl_dataset_t *ds = os->os->os_dsl_dataset; 1375 const char *snapname = arg2; 1376 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1377 dmu_buf_t *dbuf; 1378 dsl_dataset_phys_t *dsphys; 1379 uint64_t dsobj; 1380 objset_t *mos = dp->dp_meta_objset; 1381 int err; 1382 1383 spa_scrub_restart(dp->dp_spa, tx->tx_txg); 1384 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1385 1386 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1387 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 1388 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1389 dmu_buf_will_dirty(dbuf, tx); 1390 dsphys = dbuf->db_data; 1391 dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1392 dsphys->ds_fsid_guid = unique_create(); 1393 unique_remove(dsphys->ds_fsid_guid); /* it isn't open yet */ 1394 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1395 sizeof (dsphys->ds_guid)); 1396 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1397 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1398 dsphys->ds_next_snap_obj = ds->ds_object; 1399 dsphys->ds_num_children = 1; 1400 dsphys->ds_creation_time = gethrestime_sec(); 1401 dsphys->ds_creation_txg = tx->tx_txg; 1402 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1403 dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1404 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1405 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 1406 dsphys->ds_flags = ds->ds_phys->ds_flags; 1407 dsphys->ds_bp = ds->ds_phys->ds_bp; 1408 dmu_buf_rele(dbuf, FTAG); 1409 1410 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 1411 if (ds->ds_prev) { 1412 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1413 ds->ds_object || 1414 ds->ds_prev->ds_phys->ds_num_children > 1); 1415 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 1416 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1417 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1418 ds->ds_prev->ds_phys->ds_creation_txg); 1419 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 1420 } 1421 } 1422 1423 bplist_close(&ds->ds_deadlist); 1424 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1425 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, dsphys->ds_creation_txg); 1426 ds->ds_phys->ds_prev_snap_obj = dsobj; 1427 ds->ds_phys->ds_prev_snap_txg = dsphys->ds_creation_txg; 1428 ds->ds_phys->ds_unique_bytes = 0; 1429 ds->ds_phys->ds_deadlist_obj = 1430 bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 1431 VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 1432 ds->ds_phys->ds_deadlist_obj)); 1433 1434 dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1435 err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1436 snapname, 8, 1, &dsobj, tx); 1437 ASSERT(err == 0); 1438 1439 if (ds->ds_prev) 1440 dsl_dataset_close(ds->ds_prev, DS_MODE_NONE, ds); 1441 VERIFY(0 == dsl_dataset_open_obj(dp, 1442 ds->ds_phys->ds_prev_snap_obj, snapname, 1443 DS_MODE_NONE, ds, &ds->ds_prev)); 1444 1445 spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 1446 "dataset = %llu", dsobj); 1447 } 1448 1449 void 1450 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1451 { 1452 ASSERT(dmu_tx_is_syncing(tx)); 1453 ASSERT(ds->ds_user_ptr != NULL); 1454 ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1455 1456 dsl_dir_dirty(ds->ds_dir, tx); 1457 dmu_objset_sync(ds->ds_user_ptr, zio, tx); 1458 /* Unneeded? bplist_close(&ds->ds_deadlist); */ 1459 } 1460 1461 void 1462 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1463 { 1464 dsl_dir_stats(ds->ds_dir, nv); 1465 1466 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1467 ds->ds_phys->ds_creation_time); 1468 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1469 ds->ds_phys->ds_creation_txg); 1470 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, 1471 ds->ds_phys->ds_used_bytes); 1472 1473 if (ds->ds_phys->ds_next_snap_obj) { 1474 /* 1475 * This is a snapshot; override the dd's space used with 1476 * our unique space and compression ratio. 1477 */ 1478 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 1479 ds->ds_phys->ds_unique_bytes); 1480 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 1481 ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 1482 (ds->ds_phys->ds_uncompressed_bytes * 100 / 1483 ds->ds_phys->ds_compressed_bytes)); 1484 } 1485 } 1486 1487 void 1488 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1489 { 1490 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 1491 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 1492 if (ds->ds_phys->ds_next_snap_obj) { 1493 stat->dds_is_snapshot = B_TRUE; 1494 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 1495 } 1496 1497 /* clone origin is really a dsl_dir thing... */ 1498 if (ds->ds_dir->dd_phys->dd_clone_parent_obj) { 1499 dsl_dataset_t *ods; 1500 1501 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 1502 VERIFY(0 == dsl_dataset_open_obj(ds->ds_dir->dd_pool, 1503 ds->ds_dir->dd_phys->dd_clone_parent_obj, 1504 NULL, DS_MODE_NONE, FTAG, &ods)); 1505 dsl_dataset_name(ods, stat->dds_clone_of); 1506 dsl_dataset_close(ods, DS_MODE_NONE, FTAG); 1507 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 1508 } 1509 } 1510 1511 uint64_t 1512 dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1513 { 1514 return (ds->ds_phys->ds_fsid_guid); 1515 } 1516 1517 void 1518 dsl_dataset_space(dsl_dataset_t *ds, 1519 uint64_t *refdbytesp, uint64_t *availbytesp, 1520 uint64_t *usedobjsp, uint64_t *availobjsp) 1521 { 1522 *refdbytesp = ds->ds_phys->ds_used_bytes; 1523 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1524 *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 1525 *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1526 } 1527 1528 /* ARGSUSED */ 1529 static int 1530 dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 1531 { 1532 dsl_dataset_t *ds = arg1; 1533 char *newsnapname = arg2; 1534 dsl_dir_t *dd = ds->ds_dir; 1535 objset_t *mos = dd->dd_pool->dp_meta_objset; 1536 dsl_dataset_t *hds; 1537 uint64_t val; 1538 int err; 1539 1540 err = dsl_dataset_open_obj(dd->dd_pool, 1541 dd->dd_phys->dd_head_dataset_obj, NULL, DS_MODE_NONE, FTAG, &hds); 1542 if (err) 1543 return (err); 1544 1545 /* new name better not be in use */ 1546 err = zap_lookup(mos, hds->ds_phys->ds_snapnames_zapobj, 1547 newsnapname, 8, 1, &val); 1548 dsl_dataset_close(hds, DS_MODE_NONE, FTAG); 1549 1550 if (err == 0) 1551 err = EEXIST; 1552 else if (err == ENOENT) 1553 err = 0; 1554 1555 /* dataset name + 1 for the "@" + the new snapshot name must fit */ 1556 if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 1557 err = ENAMETOOLONG; 1558 1559 return (err); 1560 } 1561 1562 static void 1563 dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 1564 cred_t *cr, dmu_tx_t *tx) 1565 { 1566 dsl_dataset_t *ds = arg1; 1567 const char *newsnapname = arg2; 1568 dsl_dir_t *dd = ds->ds_dir; 1569 objset_t *mos = dd->dd_pool->dp_meta_objset; 1570 dsl_dataset_t *hds; 1571 int err; 1572 1573 ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 1574 1575 VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, 1576 dd->dd_phys->dd_head_dataset_obj, NULL, DS_MODE_NONE, FTAG, &hds)); 1577 1578 VERIFY(0 == dsl_dataset_get_snapname(ds)); 1579 err = zap_remove(mos, hds->ds_phys->ds_snapnames_zapobj, 1580 ds->ds_snapname, tx); 1581 ASSERT3U(err, ==, 0); 1582 mutex_enter(&ds->ds_lock); 1583 (void) strcpy(ds->ds_snapname, newsnapname); 1584 mutex_exit(&ds->ds_lock); 1585 err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 1586 ds->ds_snapname, 8, 1, &ds->ds_object, tx); 1587 ASSERT3U(err, ==, 0); 1588 1589 spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 1590 cr, "dataset = %llu", ds->ds_object); 1591 dsl_dataset_close(hds, DS_MODE_NONE, FTAG); 1592 } 1593 1594 struct renamearg { 1595 dsl_sync_task_group_t *dstg; 1596 char failed[MAXPATHLEN]; 1597 char *oldsnap; 1598 char *newsnap; 1599 }; 1600 1601 static int 1602 dsl_snapshot_rename_one(char *name, void *arg) 1603 { 1604 struct renamearg *ra = arg; 1605 dsl_dataset_t *ds = NULL; 1606 char *cp; 1607 int err; 1608 1609 cp = name + strlen(name); 1610 *cp = '@'; 1611 (void) strcpy(cp + 1, ra->oldsnap); 1612 1613 /* 1614 * For recursive snapshot renames the parent won't be changing 1615 * so we just pass name for both the to/from argument. 1616 */ 1617 if (err = zfs_secpolicy_rename_perms(name, name, CRED())) { 1618 (void) strcpy(ra->failed, name); 1619 return (err); 1620 } 1621 1622 err = dsl_dataset_open(name, DS_MODE_READONLY | DS_MODE_STANDARD, 1623 ra->dstg, &ds); 1624 if (err == ENOENT) { 1625 *cp = '\0'; 1626 return (0); 1627 } 1628 if (err) { 1629 (void) strcpy(ra->failed, name); 1630 *cp = '\0'; 1631 dsl_dataset_close(ds, DS_MODE_STANDARD, ra->dstg); 1632 return (err); 1633 } 1634 1635 #ifdef _KERNEL 1636 /* for all filesystems undergoing rename, we'll need to unmount it */ 1637 (void) zfs_unmount_snap(name, NULL); 1638 #endif 1639 1640 *cp = '\0'; 1641 1642 dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 1643 dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 1644 1645 return (0); 1646 } 1647 1648 static int 1649 dsl_recursive_rename(char *oldname, const char *newname) 1650 { 1651 int err; 1652 struct renamearg *ra; 1653 dsl_sync_task_t *dst; 1654 spa_t *spa; 1655 char *cp, *fsname = spa_strdup(oldname); 1656 int len = strlen(oldname); 1657 1658 /* truncate the snapshot name to get the fsname */ 1659 cp = strchr(fsname, '@'); 1660 *cp = '\0'; 1661 1662 err = spa_open(fsname, &spa, FTAG); 1663 if (err) { 1664 kmem_free(fsname, len + 1); 1665 return (err); 1666 } 1667 ra = kmem_alloc(sizeof (struct renamearg), KM_SLEEP); 1668 ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 1669 1670 ra->oldsnap = strchr(oldname, '@') + 1; 1671 ra->newsnap = strchr(newname, '@') + 1; 1672 *ra->failed = '\0'; 1673 1674 err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 1675 DS_FIND_CHILDREN); 1676 kmem_free(fsname, len + 1); 1677 1678 if (err == 0) { 1679 err = dsl_sync_task_group_wait(ra->dstg); 1680 } 1681 1682 for (dst = list_head(&ra->dstg->dstg_tasks); dst; 1683 dst = list_next(&ra->dstg->dstg_tasks, dst)) { 1684 dsl_dataset_t *ds = dst->dst_arg1; 1685 if (dst->dst_err) { 1686 dsl_dir_name(ds->ds_dir, ra->failed); 1687 (void) strcat(ra->failed, "@"); 1688 (void) strcat(ra->failed, ra->newsnap); 1689 } 1690 dsl_dataset_close(ds, DS_MODE_STANDARD, ra->dstg); 1691 } 1692 1693 if (err) 1694 (void) strcpy(oldname, ra->failed); 1695 1696 dsl_sync_task_group_destroy(ra->dstg); 1697 kmem_free(ra, sizeof (struct renamearg)); 1698 spa_close(spa, FTAG); 1699 return (err); 1700 } 1701 1702 static int 1703 dsl_valid_rename(char *oldname, void *arg) 1704 { 1705 int delta = *(int *)arg; 1706 1707 if (strlen(oldname) + delta >= MAXNAMELEN) 1708 return (ENAMETOOLONG); 1709 1710 return (0); 1711 } 1712 1713 #pragma weak dmu_objset_rename = dsl_dataset_rename 1714 int 1715 dsl_dataset_rename(char *oldname, const char *newname, 1716 boolean_t recursive) 1717 { 1718 dsl_dir_t *dd; 1719 dsl_dataset_t *ds; 1720 const char *tail; 1721 int err; 1722 1723 err = dsl_dir_open(oldname, FTAG, &dd, &tail); 1724 if (err) 1725 return (err); 1726 if (tail == NULL) { 1727 int delta = strlen(newname) - strlen(oldname); 1728 1729 /* if we're growing, validate child size lengths */ 1730 if (delta > 0) 1731 err = dmu_objset_find(oldname, dsl_valid_rename, 1732 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 1733 1734 if (!err) 1735 err = dsl_dir_rename(dd, newname); 1736 dsl_dir_close(dd, FTAG); 1737 return (err); 1738 } 1739 if (tail[0] != '@') { 1740 /* the name ended in a nonexistant component */ 1741 dsl_dir_close(dd, FTAG); 1742 return (ENOENT); 1743 } 1744 1745 dsl_dir_close(dd, FTAG); 1746 1747 /* new name must be snapshot in same filesystem */ 1748 tail = strchr(newname, '@'); 1749 if (tail == NULL) 1750 return (EINVAL); 1751 tail++; 1752 if (strncmp(oldname, newname, tail - newname) != 0) 1753 return (EXDEV); 1754 1755 if (recursive) { 1756 err = dsl_recursive_rename(oldname, newname); 1757 } else { 1758 err = dsl_dataset_open(oldname, 1759 DS_MODE_READONLY | DS_MODE_STANDARD, FTAG, &ds); 1760 if (err) 1761 return (err); 1762 1763 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 1764 dsl_dataset_snapshot_rename_check, 1765 dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 1766 1767 dsl_dataset_close(ds, DS_MODE_STANDARD, FTAG); 1768 } 1769 1770 return (err); 1771 } 1772 1773 struct promotearg { 1774 uint64_t used, comp, uncomp, unique; 1775 uint64_t newnext_obj, snapnames_obj; 1776 }; 1777 1778 /* ARGSUSED */ 1779 static int 1780 dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 1781 { 1782 dsl_dataset_t *hds = arg1; 1783 struct promotearg *pa = arg2; 1784 dsl_dir_t *dd = hds->ds_dir; 1785 dsl_pool_t *dp = hds->ds_dir->dd_pool; 1786 dsl_dir_t *pdd = NULL; 1787 dsl_dataset_t *ds = NULL; 1788 dsl_dataset_t *pivot_ds = NULL; 1789 dsl_dataset_t *newnext_ds = NULL; 1790 int err; 1791 char *name = NULL; 1792 uint64_t itor = 0; 1793 blkptr_t bp; 1794 1795 bzero(pa, sizeof (*pa)); 1796 1797 /* Check that it is a clone */ 1798 if (dd->dd_phys->dd_clone_parent_obj == 0) 1799 return (EINVAL); 1800 1801 /* Since this is so expensive, don't do the preliminary check */ 1802 if (!dmu_tx_is_syncing(tx)) 1803 return (0); 1804 1805 if (err = dsl_dataset_open_obj(dp, 1806 dd->dd_phys->dd_clone_parent_obj, 1807 NULL, DS_MODE_EXCLUSIVE, FTAG, &pivot_ds)) 1808 goto out; 1809 pdd = pivot_ds->ds_dir; 1810 1811 { 1812 dsl_dataset_t *phds; 1813 if (err = dsl_dataset_open_obj(dd->dd_pool, 1814 pdd->dd_phys->dd_head_dataset_obj, 1815 NULL, DS_MODE_NONE, FTAG, &phds)) 1816 goto out; 1817 pa->snapnames_obj = phds->ds_phys->ds_snapnames_zapobj; 1818 dsl_dataset_close(phds, DS_MODE_NONE, FTAG); 1819 } 1820 1821 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) { 1822 err = EXDEV; 1823 goto out; 1824 } 1825 1826 /* find pivot point's new next ds */ 1827 VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, hds->ds_object, 1828 NULL, DS_MODE_NONE, FTAG, &newnext_ds)); 1829 while (newnext_ds->ds_phys->ds_prev_snap_obj != pivot_ds->ds_object) { 1830 dsl_dataset_t *prev; 1831 1832 if (err = dsl_dataset_open_obj(dd->dd_pool, 1833 newnext_ds->ds_phys->ds_prev_snap_obj, 1834 NULL, DS_MODE_NONE, FTAG, &prev)) 1835 goto out; 1836 dsl_dataset_close(newnext_ds, DS_MODE_NONE, FTAG); 1837 newnext_ds = prev; 1838 } 1839 pa->newnext_obj = newnext_ds->ds_object; 1840 1841 /* compute pivot point's new unique space */ 1842 while ((err = bplist_iterate(&newnext_ds->ds_deadlist, 1843 &itor, &bp)) == 0) { 1844 if (bp.blk_birth > pivot_ds->ds_phys->ds_prev_snap_txg) 1845 pa->unique += bp_get_dasize(dd->dd_pool->dp_spa, &bp); 1846 } 1847 if (err != ENOENT) 1848 goto out; 1849 1850 /* Walk the snapshots that we are moving */ 1851 name = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1852 ds = pivot_ds; 1853 /* CONSTCOND */ 1854 while (TRUE) { 1855 uint64_t val, dlused, dlcomp, dluncomp; 1856 dsl_dataset_t *prev; 1857 1858 /* Check that the snapshot name does not conflict */ 1859 dsl_dataset_name(ds, name); 1860 err = zap_lookup(dd->dd_pool->dp_meta_objset, 1861 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 1862 8, 1, &val); 1863 if (err != ENOENT) { 1864 if (err == 0) 1865 err = EEXIST; 1866 goto out; 1867 } 1868 1869 /* 1870 * compute space to transfer. Each snapshot gave birth to: 1871 * (my used) - (prev's used) + (deadlist's used) 1872 */ 1873 pa->used += ds->ds_phys->ds_used_bytes; 1874 pa->comp += ds->ds_phys->ds_compressed_bytes; 1875 pa->uncomp += ds->ds_phys->ds_uncompressed_bytes; 1876 1877 /* If we reach the first snapshot, we're done. */ 1878 if (ds->ds_phys->ds_prev_snap_obj == 0) 1879 break; 1880 1881 if (err = bplist_space(&ds->ds_deadlist, 1882 &dlused, &dlcomp, &dluncomp)) 1883 goto out; 1884 if (err = dsl_dataset_open_obj(dd->dd_pool, 1885 ds->ds_phys->ds_prev_snap_obj, NULL, DS_MODE_EXCLUSIVE, 1886 FTAG, &prev)) 1887 goto out; 1888 pa->used += dlused - prev->ds_phys->ds_used_bytes; 1889 pa->comp += dlcomp - prev->ds_phys->ds_compressed_bytes; 1890 pa->uncomp += dluncomp - prev->ds_phys->ds_uncompressed_bytes; 1891 1892 /* 1893 * We could be a clone of a clone. If we reach our 1894 * parent's branch point, we're done. 1895 */ 1896 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) { 1897 dsl_dataset_close(prev, DS_MODE_EXCLUSIVE, FTAG); 1898 break; 1899 } 1900 if (ds != pivot_ds) 1901 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG); 1902 ds = prev; 1903 } 1904 1905 /* Check that there is enough space here */ 1906 err = dsl_dir_transfer_possible(pdd, dd, pa->used); 1907 1908 out: 1909 if (ds && ds != pivot_ds) 1910 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG); 1911 if (pivot_ds) 1912 dsl_dataset_close(pivot_ds, DS_MODE_EXCLUSIVE, FTAG); 1913 if (newnext_ds) 1914 dsl_dataset_close(newnext_ds, DS_MODE_NONE, FTAG); 1915 if (name) 1916 kmem_free(name, MAXPATHLEN); 1917 return (err); 1918 } 1919 1920 static void 1921 dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1922 { 1923 dsl_dataset_t *hds = arg1; 1924 struct promotearg *pa = arg2; 1925 dsl_dir_t *dd = hds->ds_dir; 1926 dsl_pool_t *dp = hds->ds_dir->dd_pool; 1927 dsl_dir_t *pdd = NULL; 1928 dsl_dataset_t *ds, *pivot_ds; 1929 char *name; 1930 1931 ASSERT(dd->dd_phys->dd_clone_parent_obj != 0); 1932 ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 1933 1934 VERIFY(0 == dsl_dataset_open_obj(dp, 1935 dd->dd_phys->dd_clone_parent_obj, 1936 NULL, DS_MODE_EXCLUSIVE, FTAG, &pivot_ds)); 1937 /* 1938 * We need to explicitly open pdd, since pivot_ds's pdd will be 1939 * changing. 1940 */ 1941 VERIFY(0 == dsl_dir_open_obj(dp, pivot_ds->ds_dir->dd_object, 1942 NULL, FTAG, &pdd)); 1943 1944 /* move snapshots to this dir */ 1945 name = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1946 ds = pivot_ds; 1947 /* CONSTCOND */ 1948 while (TRUE) { 1949 dsl_dataset_t *prev; 1950 1951 /* move snap name entry */ 1952 dsl_dataset_name(ds, name); 1953 VERIFY(0 == zap_remove(dp->dp_meta_objset, 1954 pa->snapnames_obj, ds->ds_snapname, tx)); 1955 VERIFY(0 == zap_add(dp->dp_meta_objset, 1956 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 1957 8, 1, &ds->ds_object, tx)); 1958 1959 /* change containing dsl_dir */ 1960 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1961 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, pdd->dd_object); 1962 ds->ds_phys->ds_dir_obj = dd->dd_object; 1963 ASSERT3P(ds->ds_dir, ==, pdd); 1964 dsl_dir_close(ds->ds_dir, ds); 1965 VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 1966 NULL, ds, &ds->ds_dir)); 1967 1968 ASSERT3U(dsl_prop_numcb(ds), ==, 0); 1969 1970 if (ds->ds_phys->ds_prev_snap_obj == 0) 1971 break; 1972 1973 VERIFY(0 == dsl_dataset_open_obj(dp, 1974 ds->ds_phys->ds_prev_snap_obj, NULL, DS_MODE_EXCLUSIVE, 1975 FTAG, &prev)); 1976 1977 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) { 1978 dsl_dataset_close(prev, DS_MODE_EXCLUSIVE, FTAG); 1979 break; 1980 } 1981 if (ds != pivot_ds) 1982 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG); 1983 ds = prev; 1984 } 1985 if (ds != pivot_ds) 1986 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG); 1987 1988 /* change pivot point's next snap */ 1989 dmu_buf_will_dirty(pivot_ds->ds_dbuf, tx); 1990 pivot_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj; 1991 1992 /* change clone_parent-age */ 1993 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1994 ASSERT3U(dd->dd_phys->dd_clone_parent_obj, ==, pivot_ds->ds_object); 1995 dd->dd_phys->dd_clone_parent_obj = pdd->dd_phys->dd_clone_parent_obj; 1996 dmu_buf_will_dirty(pdd->dd_dbuf, tx); 1997 pdd->dd_phys->dd_clone_parent_obj = pivot_ds->ds_object; 1998 1999 /* change space accounting */ 2000 dsl_dir_diduse_space(pdd, -pa->used, -pa->comp, -pa->uncomp, tx); 2001 dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx); 2002 pivot_ds->ds_phys->ds_unique_bytes = pa->unique; 2003 2004 /* log history record */ 2005 spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 2006 cr, "dataset = %llu", ds->ds_object); 2007 2008 dsl_dir_close(pdd, FTAG); 2009 dsl_dataset_close(pivot_ds, DS_MODE_EXCLUSIVE, FTAG); 2010 kmem_free(name, MAXPATHLEN); 2011 } 2012 2013 int 2014 dsl_dataset_promote(const char *name) 2015 { 2016 dsl_dataset_t *ds; 2017 int err; 2018 dmu_object_info_t doi; 2019 struct promotearg pa; 2020 2021 err = dsl_dataset_open(name, DS_MODE_NONE, FTAG, &ds); 2022 if (err) 2023 return (err); 2024 2025 err = dmu_object_info(ds->ds_dir->dd_pool->dp_meta_objset, 2026 ds->ds_phys->ds_snapnames_zapobj, &doi); 2027 if (err) { 2028 dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 2029 return (err); 2030 } 2031 2032 /* 2033 * Add in 128x the snapnames zapobj size, since we will be moving 2034 * a bunch of snapnames to the promoted ds, and dirtying their 2035 * bonus buffers. 2036 */ 2037 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 2038 dsl_dataset_promote_check, 2039 dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks); 2040 dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 2041 return (err); 2042 } 2043 2044 /* 2045 * Given a pool name and a dataset object number in that pool, 2046 * return the name of that dataset. 2047 */ 2048 int 2049 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 2050 { 2051 spa_t *spa; 2052 dsl_pool_t *dp; 2053 dsl_dataset_t *ds = NULL; 2054 int error; 2055 2056 if ((error = spa_open(pname, &spa, FTAG)) != 0) 2057 return (error); 2058 dp = spa_get_dsl(spa); 2059 rw_enter(&dp->dp_config_rwlock, RW_READER); 2060 if ((error = dsl_dataset_open_obj(dp, obj, 2061 NULL, DS_MODE_NONE, FTAG, &ds)) != 0) { 2062 rw_exit(&dp->dp_config_rwlock); 2063 spa_close(spa, FTAG); 2064 return (error); 2065 } 2066 dsl_dataset_name(ds, buf); 2067 dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 2068 rw_exit(&dp->dp_config_rwlock); 2069 spa_close(spa, FTAG); 2070 2071 return (0); 2072 } 2073