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