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