dsl_dir.c revision 282758
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) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>. 24 * All rights reserved. 25 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 26 * Copyright (c) 2014 Joyent, Inc. All rights reserved. 27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 28 */ 29 30#include <sys/dmu.h> 31#include <sys/dmu_objset.h> 32#include <sys/dmu_tx.h> 33#include <sys/dsl_dataset.h> 34#include <sys/dsl_dir.h> 35#include <sys/dsl_prop.h> 36#include <sys/dsl_synctask.h> 37#include <sys/dsl_deleg.h> 38#include <sys/dmu_impl.h> 39#include <sys/spa.h> 40#include <sys/metaslab.h> 41#include <sys/zap.h> 42#include <sys/zio.h> 43#include <sys/arc.h> 44#include <sys/sunddi.h> 45#include <sys/zvol.h> 46#ifdef _KERNEL 47#include <sys/zfs_vfsops.h> 48#endif 49#include <sys/zfeature.h> 50#include <sys/policy.h> 51#include <sys/zfs_znode.h> 52#include "zfs_namecheck.h" 53#include "zfs_prop.h" 54 55/* 56 * Filesystem and Snapshot Limits 57 * ------------------------------ 58 * 59 * These limits are used to restrict the number of filesystems and/or snapshots 60 * that can be created at a given level in the tree or below. A typical 61 * use-case is with a delegated dataset where the administrator wants to ensure 62 * that a user within the zone is not creating too many additional filesystems 63 * or snapshots, even though they're not exceeding their space quota. 64 * 65 * The filesystem and snapshot counts are stored as extensible properties. This 66 * capability is controlled by a feature flag and must be enabled to be used. 67 * Once enabled, the feature is not active until the first limit is set. At 68 * that point, future operations to create/destroy filesystems or snapshots 69 * will validate and update the counts. 70 * 71 * Because the count properties will not exist before the feature is active, 72 * the counts are updated when a limit is first set on an uninitialized 73 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes 74 * all of the nested filesystems/snapshots. Thus, a new leaf node has a 75 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and 76 * snapshot count properties on a node indicate uninitialized counts on that 77 * node.) When first setting a limit on an uninitialized node, the code starts 78 * at the filesystem with the new limit and descends into all sub-filesystems 79 * to add the count properties. 80 * 81 * In practice this is lightweight since a limit is typically set when the 82 * filesystem is created and thus has no children. Once valid, changing the 83 * limit value won't require a re-traversal since the counts are already valid. 84 * When recursively fixing the counts, if a node with a limit is encountered 85 * during the descent, the counts are known to be valid and there is no need to 86 * descend into that filesystem's children. The counts on filesystems above the 87 * one with the new limit will still be uninitialized, unless a limit is 88 * eventually set on one of those filesystems. The counts are always recursively 89 * updated when a limit is set on a dataset, unless there is already a limit. 90 * When a new limit value is set on a filesystem with an existing limit, it is 91 * possible for the new limit to be less than the current count at that level 92 * since a user who can change the limit is also allowed to exceed the limit. 93 * 94 * Once the feature is active, then whenever a filesystem or snapshot is 95 * created, the code recurses up the tree, validating the new count against the 96 * limit at each initialized level. In practice, most levels will not have a 97 * limit set. If there is a limit at any initialized level up the tree, the 98 * check must pass or the creation will fail. Likewise, when a filesystem or 99 * snapshot is destroyed, the counts are recursively adjusted all the way up 100 * the initizized nodes in the tree. Renaming a filesystem into different point 101 * in the tree will first validate, then update the counts on each branch up to 102 * the common ancestor. A receive will also validate the counts and then update 103 * them. 104 * 105 * An exception to the above behavior is that the limit is not enforced if the 106 * user has permission to modify the limit. This is primarily so that 107 * recursive snapshots in the global zone always work. We want to prevent a 108 * denial-of-service in which a lower level delegated dataset could max out its 109 * limit and thus block recursive snapshots from being taken in the global zone. 110 * Because of this, it is possible for the snapshot count to be over the limit 111 * and snapshots taken in the global zone could cause a lower level dataset to 112 * hit or exceed its limit. The administrator taking the global zone recursive 113 * snapshot should be aware of this side-effect and behave accordingly. 114 * For consistency, the filesystem limit is also not enforced if the user can 115 * modify the limit. 116 * 117 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check() 118 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in 119 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by 120 * dsl_dir_init_fs_ss_count(). 121 * 122 * There is a special case when we receive a filesystem that already exists. In 123 * this case a temporary clone name of %X is created (see dmu_recv_begin). We 124 * never update the filesystem counts for temporary clones. 125 * 126 * Likewise, we do not update the snapshot counts for temporary snapshots, 127 * such as those created by zfs diff. 128 */ 129 130extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd); 131 132static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd); 133 134/* ARGSUSED */ 135static void 136dsl_dir_evict(dmu_buf_t *db, void *arg) 137{ 138 dsl_dir_t *dd = arg; 139 dsl_pool_t *dp = dd->dd_pool; 140 int t; 141 142 for (t = 0; t < TXG_SIZE; t++) { 143 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t)); 144 ASSERT(dd->dd_tempreserved[t] == 0); 145 ASSERT(dd->dd_space_towrite[t] == 0); 146 } 147 148 if (dd->dd_parent) 149 dsl_dir_rele(dd->dd_parent, dd); 150 151 spa_close(dd->dd_pool->dp_spa, dd); 152 153 /* 154 * The props callback list should have been cleaned up by 155 * objset_evict(). 156 */ 157 list_destroy(&dd->dd_prop_cbs); 158 mutex_destroy(&dd->dd_lock); 159 kmem_free(dd, sizeof (dsl_dir_t)); 160} 161 162int 163dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj, 164 const char *tail, void *tag, dsl_dir_t **ddp) 165{ 166 dmu_buf_t *dbuf; 167 dsl_dir_t *dd; 168 int err; 169 170 ASSERT(dsl_pool_config_held(dp)); 171 172 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf); 173 if (err != 0) 174 return (err); 175 dd = dmu_buf_get_user(dbuf); 176#ifdef ZFS_DEBUG 177 { 178 dmu_object_info_t doi; 179 dmu_object_info_from_db(dbuf, &doi); 180 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR); 181 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t)); 182 } 183#endif 184 if (dd == NULL) { 185 dsl_dir_t *winner; 186 187 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP); 188 dd->dd_object = ddobj; 189 dd->dd_dbuf = dbuf; 190 dd->dd_pool = dp; 191 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); 192 193 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t), 194 offsetof(dsl_prop_cb_record_t, cbr_node)); 195 196 dsl_dir_snap_cmtime_update(dd); 197 198 if (dsl_dir_phys(dd)->dd_parent_obj) { 199 err = dsl_dir_hold_obj(dp, 200 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd, 201 &dd->dd_parent); 202 if (err != 0) 203 goto errout; 204 if (tail) { 205#ifdef ZFS_DEBUG 206 uint64_t foundobj; 207 208 err = zap_lookup(dp->dp_meta_objset, 209 dsl_dir_phys(dd->dd_parent)-> 210 dd_child_dir_zapobj, tail, 211 sizeof (foundobj), 1, &foundobj); 212 ASSERT(err || foundobj == ddobj); 213#endif 214 (void) strcpy(dd->dd_myname, tail); 215 } else { 216 err = zap_value_search(dp->dp_meta_objset, 217 dsl_dir_phys(dd->dd_parent)-> 218 dd_child_dir_zapobj, 219 ddobj, 0, dd->dd_myname); 220 } 221 if (err != 0) 222 goto errout; 223 } else { 224 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa)); 225 } 226 227 if (dsl_dir_is_clone(dd)) { 228 dmu_buf_t *origin_bonus; 229 dsl_dataset_phys_t *origin_phys; 230 231 /* 232 * We can't open the origin dataset, because 233 * that would require opening this dsl_dir. 234 * Just look at its phys directly instead. 235 */ 236 err = dmu_bonus_hold(dp->dp_meta_objset, 237 dsl_dir_phys(dd)->dd_origin_obj, FTAG, 238 &origin_bonus); 239 if (err != 0) 240 goto errout; 241 origin_phys = origin_bonus->db_data; 242 dd->dd_origin_txg = 243 origin_phys->ds_creation_txg; 244 dmu_buf_rele(origin_bonus, FTAG); 245 } 246 247 winner = dmu_buf_set_user_ie(dbuf, dd, dsl_dir_evict); 248 if (winner) { 249 if (dd->dd_parent) 250 dsl_dir_rele(dd->dd_parent, dd); 251 mutex_destroy(&dd->dd_lock); 252 kmem_free(dd, sizeof (dsl_dir_t)); 253 dd = winner; 254 } else { 255 spa_open_ref(dp->dp_spa, dd); 256 } 257 } 258 259 /* 260 * The dsl_dir_t has both open-to-close and instantiate-to-evict 261 * holds on the spa. We need the open-to-close holds because 262 * otherwise the spa_refcnt wouldn't change when we open a 263 * dir which the spa also has open, so we could incorrectly 264 * think it was OK to unload/export/destroy the pool. We need 265 * the instantiate-to-evict hold because the dsl_dir_t has a 266 * pointer to the dd_pool, which has a pointer to the spa_t. 267 */ 268 spa_open_ref(dp->dp_spa, tag); 269 ASSERT3P(dd->dd_pool, ==, dp); 270 ASSERT3U(dd->dd_object, ==, ddobj); 271 ASSERT3P(dd->dd_dbuf, ==, dbuf); 272 *ddp = dd; 273 return (0); 274 275errout: 276 if (dd->dd_parent) 277 dsl_dir_rele(dd->dd_parent, dd); 278 mutex_destroy(&dd->dd_lock); 279 kmem_free(dd, sizeof (dsl_dir_t)); 280 dmu_buf_rele(dbuf, tag); 281 return (err); 282} 283 284void 285dsl_dir_rele(dsl_dir_t *dd, void *tag) 286{ 287 dprintf_dd(dd, "%s\n", ""); 288 spa_close(dd->dd_pool->dp_spa, tag); 289 dmu_buf_rele(dd->dd_dbuf, tag); 290} 291 292/* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */ 293void 294dsl_dir_name(dsl_dir_t *dd, char *buf) 295{ 296 if (dd->dd_parent) { 297 dsl_dir_name(dd->dd_parent, buf); 298 (void) strcat(buf, "/"); 299 } else { 300 buf[0] = '\0'; 301 } 302 if (!MUTEX_HELD(&dd->dd_lock)) { 303 /* 304 * recursive mutex so that we can use 305 * dprintf_dd() with dd_lock held 306 */ 307 mutex_enter(&dd->dd_lock); 308 (void) strcat(buf, dd->dd_myname); 309 mutex_exit(&dd->dd_lock); 310 } else { 311 (void) strcat(buf, dd->dd_myname); 312 } 313} 314 315/* Calculate name length, avoiding all the strcat calls of dsl_dir_name */ 316int 317dsl_dir_namelen(dsl_dir_t *dd) 318{ 319 int result = 0; 320 321 if (dd->dd_parent) { 322 /* parent's name + 1 for the "/" */ 323 result = dsl_dir_namelen(dd->dd_parent) + 1; 324 } 325 326 if (!MUTEX_HELD(&dd->dd_lock)) { 327 /* see dsl_dir_name */ 328 mutex_enter(&dd->dd_lock); 329 result += strlen(dd->dd_myname); 330 mutex_exit(&dd->dd_lock); 331 } else { 332 result += strlen(dd->dd_myname); 333 } 334 335 return (result); 336} 337 338static int 339getcomponent(const char *path, char *component, const char **nextp) 340{ 341 char *p; 342 343 if ((path == NULL) || (path[0] == '\0')) 344 return (SET_ERROR(ENOENT)); 345 /* This would be a good place to reserve some namespace... */ 346 p = strpbrk(path, "/@"); 347 if (p && (p[1] == '/' || p[1] == '@')) { 348 /* two separators in a row */ 349 return (SET_ERROR(EINVAL)); 350 } 351 if (p == NULL || p == path) { 352 /* 353 * if the first thing is an @ or /, it had better be an 354 * @ and it had better not have any more ats or slashes, 355 * and it had better have something after the @. 356 */ 357 if (p != NULL && 358 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) 359 return (SET_ERROR(EINVAL)); 360 if (strlen(path) >= MAXNAMELEN) 361 return (SET_ERROR(ENAMETOOLONG)); 362 (void) strcpy(component, path); 363 p = NULL; 364 } else if (p[0] == '/') { 365 if (p - path >= MAXNAMELEN) 366 return (SET_ERROR(ENAMETOOLONG)); 367 (void) strncpy(component, path, p - path); 368 component[p - path] = '\0'; 369 p++; 370 } else if (p[0] == '@') { 371 /* 372 * if the next separator is an @, there better not be 373 * any more slashes. 374 */ 375 if (strchr(path, '/')) 376 return (SET_ERROR(EINVAL)); 377 if (p - path >= MAXNAMELEN) 378 return (SET_ERROR(ENAMETOOLONG)); 379 (void) strncpy(component, path, p - path); 380 component[p - path] = '\0'; 381 } else { 382 panic("invalid p=%p", (void *)p); 383 } 384 *nextp = p; 385 return (0); 386} 387 388/* 389 * Return the dsl_dir_t, and possibly the last component which couldn't 390 * be found in *tail. The name must be in the specified dsl_pool_t. This 391 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the 392 * path is bogus, or if tail==NULL and we couldn't parse the whole name. 393 * (*tail)[0] == '@' means that the last component is a snapshot. 394 */ 395int 396dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag, 397 dsl_dir_t **ddp, const char **tailp) 398{ 399 char buf[MAXNAMELEN]; 400 const char *spaname, *next, *nextnext = NULL; 401 int err; 402 dsl_dir_t *dd; 403 uint64_t ddobj; 404 405 err = getcomponent(name, buf, &next); 406 if (err != 0) 407 return (err); 408 409 /* Make sure the name is in the specified pool. */ 410 spaname = spa_name(dp->dp_spa); 411 if (strcmp(buf, spaname) != 0) 412 return (SET_ERROR(EXDEV)); 413 414 ASSERT(dsl_pool_config_held(dp)); 415 416 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); 417 if (err != 0) { 418 return (err); 419 } 420 421 while (next != NULL) { 422 dsl_dir_t *child_ds; 423 err = getcomponent(next, buf, &nextnext); 424 if (err != 0) 425 break; 426 ASSERT(next[0] != '\0'); 427 if (next[0] == '@') 428 break; 429 dprintf("looking up %s in obj%lld\n", 430 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj); 431 432 err = zap_lookup(dp->dp_meta_objset, 433 dsl_dir_phys(dd)->dd_child_dir_zapobj, 434 buf, sizeof (ddobj), 1, &ddobj); 435 if (err != 0) { 436 if (err == ENOENT) 437 err = 0; 438 break; 439 } 440 441 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_ds); 442 if (err != 0) 443 break; 444 dsl_dir_rele(dd, tag); 445 dd = child_ds; 446 next = nextnext; 447 } 448 449 if (err != 0) { 450 dsl_dir_rele(dd, tag); 451 return (err); 452 } 453 454 /* 455 * It's an error if there's more than one component left, or 456 * tailp==NULL and there's any component left. 457 */ 458 if (next != NULL && 459 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { 460 /* bad path name */ 461 dsl_dir_rele(dd, tag); 462 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); 463 err = SET_ERROR(ENOENT); 464 } 465 if (tailp != NULL) 466 *tailp = next; 467 *ddp = dd; 468 return (err); 469} 470 471/* 472 * If the counts are already initialized for this filesystem and its 473 * descendants then do nothing, otherwise initialize the counts. 474 * 475 * The counts on this filesystem, and those below, may be uninitialized due to 476 * either the use of a pre-existing pool which did not support the 477 * filesystem/snapshot limit feature, or one in which the feature had not yet 478 * been enabled. 479 * 480 * Recursively descend the filesystem tree and update the filesystem/snapshot 481 * counts on each filesystem below, then update the cumulative count on the 482 * current filesystem. If the filesystem already has a count set on it, 483 * then we know that its counts, and the counts on the filesystems below it, 484 * are already correct, so we don't have to update this filesystem. 485 */ 486static void 487dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx) 488{ 489 uint64_t my_fs_cnt = 0; 490 uint64_t my_ss_cnt = 0; 491 dsl_pool_t *dp = dd->dd_pool; 492 objset_t *os = dp->dp_meta_objset; 493 zap_cursor_t *zc; 494 zap_attribute_t *za; 495 dsl_dataset_t *ds; 496 497 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)); 498 ASSERT(dsl_pool_config_held(dp)); 499 ASSERT(dmu_tx_is_syncing(tx)); 500 501 dsl_dir_zapify(dd, tx); 502 503 /* 504 * If the filesystem count has already been initialized then we 505 * don't need to recurse down any further. 506 */ 507 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0) 508 return; 509 510 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); 511 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 512 513 /* Iterate my child dirs */ 514 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj); 515 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) { 516 dsl_dir_t *chld_dd; 517 uint64_t count; 518 519 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG, 520 &chld_dd)); 521 522 /* 523 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and 524 * temporary datasets. 525 */ 526 if (chld_dd->dd_myname[0] == '$' || 527 chld_dd->dd_myname[0] == '%') { 528 dsl_dir_rele(chld_dd, FTAG); 529 continue; 530 } 531 532 my_fs_cnt++; /* count this child */ 533 534 dsl_dir_init_fs_ss_count(chld_dd, tx); 535 536 VERIFY0(zap_lookup(os, chld_dd->dd_object, 537 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count)); 538 my_fs_cnt += count; 539 VERIFY0(zap_lookup(os, chld_dd->dd_object, 540 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count)); 541 my_ss_cnt += count; 542 543 dsl_dir_rele(chld_dd, FTAG); 544 } 545 zap_cursor_fini(zc); 546 /* Count my snapshots (we counted children's snapshots above) */ 547 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 548 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds)); 549 550 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj); 551 zap_cursor_retrieve(zc, za) == 0; 552 zap_cursor_advance(zc)) { 553 /* Don't count temporary snapshots */ 554 if (za->za_name[0] != '%') 555 my_ss_cnt++; 556 } 557 zap_cursor_fini(zc); 558 559 dsl_dataset_rele(ds, FTAG); 560 561 kmem_free(zc, sizeof (zap_cursor_t)); 562 kmem_free(za, sizeof (zap_attribute_t)); 563 564 /* we're in a sync task, update counts */ 565 dmu_buf_will_dirty(dd->dd_dbuf, tx); 566 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 567 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx)); 568 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 569 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx)); 570} 571 572static int 573dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx) 574{ 575 char *ddname = (char *)arg; 576 dsl_pool_t *dp = dmu_tx_pool(tx); 577 dsl_dataset_t *ds; 578 dsl_dir_t *dd; 579 int error; 580 581 error = dsl_dataset_hold(dp, ddname, FTAG, &ds); 582 if (error != 0) 583 return (error); 584 585 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 586 dsl_dataset_rele(ds, FTAG); 587 return (SET_ERROR(ENOTSUP)); 588 } 589 590 dd = ds->ds_dir; 591 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) && 592 dsl_dir_is_zapified(dd) && 593 zap_contains(dp->dp_meta_objset, dd->dd_object, 594 DD_FIELD_FILESYSTEM_COUNT) == 0) { 595 dsl_dataset_rele(ds, FTAG); 596 return (SET_ERROR(EALREADY)); 597 } 598 599 dsl_dataset_rele(ds, FTAG); 600 return (0); 601} 602 603static void 604dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx) 605{ 606 char *ddname = (char *)arg; 607 dsl_pool_t *dp = dmu_tx_pool(tx); 608 dsl_dataset_t *ds; 609 spa_t *spa; 610 611 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds)); 612 613 spa = dsl_dataset_get_spa(ds); 614 615 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) { 616 /* 617 * Since the feature was not active and we're now setting a 618 * limit, increment the feature-active counter so that the 619 * feature becomes active for the first time. 620 * 621 * We are already in a sync task so we can update the MOS. 622 */ 623 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx); 624 } 625 626 /* 627 * Since we are now setting a non-UINT64_MAX limit on the filesystem, 628 * we need to ensure the counts are correct. Descend down the tree from 629 * this point and update all of the counts to be accurate. 630 */ 631 dsl_dir_init_fs_ss_count(ds->ds_dir, tx); 632 633 dsl_dataset_rele(ds, FTAG); 634} 635 636/* 637 * Make sure the feature is enabled and activate it if necessary. 638 * Since we're setting a limit, ensure the on-disk counts are valid. 639 * This is only called by the ioctl path when setting a limit value. 640 * 641 * We do not need to validate the new limit, since users who can change the 642 * limit are also allowed to exceed the limit. 643 */ 644int 645dsl_dir_activate_fs_ss_limit(const char *ddname) 646{ 647 int error; 648 649 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check, 650 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0, 651 ZFS_SPACE_CHECK_RESERVED); 652 653 if (error == EALREADY) 654 error = 0; 655 656 return (error); 657} 658 659/* 660 * Used to determine if the filesystem_limit or snapshot_limit should be 661 * enforced. We allow the limit to be exceeded if the user has permission to 662 * write the property value. We pass in the creds that we got in the open 663 * context since we will always be the GZ root in syncing context. We also have 664 * to handle the case where we are allowed to change the limit on the current 665 * dataset, but there may be another limit in the tree above. 666 * 667 * We can never modify these two properties within a non-global zone. In 668 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We 669 * can't use that function since we are already holding the dp_config_rwlock. 670 * In addition, we already have the dd and dealing with snapshots is simplified 671 * in this code. 672 */ 673 674typedef enum { 675 ENFORCE_ALWAYS, 676 ENFORCE_NEVER, 677 ENFORCE_ABOVE 678} enforce_res_t; 679 680static enforce_res_t 681dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr) 682{ 683 enforce_res_t enforce = ENFORCE_ALWAYS; 684 uint64_t obj; 685 dsl_dataset_t *ds; 686 uint64_t zoned; 687 688 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || 689 prop == ZFS_PROP_SNAPSHOT_LIMIT); 690 691#ifdef _KERNEL 692#ifdef __FreeBSD__ 693 if (jailed(cr)) 694#else 695 if (crgetzoneid(cr) != GLOBAL_ZONEID) 696#endif 697 return (ENFORCE_ALWAYS); 698 699 if (secpolicy_zfs(cr) == 0) 700 return (ENFORCE_NEVER); 701#endif 702 703 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0) 704 return (ENFORCE_ALWAYS); 705 706 ASSERT(dsl_pool_config_held(dd->dd_pool)); 707 708 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0) 709 return (ENFORCE_ALWAYS); 710 711 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) { 712 /* Only root can access zoned fs's from the GZ */ 713 enforce = ENFORCE_ALWAYS; 714 } else { 715 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0) 716 enforce = ENFORCE_ABOVE; 717 } 718 719 dsl_dataset_rele(ds, FTAG); 720 return (enforce); 721} 722 723/* 724 * Check if adding additional child filesystem(s) would exceed any filesystem 725 * limits or adding additional snapshot(s) would exceed any snapshot limits. 726 * The prop argument indicates which limit to check. 727 * 728 * Note that all filesystem limits up to the root (or the highest 729 * initialized) filesystem or the given ancestor must be satisfied. 730 */ 731int 732dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop, 733 dsl_dir_t *ancestor, cred_t *cr) 734{ 735 objset_t *os = dd->dd_pool->dp_meta_objset; 736 uint64_t limit, count; 737 char *count_prop; 738 enforce_res_t enforce; 739 int err = 0; 740 741 ASSERT(dsl_pool_config_held(dd->dd_pool)); 742 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || 743 prop == ZFS_PROP_SNAPSHOT_LIMIT); 744 745 /* 746 * If we're allowed to change the limit, don't enforce the limit 747 * e.g. this can happen if a snapshot is taken by an administrative 748 * user in the global zone (i.e. a recursive snapshot by root). 749 * However, we must handle the case of delegated permissions where we 750 * are allowed to change the limit on the current dataset, but there 751 * is another limit in the tree above. 752 */ 753 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr); 754 if (enforce == ENFORCE_NEVER) 755 return (0); 756 757 /* 758 * e.g. if renaming a dataset with no snapshots, count adjustment 759 * is 0. 760 */ 761 if (delta == 0) 762 return (0); 763 764 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) { 765 /* 766 * We don't enforce the limit for temporary snapshots. This is 767 * indicated by a NULL cred_t argument. 768 */ 769 if (cr == NULL) 770 return (0); 771 772 count_prop = DD_FIELD_SNAPSHOT_COUNT; 773 } else { 774 count_prop = DD_FIELD_FILESYSTEM_COUNT; 775 } 776 777 /* 778 * If an ancestor has been provided, stop checking the limit once we 779 * hit that dir. We need this during rename so that we don't overcount 780 * the check once we recurse up to the common ancestor. 781 */ 782 if (ancestor == dd) 783 return (0); 784 785 /* 786 * If we hit an uninitialized node while recursing up the tree, we can 787 * stop since we know there is no limit here (or above). The counts are 788 * not valid on this node and we know we won't touch this node's counts. 789 */ 790 if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object, 791 count_prop, sizeof (count), 1, &count) == ENOENT) 792 return (0); 793 794 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL, 795 B_FALSE); 796 if (err != 0) 797 return (err); 798 799 /* Is there a limit which we've hit? */ 800 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit) 801 return (SET_ERROR(EDQUOT)); 802 803 if (dd->dd_parent != NULL) 804 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop, 805 ancestor, cr); 806 807 return (err); 808} 809 810/* 811 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all 812 * parents. When a new filesystem/snapshot is created, increment the count on 813 * all parents, and when a filesystem/snapshot is destroyed, decrement the 814 * count. 815 */ 816void 817dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop, 818 dmu_tx_t *tx) 819{ 820 int err; 821 objset_t *os = dd->dd_pool->dp_meta_objset; 822 uint64_t count; 823 824 ASSERT(dsl_pool_config_held(dd->dd_pool)); 825 ASSERT(dmu_tx_is_syncing(tx)); 826 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 || 827 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0); 828 829 /* 830 * When we receive an incremental stream into a filesystem that already 831 * exists, a temporary clone is created. We don't count this temporary 832 * clone, whose name begins with a '%'. We also ignore hidden ($FREE, 833 * $MOS & $ORIGIN) objsets. 834 */ 835 if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') && 836 strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0) 837 return; 838 839 /* 840 * e.g. if renaming a dataset with no snapshots, count adjustment is 0 841 */ 842 if (delta == 0) 843 return; 844 845 /* 846 * If we hit an uninitialized node while recursing up the tree, we can 847 * stop since we know the counts are not valid on this node and we 848 * know we shouldn't touch this node's counts. An uninitialized count 849 * on the node indicates that either the feature has not yet been 850 * activated or there are no limits on this part of the tree. 851 */ 852 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object, 853 prop, sizeof (count), 1, &count)) == ENOENT) 854 return; 855 VERIFY0(err); 856 857 count += delta; 858 /* Use a signed verify to make sure we're not neg. */ 859 VERIFY3S(count, >=, 0); 860 861 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count, 862 tx)); 863 864 /* Roll up this additional count into our ancestors */ 865 if (dd->dd_parent != NULL) 866 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx); 867} 868 869uint64_t 870dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, 871 dmu_tx_t *tx) 872{ 873 objset_t *mos = dp->dp_meta_objset; 874 uint64_t ddobj; 875 dsl_dir_phys_t *ddphys; 876 dmu_buf_t *dbuf; 877 878 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, 879 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); 880 if (pds) { 881 VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj, 882 name, sizeof (uint64_t), 1, &ddobj, tx)); 883 } else { 884 /* it's the root dir */ 885 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 886 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); 887 } 888 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); 889 dmu_buf_will_dirty(dbuf, tx); 890 ddphys = dbuf->db_data; 891 892 ddphys->dd_creation_time = gethrestime_sec(); 893 if (pds) { 894 ddphys->dd_parent_obj = pds->dd_object; 895 896 /* update the filesystem counts */ 897 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx); 898 } 899 ddphys->dd_props_zapobj = zap_create(mos, 900 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 901 ddphys->dd_child_dir_zapobj = zap_create(mos, 902 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); 903 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) 904 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; 905 dmu_buf_rele(dbuf, FTAG); 906 907 return (ddobj); 908} 909 910boolean_t 911dsl_dir_is_clone(dsl_dir_t *dd) 912{ 913 return (dsl_dir_phys(dd)->dd_origin_obj && 914 (dd->dd_pool->dp_origin_snap == NULL || 915 dsl_dir_phys(dd)->dd_origin_obj != 916 dd->dd_pool->dp_origin_snap->ds_object)); 917} 918 919void 920dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) 921{ 922 mutex_enter(&dd->dd_lock); 923 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 924 dsl_dir_phys(dd)->dd_used_bytes); 925 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, 926 dsl_dir_phys(dd)->dd_quota); 927 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, 928 dsl_dir_phys(dd)->dd_reserved); 929 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 930 dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 : 931 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 / 932 dsl_dir_phys(dd)->dd_compressed_bytes)); 933 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED, 934 dsl_dir_phys(dd)->dd_uncompressed_bytes); 935 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 936 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, 937 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]); 938 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, 939 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]); 940 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, 941 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]); 942 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, 943 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] + 944 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]); 945 } 946 mutex_exit(&dd->dd_lock); 947 948 if (dsl_dir_is_zapified(dd)) { 949 uint64_t count; 950 objset_t *os = dd->dd_pool->dp_meta_objset; 951 952 if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 953 sizeof (count), 1, &count) == 0) { 954 dsl_prop_nvlist_add_uint64(nv, 955 ZFS_PROP_FILESYSTEM_COUNT, count); 956 } 957 if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 958 sizeof (count), 1, &count) == 0) { 959 dsl_prop_nvlist_add_uint64(nv, 960 ZFS_PROP_SNAPSHOT_COUNT, count); 961 } 962 } 963 964 if (dsl_dir_is_clone(dd)) { 965 dsl_dataset_t *ds; 966 char buf[MAXNAMELEN]; 967 968 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 969 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds)); 970 dsl_dataset_name(ds, buf); 971 dsl_dataset_rele(ds, FTAG); 972 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); 973 } 974} 975 976void 977dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) 978{ 979 dsl_pool_t *dp = dd->dd_pool; 980 981 ASSERT(dsl_dir_phys(dd)); 982 983 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) { 984 /* up the hold count until we can be written out */ 985 dmu_buf_add_ref(dd->dd_dbuf, dd); 986 } 987} 988 989static int64_t 990parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) 991{ 992 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved); 993 uint64_t new_accounted = 994 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved); 995 return (new_accounted - old_accounted); 996} 997 998void 999dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) 1000{ 1001 ASSERT(dmu_tx_is_syncing(tx)); 1002 1003 mutex_enter(&dd->dd_lock); 1004 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]); 1005 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg, 1006 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024); 1007 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0; 1008 mutex_exit(&dd->dd_lock); 1009 1010 /* release the hold from dsl_dir_dirty */ 1011 dmu_buf_rele(dd->dd_dbuf, dd); 1012} 1013 1014static uint64_t 1015dsl_dir_space_towrite(dsl_dir_t *dd) 1016{ 1017 uint64_t space = 0; 1018 int i; 1019 1020 ASSERT(MUTEX_HELD(&dd->dd_lock)); 1021 1022 for (i = 0; i < TXG_SIZE; i++) { 1023 space += dd->dd_space_towrite[i&TXG_MASK]; 1024 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0); 1025 } 1026 return (space); 1027} 1028 1029/* 1030 * How much space would dd have available if ancestor had delta applied 1031 * to it? If ondiskonly is set, we're only interested in what's 1032 * on-disk, not estimated pending changes. 1033 */ 1034uint64_t 1035dsl_dir_space_available(dsl_dir_t *dd, 1036 dsl_dir_t *ancestor, int64_t delta, int ondiskonly) 1037{ 1038 uint64_t parentspace, myspace, quota, used; 1039 1040 /* 1041 * If there are no restrictions otherwise, assume we have 1042 * unlimited space available. 1043 */ 1044 quota = UINT64_MAX; 1045 parentspace = UINT64_MAX; 1046 1047 if (dd->dd_parent != NULL) { 1048 parentspace = dsl_dir_space_available(dd->dd_parent, 1049 ancestor, delta, ondiskonly); 1050 } 1051 1052 mutex_enter(&dd->dd_lock); 1053 if (dsl_dir_phys(dd)->dd_quota != 0) 1054 quota = dsl_dir_phys(dd)->dd_quota; 1055 used = dsl_dir_phys(dd)->dd_used_bytes; 1056 if (!ondiskonly) 1057 used += dsl_dir_space_towrite(dd); 1058 1059 if (dd->dd_parent == NULL) { 1060 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE); 1061 quota = MIN(quota, poolsize); 1062 } 1063 1064 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) { 1065 /* 1066 * We have some space reserved, in addition to what our 1067 * parent gave us. 1068 */ 1069 parentspace += dsl_dir_phys(dd)->dd_reserved - used; 1070 } 1071 1072 if (dd == ancestor) { 1073 ASSERT(delta <= 0); 1074 ASSERT(used >= -delta); 1075 used += delta; 1076 if (parentspace != UINT64_MAX) 1077 parentspace -= delta; 1078 } 1079 1080 if (used > quota) { 1081 /* over quota */ 1082 myspace = 0; 1083 } else { 1084 /* 1085 * the lesser of the space provided by our parent and 1086 * the space left in our quota 1087 */ 1088 myspace = MIN(parentspace, quota - used); 1089 } 1090 1091 mutex_exit(&dd->dd_lock); 1092 1093 return (myspace); 1094} 1095 1096struct tempreserve { 1097 list_node_t tr_node; 1098 dsl_dir_t *tr_ds; 1099 uint64_t tr_size; 1100}; 1101 1102static int 1103dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, 1104 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list, 1105 dmu_tx_t *tx, boolean_t first) 1106{ 1107 uint64_t txg = tx->tx_txg; 1108 uint64_t est_inflight, used_on_disk, quota, parent_rsrv; 1109 uint64_t deferred = 0; 1110 struct tempreserve *tr; 1111 int retval = EDQUOT; 1112 int txgidx = txg & TXG_MASK; 1113 int i; 1114 uint64_t ref_rsrv = 0; 1115 1116 ASSERT3U(txg, !=, 0); 1117 ASSERT3S(asize, >, 0); 1118 1119 mutex_enter(&dd->dd_lock); 1120 1121 /* 1122 * Check against the dsl_dir's quota. We don't add in the delta 1123 * when checking for over-quota because they get one free hit. 1124 */ 1125 est_inflight = dsl_dir_space_towrite(dd); 1126 for (i = 0; i < TXG_SIZE; i++) 1127 est_inflight += dd->dd_tempreserved[i]; 1128 used_on_disk = dsl_dir_phys(dd)->dd_used_bytes; 1129 1130 /* 1131 * On the first iteration, fetch the dataset's used-on-disk and 1132 * refreservation values. Also, if checkrefquota is set, test if 1133 * allocating this space would exceed the dataset's refquota. 1134 */ 1135 if (first && tx->tx_objset) { 1136 int error; 1137 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset; 1138 1139 error = dsl_dataset_check_quota(ds, checkrefquota, 1140 asize, est_inflight, &used_on_disk, &ref_rsrv); 1141 if (error) { 1142 mutex_exit(&dd->dd_lock); 1143 return (error); 1144 } 1145 } 1146 1147 /* 1148 * If this transaction will result in a net free of space, 1149 * we want to let it through. 1150 */ 1151 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0) 1152 quota = UINT64_MAX; 1153 else 1154 quota = dsl_dir_phys(dd)->dd_quota; 1155 1156 /* 1157 * Adjust the quota against the actual pool size at the root 1158 * minus any outstanding deferred frees. 1159 * To ensure that it's possible to remove files from a full 1160 * pool without inducing transient overcommits, we throttle 1161 * netfree transactions against a quota that is slightly larger, 1162 * but still within the pool's allocation slop. In cases where 1163 * we're very close to full, this will allow a steady trickle of 1164 * removes to get through. 1165 */ 1166 if (dd->dd_parent == NULL) { 1167 spa_t *spa = dd->dd_pool->dp_spa; 1168 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree); 1169 deferred = metaslab_class_get_deferred(spa_normal_class(spa)); 1170 if (poolsize - deferred < quota) { 1171 quota = poolsize - deferred; 1172 retval = ENOSPC; 1173 } 1174 } 1175 1176 /* 1177 * If they are requesting more space, and our current estimate 1178 * is over quota, they get to try again unless the actual 1179 * on-disk is over quota and there are no pending changes (which 1180 * may free up space for us). 1181 */ 1182 if (used_on_disk + est_inflight >= quota) { 1183 if (est_inflight > 0 || used_on_disk < quota || 1184 (retval == ENOSPC && used_on_disk < quota + deferred)) 1185 retval = ERESTART; 1186 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " 1187 "quota=%lluK tr=%lluK err=%d\n", 1188 used_on_disk>>10, est_inflight>>10, 1189 quota>>10, asize>>10, retval); 1190 mutex_exit(&dd->dd_lock); 1191 return (SET_ERROR(retval)); 1192 } 1193 1194 /* We need to up our estimated delta before dropping dd_lock */ 1195 dd->dd_tempreserved[txgidx] += asize; 1196 1197 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, 1198 asize - ref_rsrv); 1199 mutex_exit(&dd->dd_lock); 1200 1201 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 1202 tr->tr_ds = dd; 1203 tr->tr_size = asize; 1204 list_insert_tail(tr_list, tr); 1205 1206 /* see if it's OK with our parent */ 1207 if (dd->dd_parent && parent_rsrv) { 1208 boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 1209 1210 return (dsl_dir_tempreserve_impl(dd->dd_parent, 1211 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE)); 1212 } else { 1213 return (0); 1214 } 1215} 1216 1217/* 1218 * Reserve space in this dsl_dir, to be used in this tx's txg. 1219 * After the space has been dirtied (and dsl_dir_willuse_space() 1220 * has been called), the reservation should be canceled, using 1221 * dsl_dir_tempreserve_clear(). 1222 */ 1223int 1224dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, 1225 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx) 1226{ 1227 int err; 1228 list_t *tr_list; 1229 1230 if (asize == 0) { 1231 *tr_cookiep = NULL; 1232 return (0); 1233 } 1234 1235 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP); 1236 list_create(tr_list, sizeof (struct tempreserve), 1237 offsetof(struct tempreserve, tr_node)); 1238 ASSERT3S(asize, >, 0); 1239 ASSERT3S(fsize, >=, 0); 1240 1241 err = arc_tempreserve_space(lsize, tx->tx_txg); 1242 if (err == 0) { 1243 struct tempreserve *tr; 1244 1245 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 1246 tr->tr_size = lsize; 1247 list_insert_tail(tr_list, tr); 1248 } else { 1249 if (err == EAGAIN) { 1250 /* 1251 * If arc_memory_throttle() detected that pageout 1252 * is running and we are low on memory, we delay new 1253 * non-pageout transactions to give pageout an 1254 * advantage. 1255 * 1256 * It is unfortunate to be delaying while the caller's 1257 * locks are held. 1258 */ 1259 txg_delay(dd->dd_pool, tx->tx_txg, 1260 MSEC2NSEC(10), MSEC2NSEC(10)); 1261 err = SET_ERROR(ERESTART); 1262 } 1263 } 1264 1265 if (err == 0) { 1266 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize, 1267 FALSE, asize > usize, tr_list, tx, TRUE); 1268 } 1269 1270 if (err != 0) 1271 dsl_dir_tempreserve_clear(tr_list, tx); 1272 else 1273 *tr_cookiep = tr_list; 1274 1275 return (err); 1276} 1277 1278/* 1279 * Clear a temporary reservation that we previously made with 1280 * dsl_dir_tempreserve_space(). 1281 */ 1282void 1283dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) 1284{ 1285 int txgidx = tx->tx_txg & TXG_MASK; 1286 list_t *tr_list = tr_cookie; 1287 struct tempreserve *tr; 1288 1289 ASSERT3U(tx->tx_txg, !=, 0); 1290 1291 if (tr_cookie == NULL) 1292 return; 1293 1294 while ((tr = list_head(tr_list)) != NULL) { 1295 if (tr->tr_ds) { 1296 mutex_enter(&tr->tr_ds->dd_lock); 1297 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, 1298 tr->tr_size); 1299 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; 1300 mutex_exit(&tr->tr_ds->dd_lock); 1301 } else { 1302 arc_tempreserve_clear(tr->tr_size); 1303 } 1304 list_remove(tr_list, tr); 1305 kmem_free(tr, sizeof (struct tempreserve)); 1306 } 1307 1308 kmem_free(tr_list, sizeof (list_t)); 1309} 1310 1311/* 1312 * This should be called from open context when we think we're going to write 1313 * or free space, for example when dirtying data. Be conservative; it's okay 1314 * to write less space or free more, but we don't want to write more or free 1315 * less than the amount specified. 1316 */ 1317void 1318dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 1319{ 1320 int64_t parent_space; 1321 uint64_t est_used; 1322 1323 mutex_enter(&dd->dd_lock); 1324 if (space > 0) 1325 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; 1326 1327 est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes; 1328 parent_space = parent_delta(dd, est_used, space); 1329 mutex_exit(&dd->dd_lock); 1330 1331 /* Make sure that we clean up dd_space_to* */ 1332 dsl_dir_dirty(dd, tx); 1333 1334 /* XXX this is potentially expensive and unnecessary... */ 1335 if (parent_space && dd->dd_parent) 1336 dsl_dir_willuse_space(dd->dd_parent, parent_space, tx); 1337} 1338 1339/* call from syncing context when we actually write/free space for this dd */ 1340void 1341dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, 1342 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) 1343{ 1344 int64_t accounted_delta; 1345 1346 /* 1347 * dsl_dataset_set_refreservation_sync_impl() calls this with 1348 * dd_lock held, so that it can atomically update 1349 * ds->ds_reserved and the dsl_dir accounting, so that 1350 * dsl_dataset_check_quota() can see dataset and dir accounting 1351 * consistently. 1352 */ 1353 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 1354 1355 ASSERT(dmu_tx_is_syncing(tx)); 1356 ASSERT(type < DD_USED_NUM); 1357 1358 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1359 1360 if (needlock) 1361 mutex_enter(&dd->dd_lock); 1362 accounted_delta = 1363 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used); 1364 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used); 1365 ASSERT(compressed >= 0 || 1366 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed); 1367 ASSERT(uncompressed >= 0 || 1368 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed); 1369 dsl_dir_phys(dd)->dd_used_bytes += used; 1370 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed; 1371 dsl_dir_phys(dd)->dd_compressed_bytes += compressed; 1372 1373 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 1374 ASSERT(used > 0 || 1375 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used); 1376 dsl_dir_phys(dd)->dd_used_breakdown[type] += used; 1377#ifdef DEBUG 1378 dd_used_t t; 1379 uint64_t u = 0; 1380 for (t = 0; t < DD_USED_NUM; t++) 1381 u += dsl_dir_phys(dd)->dd_used_breakdown[t]; 1382 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes); 1383#endif 1384 } 1385 if (needlock) 1386 mutex_exit(&dd->dd_lock); 1387 1388 if (dd->dd_parent != NULL) { 1389 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 1390 accounted_delta, compressed, uncompressed, tx); 1391 dsl_dir_transfer_space(dd->dd_parent, 1392 used - accounted_delta, 1393 DD_USED_CHILD_RSRV, DD_USED_CHILD, NULL); 1394 } 1395} 1396 1397void 1398dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, 1399 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) 1400{ 1401 ASSERT(tx == NULL || dmu_tx_is_syncing(tx)); 1402 ASSERT(oldtype < DD_USED_NUM); 1403 ASSERT(newtype < DD_USED_NUM); 1404 1405 if (delta == 0 || 1406 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN)) 1407 return; 1408 1409 if (tx != NULL) 1410 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1411 mutex_enter(&dd->dd_lock); 1412 ASSERT(delta > 0 ? 1413 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta : 1414 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta); 1415 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta)); 1416 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta; 1417 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta; 1418 mutex_exit(&dd->dd_lock); 1419} 1420 1421typedef struct dsl_dir_set_qr_arg { 1422 const char *ddsqra_name; 1423 zprop_source_t ddsqra_source; 1424 uint64_t ddsqra_value; 1425} dsl_dir_set_qr_arg_t; 1426 1427static int 1428dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx) 1429{ 1430 dsl_dir_set_qr_arg_t *ddsqra = arg; 1431 dsl_pool_t *dp = dmu_tx_pool(tx); 1432 dsl_dataset_t *ds; 1433 int error; 1434 uint64_t towrite, newval; 1435 1436 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 1437 if (error != 0) 1438 return (error); 1439 1440 error = dsl_prop_predict(ds->ds_dir, "quota", 1441 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 1442 if (error != 0) { 1443 dsl_dataset_rele(ds, FTAG); 1444 return (error); 1445 } 1446 1447 if (newval == 0) { 1448 dsl_dataset_rele(ds, FTAG); 1449 return (0); 1450 } 1451 1452 mutex_enter(&ds->ds_dir->dd_lock); 1453 /* 1454 * If we are doing the preliminary check in open context, and 1455 * there are pending changes, then don't fail it, since the 1456 * pending changes could under-estimate the amount of space to be 1457 * freed up. 1458 */ 1459 towrite = dsl_dir_space_towrite(ds->ds_dir); 1460 if ((dmu_tx_is_syncing(tx) || towrite == 0) && 1461 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved || 1462 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) { 1463 error = SET_ERROR(ENOSPC); 1464 } 1465 mutex_exit(&ds->ds_dir->dd_lock); 1466 dsl_dataset_rele(ds, FTAG); 1467 return (error); 1468} 1469 1470static void 1471dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx) 1472{ 1473 dsl_dir_set_qr_arg_t *ddsqra = arg; 1474 dsl_pool_t *dp = dmu_tx_pool(tx); 1475 dsl_dataset_t *ds; 1476 uint64_t newval; 1477 1478 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 1479 1480 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { 1481 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA), 1482 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 1483 &ddsqra->ddsqra_value, tx); 1484 1485 VERIFY0(dsl_prop_get_int_ds(ds, 1486 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval)); 1487 } else { 1488 newval = ddsqra->ddsqra_value; 1489 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", 1490 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval); 1491 } 1492 1493 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 1494 mutex_enter(&ds->ds_dir->dd_lock); 1495 dsl_dir_phys(ds->ds_dir)->dd_quota = newval; 1496 mutex_exit(&ds->ds_dir->dd_lock); 1497 dsl_dataset_rele(ds, FTAG); 1498} 1499 1500int 1501dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota) 1502{ 1503 dsl_dir_set_qr_arg_t ddsqra; 1504 1505 ddsqra.ddsqra_name = ddname; 1506 ddsqra.ddsqra_source = source; 1507 ddsqra.ddsqra_value = quota; 1508 1509 return (dsl_sync_task(ddname, dsl_dir_set_quota_check, 1510 dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 1511} 1512 1513int 1514dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx) 1515{ 1516 dsl_dir_set_qr_arg_t *ddsqra = arg; 1517 dsl_pool_t *dp = dmu_tx_pool(tx); 1518 dsl_dataset_t *ds; 1519 dsl_dir_t *dd; 1520 uint64_t newval, used, avail; 1521 int error; 1522 1523 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 1524 if (error != 0) 1525 return (error); 1526 dd = ds->ds_dir; 1527 1528 /* 1529 * If we are doing the preliminary check in open context, the 1530 * space estimates may be inaccurate. 1531 */ 1532 if (!dmu_tx_is_syncing(tx)) { 1533 dsl_dataset_rele(ds, FTAG); 1534 return (0); 1535 } 1536 1537 error = dsl_prop_predict(ds->ds_dir, 1538 zfs_prop_to_name(ZFS_PROP_RESERVATION), 1539 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 1540 if (error != 0) { 1541 dsl_dataset_rele(ds, FTAG); 1542 return (error); 1543 } 1544 1545 mutex_enter(&dd->dd_lock); 1546 used = dsl_dir_phys(dd)->dd_used_bytes; 1547 mutex_exit(&dd->dd_lock); 1548 1549 if (dd->dd_parent) { 1550 avail = dsl_dir_space_available(dd->dd_parent, 1551 NULL, 0, FALSE); 1552 } else { 1553 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used; 1554 } 1555 1556 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) { 1557 uint64_t delta = MAX(used, newval) - 1558 MAX(used, dsl_dir_phys(dd)->dd_reserved); 1559 1560 if (delta > avail || 1561 (dsl_dir_phys(dd)->dd_quota > 0 && 1562 newval > dsl_dir_phys(dd)->dd_quota)) 1563 error = SET_ERROR(ENOSPC); 1564 } 1565 1566 dsl_dataset_rele(ds, FTAG); 1567 return (error); 1568} 1569 1570void 1571dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx) 1572{ 1573 uint64_t used; 1574 int64_t delta; 1575 1576 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1577 1578 mutex_enter(&dd->dd_lock); 1579 used = dsl_dir_phys(dd)->dd_used_bytes; 1580 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved); 1581 dsl_dir_phys(dd)->dd_reserved = value; 1582 1583 if (dd->dd_parent != NULL) { 1584 /* Roll up this additional usage into our ancestors */ 1585 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 1586 delta, 0, 0, tx); 1587 } 1588 mutex_exit(&dd->dd_lock); 1589} 1590 1591static void 1592dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx) 1593{ 1594 dsl_dir_set_qr_arg_t *ddsqra = arg; 1595 dsl_pool_t *dp = dmu_tx_pool(tx); 1596 dsl_dataset_t *ds; 1597 uint64_t newval; 1598 1599 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 1600 1601 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { 1602 dsl_prop_set_sync_impl(ds, 1603 zfs_prop_to_name(ZFS_PROP_RESERVATION), 1604 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 1605 &ddsqra->ddsqra_value, tx); 1606 1607 VERIFY0(dsl_prop_get_int_ds(ds, 1608 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval)); 1609 } else { 1610 newval = ddsqra->ddsqra_value; 1611 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", 1612 zfs_prop_to_name(ZFS_PROP_RESERVATION), 1613 (longlong_t)newval); 1614 } 1615 1616 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx); 1617 dsl_dataset_rele(ds, FTAG); 1618} 1619 1620int 1621dsl_dir_set_reservation(const char *ddname, zprop_source_t source, 1622 uint64_t reservation) 1623{ 1624 dsl_dir_set_qr_arg_t ddsqra; 1625 1626 ddsqra.ddsqra_name = ddname; 1627 ddsqra.ddsqra_source = source; 1628 ddsqra.ddsqra_value = reservation; 1629 1630 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check, 1631 dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 1632} 1633 1634static dsl_dir_t * 1635closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) 1636{ 1637 for (; ds1; ds1 = ds1->dd_parent) { 1638 dsl_dir_t *dd; 1639 for (dd = ds2; dd; dd = dd->dd_parent) { 1640 if (ds1 == dd) 1641 return (dd); 1642 } 1643 } 1644 return (NULL); 1645} 1646 1647/* 1648 * If delta is applied to dd, how much of that delta would be applied to 1649 * ancestor? Syncing context only. 1650 */ 1651static int64_t 1652would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) 1653{ 1654 if (dd == ancestor) 1655 return (delta); 1656 1657 mutex_enter(&dd->dd_lock); 1658 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta); 1659 mutex_exit(&dd->dd_lock); 1660 return (would_change(dd->dd_parent, delta, ancestor)); 1661} 1662 1663typedef struct dsl_dir_rename_arg { 1664 const char *ddra_oldname; 1665 const char *ddra_newname; 1666 cred_t *ddra_cred; 1667} dsl_dir_rename_arg_t; 1668 1669/* ARGSUSED */ 1670static int 1671dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1672{ 1673 int *deltap = arg; 1674 char namebuf[MAXNAMELEN]; 1675 1676 dsl_dataset_name(ds, namebuf); 1677 1678 if (strlen(namebuf) + *deltap >= MAXNAMELEN) 1679 return (SET_ERROR(ENAMETOOLONG)); 1680 return (0); 1681} 1682 1683static int 1684dsl_dir_rename_check(void *arg, dmu_tx_t *tx) 1685{ 1686 dsl_dir_rename_arg_t *ddra = arg; 1687 dsl_pool_t *dp = dmu_tx_pool(tx); 1688 dsl_dir_t *dd, *newparent; 1689 const char *mynewname; 1690 int error; 1691 int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname); 1692 1693 /* target dir should exist */ 1694 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL); 1695 if (error != 0) 1696 return (error); 1697 1698 /* new parent should exist */ 1699 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG, 1700 &newparent, &mynewname); 1701 if (error != 0) { 1702 dsl_dir_rele(dd, FTAG); 1703 return (error); 1704 } 1705 1706 /* can't rename to different pool */ 1707 if (dd->dd_pool != newparent->dd_pool) { 1708 dsl_dir_rele(newparent, FTAG); 1709 dsl_dir_rele(dd, FTAG); 1710 return (SET_ERROR(EXDEV)); 1711 } 1712 1713 /* new name should not already exist */ 1714 if (mynewname == NULL) { 1715 dsl_dir_rele(newparent, FTAG); 1716 dsl_dir_rele(dd, FTAG); 1717 return (SET_ERROR(EEXIST)); 1718 } 1719 1720 /* if the name length is growing, validate child name lengths */ 1721 if (delta > 0) { 1722 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename, 1723 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 1724 if (error != 0) { 1725 dsl_dir_rele(newparent, FTAG); 1726 dsl_dir_rele(dd, FTAG); 1727 return (error); 1728 } 1729 } 1730 1731 if (dmu_tx_is_syncing(tx)) { 1732 if (spa_feature_is_active(dp->dp_spa, 1733 SPA_FEATURE_FS_SS_LIMIT)) { 1734 /* 1735 * Although this is the check function and we don't 1736 * normally make on-disk changes in check functions, 1737 * we need to do that here. 1738 * 1739 * Ensure this portion of the tree's counts have been 1740 * initialized in case the new parent has limits set. 1741 */ 1742 dsl_dir_init_fs_ss_count(dd, tx); 1743 } 1744 } 1745 1746 if (newparent != dd->dd_parent) { 1747 /* is there enough space? */ 1748 uint64_t myspace = 1749 MAX(dsl_dir_phys(dd)->dd_used_bytes, 1750 dsl_dir_phys(dd)->dd_reserved); 1751 objset_t *os = dd->dd_pool->dp_meta_objset; 1752 uint64_t fs_cnt = 0; 1753 uint64_t ss_cnt = 0; 1754 1755 if (dsl_dir_is_zapified(dd)) { 1756 int err; 1757 1758 err = zap_lookup(os, dd->dd_object, 1759 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, 1760 &fs_cnt); 1761 if (err != ENOENT && err != 0) { 1762 dsl_dir_rele(newparent, FTAG); 1763 dsl_dir_rele(dd, FTAG); 1764 return (err); 1765 } 1766 1767 /* 1768 * have to add 1 for the filesystem itself that we're 1769 * moving 1770 */ 1771 fs_cnt++; 1772 1773 err = zap_lookup(os, dd->dd_object, 1774 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, 1775 &ss_cnt); 1776 if (err != ENOENT && err != 0) { 1777 dsl_dir_rele(newparent, FTAG); 1778 dsl_dir_rele(dd, FTAG); 1779 return (err); 1780 } 1781 } 1782 1783 /* no rename into our descendant */ 1784 if (closest_common_ancestor(dd, newparent) == dd) { 1785 dsl_dir_rele(newparent, FTAG); 1786 dsl_dir_rele(dd, FTAG); 1787 return (SET_ERROR(EINVAL)); 1788 } 1789 1790 error = dsl_dir_transfer_possible(dd->dd_parent, 1791 newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred); 1792 if (error != 0) { 1793 dsl_dir_rele(newparent, FTAG); 1794 dsl_dir_rele(dd, FTAG); 1795 return (error); 1796 } 1797 } 1798 1799 dsl_dir_rele(newparent, FTAG); 1800 dsl_dir_rele(dd, FTAG); 1801 return (0); 1802} 1803 1804static void 1805dsl_dir_rename_sync(void *arg, dmu_tx_t *tx) 1806{ 1807 dsl_dir_rename_arg_t *ddra = arg; 1808 dsl_pool_t *dp = dmu_tx_pool(tx); 1809 dsl_dir_t *dd, *newparent; 1810 const char *mynewname; 1811 int error; 1812 objset_t *mos = dp->dp_meta_objset; 1813 1814 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL)); 1815 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent, 1816 &mynewname)); 1817 1818 /* Log this before we change the name. */ 1819 spa_history_log_internal_dd(dd, "rename", tx, 1820 "-> %s", ddra->ddra_newname); 1821 1822 if (newparent != dd->dd_parent) { 1823 objset_t *os = dd->dd_pool->dp_meta_objset; 1824 uint64_t fs_cnt = 0; 1825 uint64_t ss_cnt = 0; 1826 1827 /* 1828 * We already made sure the dd counts were initialized in the 1829 * check function. 1830 */ 1831 if (spa_feature_is_active(dp->dp_spa, 1832 SPA_FEATURE_FS_SS_LIMIT)) { 1833 VERIFY0(zap_lookup(os, dd->dd_object, 1834 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, 1835 &fs_cnt)); 1836 /* add 1 for the filesystem itself that we're moving */ 1837 fs_cnt++; 1838 1839 VERIFY0(zap_lookup(os, dd->dd_object, 1840 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, 1841 &ss_cnt)); 1842 } 1843 1844 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt, 1845 DD_FIELD_FILESYSTEM_COUNT, tx); 1846 dsl_fs_ss_count_adjust(newparent, fs_cnt, 1847 DD_FIELD_FILESYSTEM_COUNT, tx); 1848 1849 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt, 1850 DD_FIELD_SNAPSHOT_COUNT, tx); 1851 dsl_fs_ss_count_adjust(newparent, ss_cnt, 1852 DD_FIELD_SNAPSHOT_COUNT, tx); 1853 1854 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 1855 -dsl_dir_phys(dd)->dd_used_bytes, 1856 -dsl_dir_phys(dd)->dd_compressed_bytes, 1857 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx); 1858 dsl_dir_diduse_space(newparent, DD_USED_CHILD, 1859 dsl_dir_phys(dd)->dd_used_bytes, 1860 dsl_dir_phys(dd)->dd_compressed_bytes, 1861 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx); 1862 1863 if (dsl_dir_phys(dd)->dd_reserved > 1864 dsl_dir_phys(dd)->dd_used_bytes) { 1865 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved - 1866 dsl_dir_phys(dd)->dd_used_bytes; 1867 1868 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 1869 -unused_rsrv, 0, 0, tx); 1870 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV, 1871 unused_rsrv, 0, 0, tx); 1872 } 1873 } 1874 1875 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1876 1877 /* remove from old parent zapobj */ 1878 error = zap_remove(mos, 1879 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj, 1880 dd->dd_myname, tx); 1881 ASSERT0(error); 1882 1883 (void) strcpy(dd->dd_myname, mynewname); 1884 dsl_dir_rele(dd->dd_parent, dd); 1885 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object; 1886 VERIFY0(dsl_dir_hold_obj(dp, 1887 newparent->dd_object, NULL, dd, &dd->dd_parent)); 1888 1889 /* add to new parent zapobj */ 1890 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj, 1891 dd->dd_myname, 8, 1, &dd->dd_object, tx)); 1892 1893#ifdef __FreeBSD__ 1894#ifdef _KERNEL 1895 zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname); 1896 zvol_rename_minors(ddra->ddra_oldname, ddra->ddra_newname); 1897#endif 1898#endif 1899 1900 dsl_prop_notify_all(dd); 1901 1902 dsl_dir_rele(newparent, FTAG); 1903 dsl_dir_rele(dd, FTAG); 1904} 1905 1906int 1907dsl_dir_rename(const char *oldname, const char *newname) 1908{ 1909 dsl_dir_rename_arg_t ddra; 1910 1911 ddra.ddra_oldname = oldname; 1912 ddra.ddra_newname = newname; 1913 ddra.ddra_cred = CRED(); 1914 1915 return (dsl_sync_task(oldname, 1916 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra, 1917 3, ZFS_SPACE_CHECK_RESERVED)); 1918} 1919 1920int 1921dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, 1922 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr) 1923{ 1924 dsl_dir_t *ancestor; 1925 int64_t adelta; 1926 uint64_t avail; 1927 int err; 1928 1929 ancestor = closest_common_ancestor(sdd, tdd); 1930 adelta = would_change(sdd, -space, ancestor); 1931 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); 1932 if (avail < space) 1933 return (SET_ERROR(ENOSPC)); 1934 1935 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT, 1936 ancestor, cr); 1937 if (err != 0) 1938 return (err); 1939 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT, 1940 ancestor, cr); 1941 if (err != 0) 1942 return (err); 1943 1944 return (0); 1945} 1946 1947timestruc_t 1948dsl_dir_snap_cmtime(dsl_dir_t *dd) 1949{ 1950 timestruc_t t; 1951 1952 mutex_enter(&dd->dd_lock); 1953 t = dd->dd_snap_cmtime; 1954 mutex_exit(&dd->dd_lock); 1955 1956 return (t); 1957} 1958 1959void 1960dsl_dir_snap_cmtime_update(dsl_dir_t *dd) 1961{ 1962 timestruc_t t; 1963 1964 gethrestime(&t); 1965 mutex_enter(&dd->dd_lock); 1966 dd->dd_snap_cmtime = t; 1967 mutex_exit(&dd->dd_lock); 1968} 1969 1970void 1971dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx) 1972{ 1973 objset_t *mos = dd->dd_pool->dp_meta_objset; 1974 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx); 1975} 1976 1977boolean_t 1978dsl_dir_is_zapified(dsl_dir_t *dd) 1979{ 1980 dmu_object_info_t doi; 1981 1982 dmu_object_info_from_db(dd->dd_dbuf, &doi); 1983 return (doi.doi_type == DMU_OTN_ZAP_METADATA); 1984} 1985