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/dmu.h> 27 #include <sys/dmu_objset.h> 28 #include <sys/dmu_tx.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/dsl_deleg.h> 34 #include <sys/spa.h> 35 #include <sys/metaslab.h> 36 #include <sys/zap.h> 37 #include <sys/zio.h> 38 #include <sys/arc.h> 39 #include <sys/sunddi.h> 40 #include "zfs_namecheck.h" 41 42 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd); 43 static void dsl_dir_set_reservation_sync(void *arg1, void *arg2, 44 cred_t *cr, dmu_tx_t *tx); 45 46 47 /* ARGSUSED */ 48 static void 49 dsl_dir_evict(dmu_buf_t *db, void *arg) 50 { 51 dsl_dir_t *dd = arg; 52 dsl_pool_t *dp = dd->dd_pool; 53 int t; 54 55 for (t = 0; t < TXG_SIZE; t++) { 56 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t)); 57 ASSERT(dd->dd_tempreserved[t] == 0); 58 ASSERT(dd->dd_space_towrite[t] == 0); 59 } 60 61 if (dd->dd_parent) 62 dsl_dir_close(dd->dd_parent, dd); 63 64 spa_close(dd->dd_pool->dp_spa, dd); 65 66 /* 67 * The props callback list should be empty since they hold the 68 * dir open. 69 */ 70 list_destroy(&dd->dd_prop_cbs); 71 mutex_destroy(&dd->dd_lock); 72 kmem_free(dd, sizeof (dsl_dir_t)); 73 } 74 75 int 76 dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj, 77 const char *tail, void *tag, dsl_dir_t **ddp) 78 { 79 dmu_buf_t *dbuf; 80 dsl_dir_t *dd; 81 int err; 82 83 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 84 dsl_pool_sync_context(dp)); 85 86 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf); 87 if (err) 88 return (err); 89 dd = dmu_buf_get_user(dbuf); 90 #ifdef ZFS_DEBUG 91 { 92 dmu_object_info_t doi; 93 dmu_object_info_from_db(dbuf, &doi); 94 ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR); 95 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t)); 96 } 97 #endif 98 if (dd == NULL) { 99 dsl_dir_t *winner; 100 101 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP); 102 dd->dd_object = ddobj; 103 dd->dd_dbuf = dbuf; 104 dd->dd_pool = dp; 105 dd->dd_phys = dbuf->db_data; 106 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); 107 108 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t), 109 offsetof(dsl_prop_cb_record_t, cbr_node)); 110 111 dsl_dir_snap_cmtime_update(dd); 112 113 if (dd->dd_phys->dd_parent_obj) { 114 err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj, 115 NULL, dd, &dd->dd_parent); 116 if (err) 117 goto errout; 118 if (tail) { 119 #ifdef ZFS_DEBUG 120 uint64_t foundobj; 121 122 err = zap_lookup(dp->dp_meta_objset, 123 dd->dd_parent->dd_phys->dd_child_dir_zapobj, 124 tail, sizeof (foundobj), 1, &foundobj); 125 ASSERT(err || foundobj == ddobj); 126 #endif 127 (void) strcpy(dd->dd_myname, tail); 128 } else { 129 err = zap_value_search(dp->dp_meta_objset, 130 dd->dd_parent->dd_phys->dd_child_dir_zapobj, 131 ddobj, 0, dd->dd_myname); 132 } 133 if (err) 134 goto errout; 135 } else { 136 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa)); 137 } 138 139 winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys, 140 dsl_dir_evict); 141 if (winner) { 142 if (dd->dd_parent) 143 dsl_dir_close(dd->dd_parent, dd); 144 mutex_destroy(&dd->dd_lock); 145 kmem_free(dd, sizeof (dsl_dir_t)); 146 dd = winner; 147 } else { 148 spa_open_ref(dp->dp_spa, dd); 149 } 150 } 151 152 /* 153 * The dsl_dir_t has both open-to-close and instantiate-to-evict 154 * holds on the spa. We need the open-to-close holds because 155 * otherwise the spa_refcnt wouldn't change when we open a 156 * dir which the spa also has open, so we could incorrectly 157 * think it was OK to unload/export/destroy the pool. We need 158 * the instantiate-to-evict hold because the dsl_dir_t has a 159 * pointer to the dd_pool, which has a pointer to the spa_t. 160 */ 161 spa_open_ref(dp->dp_spa, tag); 162 ASSERT3P(dd->dd_pool, ==, dp); 163 ASSERT3U(dd->dd_object, ==, ddobj); 164 ASSERT3P(dd->dd_dbuf, ==, dbuf); 165 *ddp = dd; 166 return (0); 167 168 errout: 169 if (dd->dd_parent) 170 dsl_dir_close(dd->dd_parent, dd); 171 mutex_destroy(&dd->dd_lock); 172 kmem_free(dd, sizeof (dsl_dir_t)); 173 dmu_buf_rele(dbuf, tag); 174 return (err); 175 176 } 177 178 void 179 dsl_dir_close(dsl_dir_t *dd, void *tag) 180 { 181 dprintf_dd(dd, "%s\n", ""); 182 spa_close(dd->dd_pool->dp_spa, tag); 183 dmu_buf_rele(dd->dd_dbuf, tag); 184 } 185 186 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */ 187 void 188 dsl_dir_name(dsl_dir_t *dd, char *buf) 189 { 190 if (dd->dd_parent) { 191 dsl_dir_name(dd->dd_parent, buf); 192 (void) strcat(buf, "/"); 193 } else { 194 buf[0] = '\0'; 195 } 196 if (!MUTEX_HELD(&dd->dd_lock)) { 197 /* 198 * recursive mutex so that we can use 199 * dprintf_dd() with dd_lock held 200 */ 201 mutex_enter(&dd->dd_lock); 202 (void) strcat(buf, dd->dd_myname); 203 mutex_exit(&dd->dd_lock); 204 } else { 205 (void) strcat(buf, dd->dd_myname); 206 } 207 } 208 209 /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */ 210 int 211 dsl_dir_namelen(dsl_dir_t *dd) 212 { 213 int result = 0; 214 215 if (dd->dd_parent) { 216 /* parent's name + 1 for the "/" */ 217 result = dsl_dir_namelen(dd->dd_parent) + 1; 218 } 219 220 if (!MUTEX_HELD(&dd->dd_lock)) { 221 /* see dsl_dir_name */ 222 mutex_enter(&dd->dd_lock); 223 result += strlen(dd->dd_myname); 224 mutex_exit(&dd->dd_lock); 225 } else { 226 result += strlen(dd->dd_myname); 227 } 228 229 return (result); 230 } 231 232 static int 233 getcomponent(const char *path, char *component, const char **nextp) 234 { 235 char *p; 236 if ((path == NULL) || (path[0] == '\0')) 237 return (ENOENT); 238 /* This would be a good place to reserve some namespace... */ 239 p = strpbrk(path, "/@"); 240 if (p && (p[1] == '/' || p[1] == '@')) { 241 /* two separators in a row */ 242 return (EINVAL); 243 } 244 if (p == NULL || p == path) { 245 /* 246 * if the first thing is an @ or /, it had better be an 247 * @ and it had better not have any more ats or slashes, 248 * and it had better have something after the @. 249 */ 250 if (p != NULL && 251 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) 252 return (EINVAL); 253 if (strlen(path) >= MAXNAMELEN) 254 return (ENAMETOOLONG); 255 (void) strcpy(component, path); 256 p = NULL; 257 } else if (p[0] == '/') { 258 if (p-path >= MAXNAMELEN) 259 return (ENAMETOOLONG); 260 (void) strncpy(component, path, p - path); 261 component[p-path] = '\0'; 262 p++; 263 } else if (p[0] == '@') { 264 /* 265 * if the next separator is an @, there better not be 266 * any more slashes. 267 */ 268 if (strchr(path, '/')) 269 return (EINVAL); 270 if (p-path >= MAXNAMELEN) 271 return (ENAMETOOLONG); 272 (void) strncpy(component, path, p - path); 273 component[p-path] = '\0'; 274 } else { 275 ASSERT(!"invalid p"); 276 } 277 *nextp = p; 278 return (0); 279 } 280 281 /* 282 * same as dsl_open_dir, ignore the first component of name and use the 283 * spa instead 284 */ 285 int 286 dsl_dir_open_spa(spa_t *spa, const char *name, void *tag, 287 dsl_dir_t **ddp, const char **tailp) 288 { 289 char buf[MAXNAMELEN]; 290 const char *next, *nextnext = NULL; 291 int err; 292 dsl_dir_t *dd; 293 dsl_pool_t *dp; 294 uint64_t ddobj; 295 int openedspa = FALSE; 296 297 dprintf("%s\n", name); 298 299 err = getcomponent(name, buf, &next); 300 if (err) 301 return (err); 302 if (spa == NULL) { 303 err = spa_open(buf, &spa, FTAG); 304 if (err) { 305 dprintf("spa_open(%s) failed\n", buf); 306 return (err); 307 } 308 openedspa = TRUE; 309 310 /* XXX this assertion belongs in spa_open */ 311 ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa))); 312 } 313 314 dp = spa_get_dsl(spa); 315 316 rw_enter(&dp->dp_config_rwlock, RW_READER); 317 err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); 318 if (err) { 319 rw_exit(&dp->dp_config_rwlock); 320 if (openedspa) 321 spa_close(spa, FTAG); 322 return (err); 323 } 324 325 while (next != NULL) { 326 dsl_dir_t *child_ds; 327 err = getcomponent(next, buf, &nextnext); 328 if (err) 329 break; 330 ASSERT(next[0] != '\0'); 331 if (next[0] == '@') 332 break; 333 dprintf("looking up %s in obj%lld\n", 334 buf, dd->dd_phys->dd_child_dir_zapobj); 335 336 err = zap_lookup(dp->dp_meta_objset, 337 dd->dd_phys->dd_child_dir_zapobj, 338 buf, sizeof (ddobj), 1, &ddobj); 339 if (err) { 340 if (err == ENOENT) 341 err = 0; 342 break; 343 } 344 345 err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds); 346 if (err) 347 break; 348 dsl_dir_close(dd, tag); 349 dd = child_ds; 350 next = nextnext; 351 } 352 rw_exit(&dp->dp_config_rwlock); 353 354 if (err) { 355 dsl_dir_close(dd, tag); 356 if (openedspa) 357 spa_close(spa, FTAG); 358 return (err); 359 } 360 361 /* 362 * It's an error if there's more than one component left, or 363 * tailp==NULL and there's any component left. 364 */ 365 if (next != NULL && 366 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { 367 /* bad path name */ 368 dsl_dir_close(dd, tag); 369 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); 370 err = ENOENT; 371 } 372 if (tailp) 373 *tailp = next; 374 if (openedspa) 375 spa_close(spa, FTAG); 376 *ddp = dd; 377 return (err); 378 } 379 380 /* 381 * Return the dsl_dir_t, and possibly the last component which couldn't 382 * be found in *tail. Return NULL if the path is bogus, or if 383 * tail==NULL and we couldn't parse the whole name. (*tail)[0] == '@' 384 * means that the last component is a snapshot. 385 */ 386 int 387 dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp) 388 { 389 return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp)); 390 } 391 392 uint64_t 393 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, 394 dmu_tx_t *tx) 395 { 396 objset_t *mos = dp->dp_meta_objset; 397 uint64_t ddobj; 398 dsl_dir_phys_t *dsphys; 399 dmu_buf_t *dbuf; 400 401 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, 402 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); 403 if (pds) { 404 VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj, 405 name, sizeof (uint64_t), 1, &ddobj, tx)); 406 } else { 407 /* it's the root dir */ 408 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 409 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); 410 } 411 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); 412 dmu_buf_will_dirty(dbuf, tx); 413 dsphys = dbuf->db_data; 414 415 dsphys->dd_creation_time = gethrestime_sec(); 416 if (pds) 417 dsphys->dd_parent_obj = pds->dd_object; 418 dsphys->dd_props_zapobj = zap_create(mos, 419 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 420 dsphys->dd_child_dir_zapobj = zap_create(mos, 421 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); 422 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) 423 dsphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; 424 dmu_buf_rele(dbuf, FTAG); 425 426 return (ddobj); 427 } 428 429 /* ARGSUSED */ 430 int 431 dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 432 { 433 dsl_dir_t *dd = arg1; 434 dsl_pool_t *dp = dd->dd_pool; 435 objset_t *mos = dp->dp_meta_objset; 436 int err; 437 uint64_t count; 438 439 /* 440 * There should be exactly two holds, both from 441 * dsl_dataset_destroy: one on the dd directory, and one on its 442 * head ds. Otherwise, someone is trying to lookup something 443 * inside this dir while we want to destroy it. The 444 * config_rwlock ensures that nobody else opens it after we 445 * check. 446 */ 447 if (dmu_buf_refcount(dd->dd_dbuf) > 2) 448 return (EBUSY); 449 450 err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count); 451 if (err) 452 return (err); 453 if (count != 0) 454 return (EEXIST); 455 456 return (0); 457 } 458 459 void 460 dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 461 { 462 dsl_dir_t *dd = arg1; 463 objset_t *mos = dd->dd_pool->dp_meta_objset; 464 uint64_t val, obj; 465 dd_used_t t; 466 467 ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock)); 468 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 469 470 /* Remove our reservation. */ 471 val = 0; 472 dsl_dir_set_reservation_sync(dd, &val, cr, tx); 473 ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0); 474 ASSERT3U(dd->dd_phys->dd_reserved, ==, 0); 475 for (t = 0; t < DD_USED_NUM; t++) 476 ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0); 477 478 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx)); 479 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx)); 480 VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx)); 481 VERIFY(0 == zap_remove(mos, 482 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx)); 483 484 obj = dd->dd_object; 485 dsl_dir_close(dd, tag); 486 VERIFY(0 == dmu_object_free(mos, obj, tx)); 487 } 488 489 boolean_t 490 dsl_dir_is_clone(dsl_dir_t *dd) 491 { 492 return (dd->dd_phys->dd_origin_obj && 493 (dd->dd_pool->dp_origin_snap == NULL || 494 dd->dd_phys->dd_origin_obj != 495 dd->dd_pool->dp_origin_snap->ds_object)); 496 } 497 498 void 499 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) 500 { 501 mutex_enter(&dd->dd_lock); 502 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 503 dd->dd_phys->dd_used_bytes); 504 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota); 505 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, 506 dd->dd_phys->dd_reserved); 507 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 508 dd->dd_phys->dd_compressed_bytes == 0 ? 100 : 509 (dd->dd_phys->dd_uncompressed_bytes * 100 / 510 dd->dd_phys->dd_compressed_bytes)); 511 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 512 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, 513 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]); 514 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, 515 dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]); 516 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, 517 dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]); 518 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, 519 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] + 520 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]); 521 } 522 mutex_exit(&dd->dd_lock); 523 524 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 525 if (dsl_dir_is_clone(dd)) { 526 dsl_dataset_t *ds; 527 char buf[MAXNAMELEN]; 528 529 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 530 dd->dd_phys->dd_origin_obj, FTAG, &ds)); 531 dsl_dataset_name(ds, buf); 532 dsl_dataset_rele(ds, FTAG); 533 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); 534 } 535 rw_exit(&dd->dd_pool->dp_config_rwlock); 536 } 537 538 void 539 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) 540 { 541 dsl_pool_t *dp = dd->dd_pool; 542 543 ASSERT(dd->dd_phys); 544 545 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) { 546 /* up the hold count until we can be written out */ 547 dmu_buf_add_ref(dd->dd_dbuf, dd); 548 } 549 } 550 551 static int64_t 552 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) 553 { 554 uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved); 555 uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved); 556 return (new_accounted - old_accounted); 557 } 558 559 void 560 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) 561 { 562 ASSERT(dmu_tx_is_syncing(tx)); 563 564 dmu_buf_will_dirty(dd->dd_dbuf, tx); 565 566 mutex_enter(&dd->dd_lock); 567 ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0); 568 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg, 569 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024); 570 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0; 571 mutex_exit(&dd->dd_lock); 572 573 /* release the hold from dsl_dir_dirty */ 574 dmu_buf_rele(dd->dd_dbuf, dd); 575 } 576 577 static uint64_t 578 dsl_dir_space_towrite(dsl_dir_t *dd) 579 { 580 uint64_t space = 0; 581 int i; 582 583 ASSERT(MUTEX_HELD(&dd->dd_lock)); 584 585 for (i = 0; i < TXG_SIZE; i++) { 586 space += dd->dd_space_towrite[i&TXG_MASK]; 587 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0); 588 } 589 return (space); 590 } 591 592 /* 593 * How much space would dd have available if ancestor had delta applied 594 * to it? If ondiskonly is set, we're only interested in what's 595 * on-disk, not estimated pending changes. 596 */ 597 uint64_t 598 dsl_dir_space_available(dsl_dir_t *dd, 599 dsl_dir_t *ancestor, int64_t delta, int ondiskonly) 600 { 601 uint64_t parentspace, myspace, quota, used; 602 603 /* 604 * If there are no restrictions otherwise, assume we have 605 * unlimited space available. 606 */ 607 quota = UINT64_MAX; 608 parentspace = UINT64_MAX; 609 610 if (dd->dd_parent != NULL) { 611 parentspace = dsl_dir_space_available(dd->dd_parent, 612 ancestor, delta, ondiskonly); 613 } 614 615 mutex_enter(&dd->dd_lock); 616 if (dd->dd_phys->dd_quota != 0) 617 quota = dd->dd_phys->dd_quota; 618 used = dd->dd_phys->dd_used_bytes; 619 if (!ondiskonly) 620 used += dsl_dir_space_towrite(dd); 621 622 if (dd->dd_parent == NULL) { 623 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE); 624 quota = MIN(quota, poolsize); 625 } 626 627 if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) { 628 /* 629 * We have some space reserved, in addition to what our 630 * parent gave us. 631 */ 632 parentspace += dd->dd_phys->dd_reserved - used; 633 } 634 635 if (dd == ancestor) { 636 ASSERT(delta <= 0); 637 ASSERT(used >= -delta); 638 used += delta; 639 if (parentspace != UINT64_MAX) 640 parentspace -= delta; 641 } 642 643 if (used > quota) { 644 /* over quota */ 645 myspace = 0; 646 647 /* 648 * While it's OK to be a little over quota, if 649 * we think we are using more space than there 650 * is in the pool (which is already 1.6% more than 651 * dsl_pool_adjustedsize()), something is very 652 * wrong. 653 */ 654 ASSERT3U(used, <=, spa_get_dspace(dd->dd_pool->dp_spa)); 655 } else { 656 /* 657 * the lesser of the space provided by our parent and 658 * the space left in our quota 659 */ 660 myspace = MIN(parentspace, quota - used); 661 } 662 663 mutex_exit(&dd->dd_lock); 664 665 return (myspace); 666 } 667 668 struct tempreserve { 669 list_node_t tr_node; 670 dsl_pool_t *tr_dp; 671 dsl_dir_t *tr_ds; 672 uint64_t tr_size; 673 }; 674 675 static int 676 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, 677 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list, 678 dmu_tx_t *tx, boolean_t first) 679 { 680 uint64_t txg = tx->tx_txg; 681 uint64_t est_inflight, used_on_disk, quota, parent_rsrv; 682 uint64_t deferred = 0; 683 struct tempreserve *tr; 684 int retval = EDQUOT; 685 int txgidx = txg & TXG_MASK; 686 int i; 687 uint64_t ref_rsrv = 0; 688 689 ASSERT3U(txg, !=, 0); 690 ASSERT3S(asize, >, 0); 691 692 mutex_enter(&dd->dd_lock); 693 694 /* 695 * Check against the dsl_dir's quota. We don't add in the delta 696 * when checking for over-quota because they get one free hit. 697 */ 698 est_inflight = dsl_dir_space_towrite(dd); 699 for (i = 0; i < TXG_SIZE; i++) 700 est_inflight += dd->dd_tempreserved[i]; 701 used_on_disk = dd->dd_phys->dd_used_bytes; 702 703 /* 704 * On the first iteration, fetch the dataset's used-on-disk and 705 * refreservation values. Also, if checkrefquota is set, test if 706 * allocating this space would exceed the dataset's refquota. 707 */ 708 if (first && tx->tx_objset) { 709 int error; 710 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset; 711 712 error = dsl_dataset_check_quota(ds, checkrefquota, 713 asize, est_inflight, &used_on_disk, &ref_rsrv); 714 if (error) { 715 mutex_exit(&dd->dd_lock); 716 return (error); 717 } 718 } 719 720 /* 721 * If this transaction will result in a net free of space, 722 * we want to let it through. 723 */ 724 if (ignorequota || netfree || dd->dd_phys->dd_quota == 0) 725 quota = UINT64_MAX; 726 else 727 quota = dd->dd_phys->dd_quota; 728 729 /* 730 * Adjust the quota against the actual pool size at the root 731 * minus any outstanding deferred frees. 732 * To ensure that it's possible to remove files from a full 733 * pool without inducing transient overcommits, we throttle 734 * netfree transactions against a quota that is slightly larger, 735 * but still within the pool's allocation slop. In cases where 736 * we're very close to full, this will allow a steady trickle of 737 * removes to get through. 738 */ 739 if (dd->dd_parent == NULL) { 740 spa_t *spa = dd->dd_pool->dp_spa; 741 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree); 742 deferred = metaslab_class_get_deferred(spa_normal_class(spa)); 743 if (poolsize - deferred < quota) { 744 quota = poolsize - deferred; 745 retval = ENOSPC; 746 } 747 } 748 749 /* 750 * If they are requesting more space, and our current estimate 751 * is over quota, they get to try again unless the actual 752 * on-disk is over quota and there are no pending changes (which 753 * may free up space for us). 754 */ 755 if (used_on_disk + est_inflight >= quota) { 756 if (est_inflight > 0 || used_on_disk < quota || 757 (retval == ENOSPC && used_on_disk < quota + deferred)) 758 retval = ERESTART; 759 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " 760 "quota=%lluK tr=%lluK err=%d\n", 761 used_on_disk>>10, est_inflight>>10, 762 quota>>10, asize>>10, retval); 763 mutex_exit(&dd->dd_lock); 764 return (retval); 765 } 766 767 /* We need to up our estimated delta before dropping dd_lock */ 768 dd->dd_tempreserved[txgidx] += asize; 769 770 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, 771 asize - ref_rsrv); 772 mutex_exit(&dd->dd_lock); 773 774 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 775 tr->tr_ds = dd; 776 tr->tr_size = asize; 777 list_insert_tail(tr_list, tr); 778 779 /* see if it's OK with our parent */ 780 if (dd->dd_parent && parent_rsrv) { 781 boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0); 782 783 return (dsl_dir_tempreserve_impl(dd->dd_parent, 784 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE)); 785 } else { 786 return (0); 787 } 788 } 789 790 /* 791 * Reserve space in this dsl_dir, to be used in this tx's txg. 792 * After the space has been dirtied (and dsl_dir_willuse_space() 793 * has been called), the reservation should be canceled, using 794 * dsl_dir_tempreserve_clear(). 795 */ 796 int 797 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, 798 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx) 799 { 800 int err; 801 list_t *tr_list; 802 803 if (asize == 0) { 804 *tr_cookiep = NULL; 805 return (0); 806 } 807 808 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP); 809 list_create(tr_list, sizeof (struct tempreserve), 810 offsetof(struct tempreserve, tr_node)); 811 ASSERT3S(asize, >, 0); 812 ASSERT3S(fsize, >=, 0); 813 814 err = arc_tempreserve_space(lsize, tx->tx_txg); 815 if (err == 0) { 816 struct tempreserve *tr; 817 818 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 819 tr->tr_size = lsize; 820 list_insert_tail(tr_list, tr); 821 822 err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx); 823 } else { 824 if (err == EAGAIN) { 825 txg_delay(dd->dd_pool, tx->tx_txg, 1); 826 err = ERESTART; 827 } 828 dsl_pool_memory_pressure(dd->dd_pool); 829 } 830 831 if (err == 0) { 832 struct tempreserve *tr; 833 834 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 835 tr->tr_dp = dd->dd_pool; 836 tr->tr_size = asize; 837 list_insert_tail(tr_list, tr); 838 839 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize, 840 FALSE, asize > usize, tr_list, tx, TRUE); 841 } 842 843 if (err) 844 dsl_dir_tempreserve_clear(tr_list, tx); 845 else 846 *tr_cookiep = tr_list; 847 848 return (err); 849 } 850 851 /* 852 * Clear a temporary reservation that we previously made with 853 * dsl_dir_tempreserve_space(). 854 */ 855 void 856 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) 857 { 858 int txgidx = tx->tx_txg & TXG_MASK; 859 list_t *tr_list = tr_cookie; 860 struct tempreserve *tr; 861 862 ASSERT3U(tx->tx_txg, !=, 0); 863 864 if (tr_cookie == NULL) 865 return; 866 867 while (tr = list_head(tr_list)) { 868 if (tr->tr_dp) { 869 dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx); 870 } else if (tr->tr_ds) { 871 mutex_enter(&tr->tr_ds->dd_lock); 872 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, 873 tr->tr_size); 874 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; 875 mutex_exit(&tr->tr_ds->dd_lock); 876 } else { 877 arc_tempreserve_clear(tr->tr_size); 878 } 879 list_remove(tr_list, tr); 880 kmem_free(tr, sizeof (struct tempreserve)); 881 } 882 883 kmem_free(tr_list, sizeof (list_t)); 884 } 885 886 static void 887 dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 888 { 889 int64_t parent_space; 890 uint64_t est_used; 891 892 mutex_enter(&dd->dd_lock); 893 if (space > 0) 894 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; 895 896 est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes; 897 parent_space = parent_delta(dd, est_used, space); 898 mutex_exit(&dd->dd_lock); 899 900 /* Make sure that we clean up dd_space_to* */ 901 dsl_dir_dirty(dd, tx); 902 903 /* XXX this is potentially expensive and unnecessary... */ 904 if (parent_space && dd->dd_parent) 905 dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx); 906 } 907 908 /* 909 * Call in open context when we think we're going to write/free space, 910 * eg. when dirtying data. Be conservative (ie. OK to write less than 911 * this or free more than this, but don't write more or free less). 912 */ 913 void 914 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 915 { 916 dsl_pool_willuse_space(dd->dd_pool, space, tx); 917 dsl_dir_willuse_space_impl(dd, space, tx); 918 } 919 920 /* call from syncing context when we actually write/free space for this dd */ 921 void 922 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, 923 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) 924 { 925 int64_t accounted_delta; 926 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 927 928 ASSERT(dmu_tx_is_syncing(tx)); 929 ASSERT(type < DD_USED_NUM); 930 931 dsl_dir_dirty(dd, tx); 932 933 if (needlock) 934 mutex_enter(&dd->dd_lock); 935 accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used); 936 ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used); 937 ASSERT(compressed >= 0 || 938 dd->dd_phys->dd_compressed_bytes >= -compressed); 939 ASSERT(uncompressed >= 0 || 940 dd->dd_phys->dd_uncompressed_bytes >= -uncompressed); 941 dd->dd_phys->dd_used_bytes += used; 942 dd->dd_phys->dd_uncompressed_bytes += uncompressed; 943 dd->dd_phys->dd_compressed_bytes += compressed; 944 945 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 946 ASSERT(used > 0 || 947 dd->dd_phys->dd_used_breakdown[type] >= -used); 948 dd->dd_phys->dd_used_breakdown[type] += used; 949 #ifdef DEBUG 950 dd_used_t t; 951 uint64_t u = 0; 952 for (t = 0; t < DD_USED_NUM; t++) 953 u += dd->dd_phys->dd_used_breakdown[t]; 954 ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes); 955 #endif 956 } 957 if (needlock) 958 mutex_exit(&dd->dd_lock); 959 960 if (dd->dd_parent != NULL) { 961 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 962 accounted_delta, compressed, uncompressed, tx); 963 dsl_dir_transfer_space(dd->dd_parent, 964 used - accounted_delta, 965 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); 966 } 967 } 968 969 void 970 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, 971 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) 972 { 973 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 974 975 ASSERT(dmu_tx_is_syncing(tx)); 976 ASSERT(oldtype < DD_USED_NUM); 977 ASSERT(newtype < DD_USED_NUM); 978 979 if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN)) 980 return; 981 982 dsl_dir_dirty(dd, tx); 983 if (needlock) 984 mutex_enter(&dd->dd_lock); 985 ASSERT(delta > 0 ? 986 dd->dd_phys->dd_used_breakdown[oldtype] >= delta : 987 dd->dd_phys->dd_used_breakdown[newtype] >= -delta); 988 ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta)); 989 dd->dd_phys->dd_used_breakdown[oldtype] -= delta; 990 dd->dd_phys->dd_used_breakdown[newtype] += delta; 991 if (needlock) 992 mutex_exit(&dd->dd_lock); 993 } 994 995 static int 996 dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 997 { 998 dsl_dir_t *dd = arg1; 999 uint64_t *quotap = arg2; 1000 uint64_t new_quota = *quotap; 1001 int err = 0; 1002 uint64_t towrite; 1003 1004 if (new_quota == 0) 1005 return (0); 1006 1007 mutex_enter(&dd->dd_lock); 1008 /* 1009 * If we are doing the preliminary check in open context, and 1010 * there are pending changes, then don't fail it, since the 1011 * pending changes could under-estimate the amount of space to be 1012 * freed up. 1013 */ 1014 towrite = dsl_dir_space_towrite(dd); 1015 if ((dmu_tx_is_syncing(tx) || towrite == 0) && 1016 (new_quota < dd->dd_phys->dd_reserved || 1017 new_quota < dd->dd_phys->dd_used_bytes + towrite)) { 1018 err = ENOSPC; 1019 } 1020 mutex_exit(&dd->dd_lock); 1021 return (err); 1022 } 1023 1024 /* ARGSUSED */ 1025 static void 1026 dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1027 { 1028 dsl_dir_t *dd = arg1; 1029 uint64_t *quotap = arg2; 1030 uint64_t new_quota = *quotap; 1031 1032 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1033 1034 mutex_enter(&dd->dd_lock); 1035 dd->dd_phys->dd_quota = new_quota; 1036 mutex_exit(&dd->dd_lock); 1037 1038 spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa, 1039 tx, cr, "%lld dataset = %llu ", 1040 (longlong_t)new_quota, dd->dd_phys->dd_head_dataset_obj); 1041 } 1042 1043 int 1044 dsl_dir_set_quota(const char *ddname, uint64_t quota) 1045 { 1046 dsl_dir_t *dd; 1047 int err; 1048 1049 err = dsl_dir_open(ddname, FTAG, &dd, NULL); 1050 if (err) 1051 return (err); 1052 1053 if (quota != dd->dd_phys->dd_quota) { 1054 /* 1055 * If someone removes a file, then tries to set the quota, we 1056 * want to make sure the file freeing takes effect. 1057 */ 1058 txg_wait_open(dd->dd_pool, 0); 1059 1060 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check, 1061 dsl_dir_set_quota_sync, dd, "a, 0); 1062 } 1063 dsl_dir_close(dd, FTAG); 1064 return (err); 1065 } 1066 1067 int 1068 dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 1069 { 1070 dsl_dir_t *dd = arg1; 1071 uint64_t *reservationp = arg2; 1072 uint64_t new_reservation = *reservationp; 1073 uint64_t used, avail; 1074 1075 /* 1076 * If we are doing the preliminary check in open context, the 1077 * space estimates may be inaccurate. 1078 */ 1079 if (!dmu_tx_is_syncing(tx)) 1080 return (0); 1081 1082 mutex_enter(&dd->dd_lock); 1083 used = dd->dd_phys->dd_used_bytes; 1084 mutex_exit(&dd->dd_lock); 1085 1086 if (dd->dd_parent) { 1087 avail = dsl_dir_space_available(dd->dd_parent, 1088 NULL, 0, FALSE); 1089 } else { 1090 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used; 1091 } 1092 1093 if (MAX(used, new_reservation) > MAX(used, dd->dd_phys->dd_reserved)) { 1094 uint64_t delta = MAX(used, new_reservation) - 1095 MAX(used, dd->dd_phys->dd_reserved); 1096 1097 if (delta > avail) 1098 return (ENOSPC); 1099 if (dd->dd_phys->dd_quota > 0 && 1100 new_reservation > dd->dd_phys->dd_quota) 1101 return (ENOSPC); 1102 } 1103 1104 return (0); 1105 } 1106 1107 /* ARGSUSED */ 1108 static void 1109 dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1110 { 1111 dsl_dir_t *dd = arg1; 1112 uint64_t *reservationp = arg2; 1113 uint64_t new_reservation = *reservationp; 1114 uint64_t used; 1115 int64_t delta; 1116 1117 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1118 1119 mutex_enter(&dd->dd_lock); 1120 used = dd->dd_phys->dd_used_bytes; 1121 delta = MAX(used, new_reservation) - 1122 MAX(used, dd->dd_phys->dd_reserved); 1123 dd->dd_phys->dd_reserved = new_reservation; 1124 1125 if (dd->dd_parent != NULL) { 1126 /* Roll up this additional usage into our ancestors */ 1127 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 1128 delta, 0, 0, tx); 1129 } 1130 mutex_exit(&dd->dd_lock); 1131 1132 spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa, 1133 tx, cr, "%lld dataset = %llu", 1134 (longlong_t)new_reservation, dd->dd_phys->dd_head_dataset_obj); 1135 } 1136 1137 int 1138 dsl_dir_set_reservation(const char *ddname, uint64_t reservation) 1139 { 1140 dsl_dir_t *dd; 1141 int err; 1142 1143 err = dsl_dir_open(ddname, FTAG, &dd, NULL); 1144 if (err) 1145 return (err); 1146 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check, 1147 dsl_dir_set_reservation_sync, dd, &reservation, 0); 1148 dsl_dir_close(dd, FTAG); 1149 return (err); 1150 } 1151 1152 static dsl_dir_t * 1153 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) 1154 { 1155 for (; ds1; ds1 = ds1->dd_parent) { 1156 dsl_dir_t *dd; 1157 for (dd = ds2; dd; dd = dd->dd_parent) { 1158 if (ds1 == dd) 1159 return (dd); 1160 } 1161 } 1162 return (NULL); 1163 } 1164 1165 /* 1166 * If delta is applied to dd, how much of that delta would be applied to 1167 * ancestor? Syncing context only. 1168 */ 1169 static int64_t 1170 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) 1171 { 1172 if (dd == ancestor) 1173 return (delta); 1174 1175 mutex_enter(&dd->dd_lock); 1176 delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta); 1177 mutex_exit(&dd->dd_lock); 1178 return (would_change(dd->dd_parent, delta, ancestor)); 1179 } 1180 1181 struct renamearg { 1182 dsl_dir_t *newparent; 1183 const char *mynewname; 1184 }; 1185 1186 /*ARGSUSED*/ 1187 static int 1188 dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 1189 { 1190 dsl_dir_t *dd = arg1; 1191 struct renamearg *ra = arg2; 1192 dsl_pool_t *dp = dd->dd_pool; 1193 objset_t *mos = dp->dp_meta_objset; 1194 int err; 1195 uint64_t val; 1196 1197 /* There should be 2 references: the open and the dirty */ 1198 if (dmu_buf_refcount(dd->dd_dbuf) > 2) 1199 return (EBUSY); 1200 1201 /* check for existing name */ 1202 err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj, 1203 ra->mynewname, 8, 1, &val); 1204 if (err == 0) 1205 return (EEXIST); 1206 if (err != ENOENT) 1207 return (err); 1208 1209 if (ra->newparent != dd->dd_parent) { 1210 /* is there enough space? */ 1211 uint64_t myspace = 1212 MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved); 1213 1214 /* no rename into our descendant */ 1215 if (closest_common_ancestor(dd, ra->newparent) == dd) 1216 return (EINVAL); 1217 1218 if (err = dsl_dir_transfer_possible(dd->dd_parent, 1219 ra->newparent, myspace)) 1220 return (err); 1221 } 1222 1223 return (0); 1224 } 1225 1226 static void 1227 dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1228 { 1229 dsl_dir_t *dd = arg1; 1230 struct renamearg *ra = arg2; 1231 dsl_pool_t *dp = dd->dd_pool; 1232 objset_t *mos = dp->dp_meta_objset; 1233 int err; 1234 1235 ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2); 1236 1237 if (ra->newparent != dd->dd_parent) { 1238 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 1239 -dd->dd_phys->dd_used_bytes, 1240 -dd->dd_phys->dd_compressed_bytes, 1241 -dd->dd_phys->dd_uncompressed_bytes, tx); 1242 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD, 1243 dd->dd_phys->dd_used_bytes, 1244 dd->dd_phys->dd_compressed_bytes, 1245 dd->dd_phys->dd_uncompressed_bytes, tx); 1246 1247 if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) { 1248 uint64_t unused_rsrv = dd->dd_phys->dd_reserved - 1249 dd->dd_phys->dd_used_bytes; 1250 1251 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 1252 -unused_rsrv, 0, 0, tx); 1253 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV, 1254 unused_rsrv, 0, 0, tx); 1255 } 1256 } 1257 1258 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1259 1260 /* remove from old parent zapobj */ 1261 err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj, 1262 dd->dd_myname, tx); 1263 ASSERT3U(err, ==, 0); 1264 1265 (void) strcpy(dd->dd_myname, ra->mynewname); 1266 dsl_dir_close(dd->dd_parent, dd); 1267 dd->dd_phys->dd_parent_obj = ra->newparent->dd_object; 1268 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, 1269 ra->newparent->dd_object, NULL, dd, &dd->dd_parent)); 1270 1271 /* add to new parent zapobj */ 1272 err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj, 1273 dd->dd_myname, 8, 1, &dd->dd_object, tx); 1274 ASSERT3U(err, ==, 0); 1275 1276 spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, 1277 tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj); 1278 } 1279 1280 int 1281 dsl_dir_rename(dsl_dir_t *dd, const char *newname) 1282 { 1283 struct renamearg ra; 1284 int err; 1285 1286 /* new parent should exist */ 1287 err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname); 1288 if (err) 1289 return (err); 1290 1291 /* can't rename to different pool */ 1292 if (dd->dd_pool != ra.newparent->dd_pool) { 1293 err = ENXIO; 1294 goto out; 1295 } 1296 1297 /* new name should not already exist */ 1298 if (ra.mynewname == NULL) { 1299 err = EEXIST; 1300 goto out; 1301 } 1302 1303 err = dsl_sync_task_do(dd->dd_pool, 1304 dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3); 1305 1306 out: 1307 dsl_dir_close(ra.newparent, FTAG); 1308 return (err); 1309 } 1310 1311 int 1312 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space) 1313 { 1314 dsl_dir_t *ancestor; 1315 int64_t adelta; 1316 uint64_t avail; 1317 1318 ancestor = closest_common_ancestor(sdd, tdd); 1319 adelta = would_change(sdd, -space, ancestor); 1320 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); 1321 if (avail < space) 1322 return (ENOSPC); 1323 1324 return (0); 1325 } 1326 1327 timestruc_t 1328 dsl_dir_snap_cmtime(dsl_dir_t *dd) 1329 { 1330 timestruc_t t; 1331 1332 mutex_enter(&dd->dd_lock); 1333 t = dd->dd_snap_cmtime; 1334 mutex_exit(&dd->dd_lock); 1335 1336 return (t); 1337 } 1338 1339 void 1340 dsl_dir_snap_cmtime_update(dsl_dir_t *dd) 1341 { 1342 timestruc_t t; 1343 1344 gethrestime(&t); 1345 mutex_enter(&dd->dd_lock); 1346 dd->dd_snap_cmtime = t; 1347 mutex_exit(&dd->dd_lock); 1348 } 1349