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