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 https://opensource.org/licenses/CDDL-1.0. 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, 2018 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 (c) 2016 Actifio, Inc. All rights reserved. 28 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. 29 * Copyright (c) 2023 Hewlett Packard Enterprise Development LP. 30 */ 31 32 #include <sys/dmu.h> 33 #include <sys/dmu_objset.h> 34 #include <sys/dmu_tx.h> 35 #include <sys/dsl_dataset.h> 36 #include <sys/dsl_dir.h> 37 #include <sys/dsl_prop.h> 38 #include <sys/dsl_synctask.h> 39 #include <sys/dsl_deleg.h> 40 #include <sys/dmu_impl.h> 41 #include <sys/spa.h> 42 #include <sys/spa_impl.h> 43 #include <sys/metaslab.h> 44 #include <sys/zap.h> 45 #include <sys/zio.h> 46 #include <sys/arc.h> 47 #include <sys/sunddi.h> 48 #include <sys/zfeature.h> 49 #include <sys/policy.h> 50 #include <sys/zfs_vfsops.h> 51 #include <sys/zfs_znode.h> 52 #include <sys/zvol.h> 53 #include <sys/zthr.h> 54 #include "zfs_namecheck.h" 55 #include "zfs_prop.h" 56 57 /* 58 * This controls if we verify the ZVOL quota or not. 59 * Currently, quotas are not implemented for ZVOLs. 60 * The quota size is the size of the ZVOL. 61 * The size of the volume already implies the ZVOL size quota. 62 * The quota mechanism can introduce a significant performance drop. 63 */ 64 static int zvol_enforce_quotas = B_TRUE; 65 66 /* 67 * Filesystem and Snapshot Limits 68 * ------------------------------ 69 * 70 * These limits are used to restrict the number of filesystems and/or snapshots 71 * that can be created at a given level in the tree or below. A typical 72 * use-case is with a delegated dataset where the administrator wants to ensure 73 * that a user within the zone is not creating too many additional filesystems 74 * or snapshots, even though they're not exceeding their space quota. 75 * 76 * The filesystem and snapshot counts are stored as extensible properties. This 77 * capability is controlled by a feature flag and must be enabled to be used. 78 * Once enabled, the feature is not active until the first limit is set. At 79 * that point, future operations to create/destroy filesystems or snapshots 80 * will validate and update the counts. 81 * 82 * Because the count properties will not exist before the feature is active, 83 * the counts are updated when a limit is first set on an uninitialized 84 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes 85 * all of the nested filesystems/snapshots. Thus, a new leaf node has a 86 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and 87 * snapshot count properties on a node indicate uninitialized counts on that 88 * node.) When first setting a limit on an uninitialized node, the code starts 89 * at the filesystem with the new limit and descends into all sub-filesystems 90 * to add the count properties. 91 * 92 * In practice this is lightweight since a limit is typically set when the 93 * filesystem is created and thus has no children. Once valid, changing the 94 * limit value won't require a re-traversal since the counts are already valid. 95 * When recursively fixing the counts, if a node with a limit is encountered 96 * during the descent, the counts are known to be valid and there is no need to 97 * descend into that filesystem's children. The counts on filesystems above the 98 * one with the new limit will still be uninitialized, unless a limit is 99 * eventually set on one of those filesystems. The counts are always recursively 100 * updated when a limit is set on a dataset, unless there is already a limit. 101 * When a new limit value is set on a filesystem with an existing limit, it is 102 * possible for the new limit to be less than the current count at that level 103 * since a user who can change the limit is also allowed to exceed the limit. 104 * 105 * Once the feature is active, then whenever a filesystem or snapshot is 106 * created, the code recurses up the tree, validating the new count against the 107 * limit at each initialized level. In practice, most levels will not have a 108 * limit set. If there is a limit at any initialized level up the tree, the 109 * check must pass or the creation will fail. Likewise, when a filesystem or 110 * snapshot is destroyed, the counts are recursively adjusted all the way up 111 * the initialized nodes in the tree. Renaming a filesystem into different point 112 * in the tree will first validate, then update the counts on each branch up to 113 * the common ancestor. A receive will also validate the counts and then update 114 * them. 115 * 116 * An exception to the above behavior is that the limit is not enforced if the 117 * user has permission to modify the limit. This is primarily so that 118 * recursive snapshots in the global zone always work. We want to prevent a 119 * denial-of-service in which a lower level delegated dataset could max out its 120 * limit and thus block recursive snapshots from being taken in the global zone. 121 * Because of this, it is possible for the snapshot count to be over the limit 122 * and snapshots taken in the global zone could cause a lower level dataset to 123 * hit or exceed its limit. The administrator taking the global zone recursive 124 * snapshot should be aware of this side-effect and behave accordingly. 125 * For consistency, the filesystem limit is also not enforced if the user can 126 * modify the limit. 127 * 128 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check() 129 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in 130 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by 131 * dsl_dir_init_fs_ss_count(). 132 */ 133 134 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd); 135 136 typedef struct ddulrt_arg { 137 dsl_dir_t *ddulrta_dd; 138 uint64_t ddlrta_txg; 139 } ddulrt_arg_t; 140 141 static void 142 dsl_dir_evict_async(void *dbu) 143 { 144 dsl_dir_t *dd = dbu; 145 int t; 146 dsl_pool_t *dp __maybe_unused = dd->dd_pool; 147 148 dd->dd_dbuf = NULL; 149 150 for (t = 0; t < TXG_SIZE; t++) { 151 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t)); 152 ASSERT(dd->dd_tempreserved[t] == 0); 153 ASSERT(dd->dd_space_towrite[t] == 0); 154 } 155 156 if (dd->dd_parent) 157 dsl_dir_async_rele(dd->dd_parent, dd); 158 159 spa_async_close(dd->dd_pool->dp_spa, dd); 160 161 if (dsl_deadlist_is_open(&dd->dd_livelist)) 162 dsl_dir_livelist_close(dd); 163 164 dsl_prop_fini(dd); 165 cv_destroy(&dd->dd_activity_cv); 166 mutex_destroy(&dd->dd_activity_lock); 167 mutex_destroy(&dd->dd_lock); 168 kmem_free(dd, sizeof (dsl_dir_t)); 169 } 170 171 int 172 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj, 173 const char *tail, const void *tag, dsl_dir_t **ddp) 174 { 175 dmu_buf_t *dbuf; 176 dsl_dir_t *dd; 177 dmu_object_info_t doi; 178 int err; 179 180 ASSERT(dsl_pool_config_held(dp)); 181 182 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf); 183 if (err != 0) 184 return (err); 185 dd = dmu_buf_get_user(dbuf); 186 187 dmu_object_info_from_db(dbuf, &doi); 188 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR); 189 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t)); 190 191 if (dd == NULL) { 192 dsl_dir_t *winner; 193 194 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP); 195 dd->dd_object = ddobj; 196 dd->dd_dbuf = dbuf; 197 dd->dd_pool = dp; 198 199 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); 200 mutex_init(&dd->dd_activity_lock, NULL, MUTEX_DEFAULT, NULL); 201 cv_init(&dd->dd_activity_cv, NULL, CV_DEFAULT, NULL); 202 dsl_prop_init(dd); 203 204 if (dsl_dir_is_zapified(dd)) { 205 err = zap_lookup(dp->dp_meta_objset, 206 ddobj, DD_FIELD_CRYPTO_KEY_OBJ, 207 sizeof (uint64_t), 1, &dd->dd_crypto_obj); 208 if (err == 0) { 209 /* check for on-disk format errata */ 210 if (dsl_dir_incompatible_encryption_version( 211 dd)) { 212 dp->dp_spa->spa_errata = 213 ZPOOL_ERRATA_ZOL_6845_ENCRYPTION; 214 } 215 } else if (err != ENOENT) { 216 goto errout; 217 } 218 } 219 220 if (dsl_dir_phys(dd)->dd_parent_obj) { 221 err = dsl_dir_hold_obj(dp, 222 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd, 223 &dd->dd_parent); 224 if (err != 0) 225 goto errout; 226 if (tail) { 227 #ifdef ZFS_DEBUG 228 uint64_t foundobj; 229 230 err = zap_lookup(dp->dp_meta_objset, 231 dsl_dir_phys(dd->dd_parent)-> 232 dd_child_dir_zapobj, tail, 233 sizeof (foundobj), 1, &foundobj); 234 ASSERT(err || foundobj == ddobj); 235 #endif 236 (void) strlcpy(dd->dd_myname, tail, 237 sizeof (dd->dd_myname)); 238 } else { 239 err = zap_value_search(dp->dp_meta_objset, 240 dsl_dir_phys(dd->dd_parent)-> 241 dd_child_dir_zapobj, 242 ddobj, 0, dd->dd_myname, 243 sizeof (dd->dd_myname)); 244 } 245 if (err != 0) 246 goto errout; 247 } else { 248 (void) strlcpy(dd->dd_myname, spa_name(dp->dp_spa), 249 sizeof (dd->dd_myname)); 250 } 251 252 if (dsl_dir_is_clone(dd)) { 253 dmu_buf_t *origin_bonus; 254 dsl_dataset_phys_t *origin_phys; 255 256 /* 257 * We can't open the origin dataset, because 258 * that would require opening this dsl_dir. 259 * Just look at its phys directly instead. 260 */ 261 err = dmu_bonus_hold(dp->dp_meta_objset, 262 dsl_dir_phys(dd)->dd_origin_obj, FTAG, 263 &origin_bonus); 264 if (err != 0) 265 goto errout; 266 origin_phys = origin_bonus->db_data; 267 dd->dd_origin_txg = 268 origin_phys->ds_creation_txg; 269 dmu_buf_rele(origin_bonus, FTAG); 270 if (dsl_dir_is_zapified(dd)) { 271 uint64_t obj; 272 err = zap_lookup(dp->dp_meta_objset, 273 dd->dd_object, DD_FIELD_LIVELIST, 274 sizeof (uint64_t), 1, &obj); 275 if (err == 0) { 276 err = dsl_dir_livelist_open(dd, obj); 277 if (err != 0) 278 goto errout; 279 } else if (err != ENOENT) 280 goto errout; 281 } 282 } 283 284 if (dsl_dir_is_zapified(dd)) { 285 inode_timespec_t t = {0}; 286 (void) zap_lookup(dp->dp_meta_objset, ddobj, 287 DD_FIELD_SNAPSHOTS_CHANGED, 288 sizeof (uint64_t), 289 sizeof (inode_timespec_t) / sizeof (uint64_t), 290 &t); 291 dd->dd_snap_cmtime = t; 292 } 293 294 dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async, 295 &dd->dd_dbuf); 296 winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu); 297 if (winner != NULL) { 298 if (dd->dd_parent) 299 dsl_dir_rele(dd->dd_parent, dd); 300 if (dsl_deadlist_is_open(&dd->dd_livelist)) 301 dsl_dir_livelist_close(dd); 302 dsl_prop_fini(dd); 303 cv_destroy(&dd->dd_activity_cv); 304 mutex_destroy(&dd->dd_activity_lock); 305 mutex_destroy(&dd->dd_lock); 306 kmem_free(dd, sizeof (dsl_dir_t)); 307 dd = winner; 308 } else { 309 spa_open_ref(dp->dp_spa, dd); 310 } 311 } 312 313 /* 314 * The dsl_dir_t has both open-to-close and instantiate-to-evict 315 * holds on the spa. We need the open-to-close holds because 316 * otherwise the spa_refcnt wouldn't change when we open a 317 * dir which the spa also has open, so we could incorrectly 318 * think it was OK to unload/export/destroy the pool. We need 319 * the instantiate-to-evict hold because the dsl_dir_t has a 320 * pointer to the dd_pool, which has a pointer to the spa_t. 321 */ 322 spa_open_ref(dp->dp_spa, tag); 323 ASSERT3P(dd->dd_pool, ==, dp); 324 ASSERT3U(dd->dd_object, ==, ddobj); 325 ASSERT3P(dd->dd_dbuf, ==, dbuf); 326 *ddp = dd; 327 return (0); 328 329 errout: 330 if (dd->dd_parent) 331 dsl_dir_rele(dd->dd_parent, dd); 332 if (dsl_deadlist_is_open(&dd->dd_livelist)) 333 dsl_dir_livelist_close(dd); 334 dsl_prop_fini(dd); 335 cv_destroy(&dd->dd_activity_cv); 336 mutex_destroy(&dd->dd_activity_lock); 337 mutex_destroy(&dd->dd_lock); 338 kmem_free(dd, sizeof (dsl_dir_t)); 339 dmu_buf_rele(dbuf, tag); 340 return (err); 341 } 342 343 void 344 dsl_dir_rele(dsl_dir_t *dd, const void *tag) 345 { 346 dprintf_dd(dd, "%s\n", ""); 347 spa_close(dd->dd_pool->dp_spa, tag); 348 dmu_buf_rele(dd->dd_dbuf, tag); 349 } 350 351 /* 352 * Remove a reference to the given dsl dir that is being asynchronously 353 * released. Async releases occur from a taskq performing eviction of 354 * dsl datasets and dirs. This process is identical to a normal release 355 * with the exception of using the async API for releasing the reference on 356 * the spa. 357 */ 358 void 359 dsl_dir_async_rele(dsl_dir_t *dd, const void *tag) 360 { 361 dprintf_dd(dd, "%s\n", ""); 362 spa_async_close(dd->dd_pool->dp_spa, tag); 363 dmu_buf_rele(dd->dd_dbuf, tag); 364 } 365 366 /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */ 367 void 368 dsl_dir_name(dsl_dir_t *dd, char *buf) 369 { 370 if (dd->dd_parent) { 371 dsl_dir_name(dd->dd_parent, buf); 372 VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <, 373 ZFS_MAX_DATASET_NAME_LEN); 374 } else { 375 buf[0] = '\0'; 376 } 377 if (!MUTEX_HELD(&dd->dd_lock)) { 378 /* 379 * recursive mutex so that we can use 380 * dprintf_dd() with dd_lock held 381 */ 382 mutex_enter(&dd->dd_lock); 383 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN), 384 <, ZFS_MAX_DATASET_NAME_LEN); 385 mutex_exit(&dd->dd_lock); 386 } else { 387 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN), 388 <, ZFS_MAX_DATASET_NAME_LEN); 389 } 390 } 391 392 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */ 393 int 394 dsl_dir_namelen(dsl_dir_t *dd) 395 { 396 int result = 0; 397 398 if (dd->dd_parent) { 399 /* parent's name + 1 for the "/" */ 400 result = dsl_dir_namelen(dd->dd_parent) + 1; 401 } 402 403 if (!MUTEX_HELD(&dd->dd_lock)) { 404 /* see dsl_dir_name */ 405 mutex_enter(&dd->dd_lock); 406 result += strlen(dd->dd_myname); 407 mutex_exit(&dd->dd_lock); 408 } else { 409 result += strlen(dd->dd_myname); 410 } 411 412 return (result); 413 } 414 415 static int 416 getcomponent(const char *path, char *component, const char **nextp) 417 { 418 char *p; 419 420 if ((path == NULL) || (path[0] == '\0')) 421 return (SET_ERROR(ENOENT)); 422 /* This would be a good place to reserve some namespace... */ 423 p = strpbrk(path, "/@"); 424 if (p && (p[1] == '/' || p[1] == '@')) { 425 /* two separators in a row */ 426 return (SET_ERROR(EINVAL)); 427 } 428 if (p == NULL || p == path) { 429 /* 430 * if the first thing is an @ or /, it had better be an 431 * @ and it had better not have any more ats or slashes, 432 * and it had better have something after the @. 433 */ 434 if (p != NULL && 435 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) 436 return (SET_ERROR(EINVAL)); 437 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) 438 return (SET_ERROR(ENAMETOOLONG)); 439 (void) strlcpy(component, path, ZFS_MAX_DATASET_NAME_LEN); 440 p = NULL; 441 } else if (p[0] == '/') { 442 if (p - path >= ZFS_MAX_DATASET_NAME_LEN) 443 return (SET_ERROR(ENAMETOOLONG)); 444 (void) strlcpy(component, path, p - path + 1); 445 p++; 446 } else if (p[0] == '@') { 447 /* 448 * if the next separator is an @, there better not be 449 * any more slashes. 450 */ 451 if (strchr(path, '/')) 452 return (SET_ERROR(EINVAL)); 453 if (p - path >= ZFS_MAX_DATASET_NAME_LEN) 454 return (SET_ERROR(ENAMETOOLONG)); 455 (void) strlcpy(component, path, p - path + 1); 456 } else { 457 panic("invalid p=%p", (void *)p); 458 } 459 *nextp = p; 460 return (0); 461 } 462 463 /* 464 * Return the dsl_dir_t, and possibly the last component which couldn't 465 * be found in *tail. The name must be in the specified dsl_pool_t. This 466 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the 467 * path is bogus, or if tail==NULL and we couldn't parse the whole name. 468 * (*tail)[0] == '@' means that the last component is a snapshot. 469 */ 470 int 471 dsl_dir_hold(dsl_pool_t *dp, const char *name, const void *tag, 472 dsl_dir_t **ddp, const char **tailp) 473 { 474 char *buf; 475 const char *spaname, *next, *nextnext = NULL; 476 int err; 477 dsl_dir_t *dd; 478 uint64_t ddobj; 479 480 buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 481 err = getcomponent(name, buf, &next); 482 if (err != 0) 483 goto error; 484 485 /* Make sure the name is in the specified pool. */ 486 spaname = spa_name(dp->dp_spa); 487 if (strcmp(buf, spaname) != 0) { 488 err = SET_ERROR(EXDEV); 489 goto error; 490 } 491 492 ASSERT(dsl_pool_config_held(dp)); 493 494 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); 495 if (err != 0) { 496 goto error; 497 } 498 499 while (next != NULL) { 500 dsl_dir_t *child_dd; 501 err = getcomponent(next, buf, &nextnext); 502 if (err != 0) 503 break; 504 ASSERT(next[0] != '\0'); 505 if (next[0] == '@') 506 break; 507 dprintf("looking up %s in obj%lld\n", 508 buf, (longlong_t)dsl_dir_phys(dd)->dd_child_dir_zapobj); 509 510 err = zap_lookup(dp->dp_meta_objset, 511 dsl_dir_phys(dd)->dd_child_dir_zapobj, 512 buf, sizeof (ddobj), 1, &ddobj); 513 if (err != 0) { 514 if (err == ENOENT) 515 err = 0; 516 break; 517 } 518 519 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd); 520 if (err != 0) 521 break; 522 dsl_dir_rele(dd, tag); 523 dd = child_dd; 524 next = nextnext; 525 } 526 527 if (err != 0) { 528 dsl_dir_rele(dd, tag); 529 goto error; 530 } 531 532 /* 533 * It's an error if there's more than one component left, or 534 * tailp==NULL and there's any component left. 535 */ 536 if (next != NULL && 537 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { 538 /* bad path name */ 539 dsl_dir_rele(dd, tag); 540 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); 541 err = SET_ERROR(ENOENT); 542 } 543 if (tailp != NULL) 544 *tailp = next; 545 if (err == 0) 546 *ddp = dd; 547 error: 548 kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN); 549 return (err); 550 } 551 552 /* 553 * If the counts are already initialized for this filesystem and its 554 * descendants then do nothing, otherwise initialize the counts. 555 * 556 * The counts on this filesystem, and those below, may be uninitialized due to 557 * either the use of a pre-existing pool which did not support the 558 * filesystem/snapshot limit feature, or one in which the feature had not yet 559 * been enabled. 560 * 561 * Recursively descend the filesystem tree and update the filesystem/snapshot 562 * counts on each filesystem below, then update the cumulative count on the 563 * current filesystem. If the filesystem already has a count set on it, 564 * then we know that its counts, and the counts on the filesystems below it, 565 * are already correct, so we don't have to update this filesystem. 566 */ 567 static void 568 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx) 569 { 570 uint64_t my_fs_cnt = 0; 571 uint64_t my_ss_cnt = 0; 572 dsl_pool_t *dp = dd->dd_pool; 573 objset_t *os = dp->dp_meta_objset; 574 zap_cursor_t *zc; 575 zap_attribute_t *za; 576 dsl_dataset_t *ds; 577 578 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)); 579 ASSERT(dsl_pool_config_held(dp)); 580 ASSERT(dmu_tx_is_syncing(tx)); 581 582 dsl_dir_zapify(dd, tx); 583 584 /* 585 * If the filesystem count has already been initialized then we 586 * don't need to recurse down any further. 587 */ 588 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0) 589 return; 590 591 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); 592 za = zap_attribute_alloc(); 593 594 /* Iterate my child dirs */ 595 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj); 596 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) { 597 dsl_dir_t *chld_dd; 598 uint64_t count; 599 600 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG, 601 &chld_dd)); 602 603 /* 604 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets. 605 */ 606 if (chld_dd->dd_myname[0] == '$') { 607 dsl_dir_rele(chld_dd, FTAG); 608 continue; 609 } 610 611 my_fs_cnt++; /* count this child */ 612 613 dsl_dir_init_fs_ss_count(chld_dd, tx); 614 615 VERIFY0(zap_lookup(os, chld_dd->dd_object, 616 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count)); 617 my_fs_cnt += count; 618 VERIFY0(zap_lookup(os, chld_dd->dd_object, 619 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count)); 620 my_ss_cnt += count; 621 622 dsl_dir_rele(chld_dd, FTAG); 623 } 624 zap_cursor_fini(zc); 625 /* Count my snapshots (we counted children's snapshots above) */ 626 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 627 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds)); 628 629 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj); 630 zap_cursor_retrieve(zc, za) == 0; 631 zap_cursor_advance(zc)) { 632 /* Don't count temporary snapshots */ 633 if (za->za_name[0] != '%') 634 my_ss_cnt++; 635 } 636 zap_cursor_fini(zc); 637 638 dsl_dataset_rele(ds, FTAG); 639 640 kmem_free(zc, sizeof (zap_cursor_t)); 641 zap_attribute_free(za); 642 643 /* we're in a sync task, update counts */ 644 dmu_buf_will_dirty(dd->dd_dbuf, tx); 645 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 646 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx)); 647 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 648 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx)); 649 } 650 651 static int 652 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx) 653 { 654 char *ddname = (char *)arg; 655 dsl_pool_t *dp = dmu_tx_pool(tx); 656 dsl_dataset_t *ds; 657 dsl_dir_t *dd; 658 int error; 659 660 error = dsl_dataset_hold(dp, ddname, FTAG, &ds); 661 if (error != 0) 662 return (error); 663 664 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 665 dsl_dataset_rele(ds, FTAG); 666 return (SET_ERROR(ENOTSUP)); 667 } 668 669 dd = ds->ds_dir; 670 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) && 671 dsl_dir_is_zapified(dd) && 672 zap_contains(dp->dp_meta_objset, dd->dd_object, 673 DD_FIELD_FILESYSTEM_COUNT) == 0) { 674 dsl_dataset_rele(ds, FTAG); 675 return (SET_ERROR(EALREADY)); 676 } 677 678 dsl_dataset_rele(ds, FTAG); 679 return (0); 680 } 681 682 static void 683 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx) 684 { 685 char *ddname = (char *)arg; 686 dsl_pool_t *dp = dmu_tx_pool(tx); 687 dsl_dataset_t *ds; 688 spa_t *spa; 689 690 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds)); 691 692 spa = dsl_dataset_get_spa(ds); 693 694 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) { 695 /* 696 * Since the feature was not active and we're now setting a 697 * limit, increment the feature-active counter so that the 698 * feature becomes active for the first time. 699 * 700 * We are already in a sync task so we can update the MOS. 701 */ 702 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx); 703 } 704 705 /* 706 * Since we are now setting a non-UINT64_MAX limit on the filesystem, 707 * we need to ensure the counts are correct. Descend down the tree from 708 * this point and update all of the counts to be accurate. 709 */ 710 dsl_dir_init_fs_ss_count(ds->ds_dir, tx); 711 712 dsl_dataset_rele(ds, FTAG); 713 } 714 715 /* 716 * Make sure the feature is enabled and activate it if necessary. 717 * Since we're setting a limit, ensure the on-disk counts are valid. 718 * This is only called by the ioctl path when setting a limit value. 719 * 720 * We do not need to validate the new limit, since users who can change the 721 * limit are also allowed to exceed the limit. 722 */ 723 int 724 dsl_dir_activate_fs_ss_limit(const char *ddname) 725 { 726 int error; 727 728 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check, 729 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0, 730 ZFS_SPACE_CHECK_RESERVED); 731 732 if (error == EALREADY) 733 error = 0; 734 735 return (error); 736 } 737 738 /* 739 * Used to determine if the filesystem_limit or snapshot_limit should be 740 * enforced. We allow the limit to be exceeded if the user has permission to 741 * write the property value. We pass in the creds that we got in the open 742 * context since we will always be the GZ root in syncing context. We also have 743 * to handle the case where we are allowed to change the limit on the current 744 * dataset, but there may be another limit in the tree above. 745 * 746 * We can never modify these two properties within a non-global zone. In 747 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We 748 * can't use that function since we are already holding the dp_config_rwlock. 749 * In addition, we already have the dd and dealing with snapshots is simplified 750 * in this code. 751 */ 752 753 typedef enum { 754 ENFORCE_ALWAYS, 755 ENFORCE_NEVER, 756 ENFORCE_ABOVE 757 } enforce_res_t; 758 759 static enforce_res_t 760 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, 761 cred_t *cr, proc_t *proc) 762 { 763 enforce_res_t enforce = ENFORCE_ALWAYS; 764 uint64_t obj; 765 dsl_dataset_t *ds; 766 uint64_t zoned; 767 const char *zonedstr; 768 769 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || 770 prop == ZFS_PROP_SNAPSHOT_LIMIT); 771 772 #ifdef _KERNEL 773 if (crgetzoneid(cr) != GLOBAL_ZONEID) 774 return (ENFORCE_ALWAYS); 775 776 /* 777 * We are checking the saved credentials of the user process, which is 778 * not the current process. Note that we can't use secpolicy_zfs(), 779 * because it only works if the cred is that of the current process (on 780 * Linux). 781 */ 782 if (secpolicy_zfs_proc(cr, proc) == 0) 783 return (ENFORCE_NEVER); 784 #else 785 (void) proc; 786 #endif 787 788 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0) 789 return (ENFORCE_ALWAYS); 790 791 ASSERT(dsl_pool_config_held(dd->dd_pool)); 792 793 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0) 794 return (ENFORCE_ALWAYS); 795 796 zonedstr = zfs_prop_to_name(ZFS_PROP_ZONED); 797 if (dsl_prop_get_ds(ds, zonedstr, 8, 1, &zoned, NULL) || zoned) { 798 /* Only root can access zoned fs's from the GZ */ 799 enforce = ENFORCE_ALWAYS; 800 } else { 801 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0) 802 enforce = ENFORCE_ABOVE; 803 } 804 805 dsl_dataset_rele(ds, FTAG); 806 return (enforce); 807 } 808 809 /* 810 * Check if adding additional child filesystem(s) would exceed any filesystem 811 * limits or adding additional snapshot(s) would exceed any snapshot limits. 812 * The prop argument indicates which limit to check. 813 * 814 * Note that all filesystem limits up to the root (or the highest 815 * initialized) filesystem or the given ancestor must be satisfied. 816 */ 817 int 818 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop, 819 dsl_dir_t *ancestor, cred_t *cr, proc_t *proc) 820 { 821 objset_t *os = dd->dd_pool->dp_meta_objset; 822 uint64_t limit, count; 823 const char *count_prop; 824 enforce_res_t enforce; 825 int err = 0; 826 827 ASSERT(dsl_pool_config_held(dd->dd_pool)); 828 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || 829 prop == ZFS_PROP_SNAPSHOT_LIMIT); 830 831 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) { 832 /* 833 * We don't enforce the limit for temporary snapshots. This is 834 * indicated by a NULL cred_t argument. 835 */ 836 if (cr == NULL) 837 return (0); 838 839 count_prop = DD_FIELD_SNAPSHOT_COUNT; 840 } else { 841 count_prop = DD_FIELD_FILESYSTEM_COUNT; 842 } 843 /* 844 * If we're allowed to change the limit, don't enforce the limit 845 * e.g. this can happen if a snapshot is taken by an administrative 846 * user in the global zone (i.e. a recursive snapshot by root). 847 * However, we must handle the case of delegated permissions where we 848 * are allowed to change the limit on the current dataset, but there 849 * is another limit in the tree above. 850 */ 851 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr, proc); 852 if (enforce == ENFORCE_NEVER) 853 return (0); 854 855 /* 856 * e.g. if renaming a dataset with no snapshots, count adjustment 857 * is 0. 858 */ 859 if (delta == 0) 860 return (0); 861 862 /* 863 * If an ancestor has been provided, stop checking the limit once we 864 * hit that dir. We need this during rename so that we don't overcount 865 * the check once we recurse up to the common ancestor. 866 */ 867 if (ancestor == dd) 868 return (0); 869 870 /* 871 * If we hit an uninitialized node while recursing up the tree, we can 872 * stop since we know there is no limit here (or above). The counts are 873 * not valid on this node and we know we won't touch this node's counts. 874 */ 875 if (!dsl_dir_is_zapified(dd)) 876 return (0); 877 err = zap_lookup(os, dd->dd_object, 878 count_prop, sizeof (count), 1, &count); 879 if (err == ENOENT) 880 return (0); 881 if (err != 0) 882 return (err); 883 884 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL, 885 B_FALSE); 886 if (err != 0) 887 return (err); 888 889 /* Is there a limit which we've hit? */ 890 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit) 891 return (SET_ERROR(EDQUOT)); 892 893 if (dd->dd_parent != NULL) 894 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop, 895 ancestor, cr, proc); 896 897 return (err); 898 } 899 900 /* 901 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all 902 * parents. When a new filesystem/snapshot is created, increment the count on 903 * all parents, and when a filesystem/snapshot is destroyed, decrement the 904 * count. 905 */ 906 void 907 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop, 908 dmu_tx_t *tx) 909 { 910 int err; 911 objset_t *os = dd->dd_pool->dp_meta_objset; 912 uint64_t count; 913 914 ASSERT(dsl_pool_config_held(dd->dd_pool)); 915 ASSERT(dmu_tx_is_syncing(tx)); 916 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 || 917 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0); 918 919 /* 920 * We don't do accounting for hidden ($FREE, $MOS & $ORIGIN) objsets. 921 */ 922 if (dd->dd_myname[0] == '$' && strcmp(prop, 923 DD_FIELD_FILESYSTEM_COUNT) == 0) { 924 return; 925 } 926 927 /* 928 * e.g. if renaming a dataset with no snapshots, count adjustment is 0 929 */ 930 if (delta == 0) 931 return; 932 933 /* 934 * If we hit an uninitialized node while recursing up the tree, we can 935 * stop since we know the counts are not valid on this node and we 936 * know we shouldn't touch this node's counts. An uninitialized count 937 * on the node indicates that either the feature has not yet been 938 * activated or there are no limits on this part of the tree. 939 */ 940 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object, 941 prop, sizeof (count), 1, &count)) == ENOENT) 942 return; 943 VERIFY0(err); 944 945 count += delta; 946 /* Use a signed verify to make sure we're not neg. */ 947 VERIFY3S(count, >=, 0); 948 949 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count, 950 tx)); 951 952 /* Roll up this additional count into our ancestors */ 953 if (dd->dd_parent != NULL) 954 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx); 955 } 956 957 uint64_t 958 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, 959 dmu_tx_t *tx) 960 { 961 objset_t *mos = dp->dp_meta_objset; 962 uint64_t ddobj; 963 dsl_dir_phys_t *ddphys; 964 dmu_buf_t *dbuf; 965 966 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, 967 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); 968 if (pds) { 969 VERIFY0(zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj, 970 name, sizeof (uint64_t), 1, &ddobj, tx)); 971 } else { 972 /* it's the root dir */ 973 VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 974 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); 975 } 976 VERIFY0(dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); 977 dmu_buf_will_dirty(dbuf, tx); 978 ddphys = dbuf->db_data; 979 980 ddphys->dd_creation_time = gethrestime_sec(); 981 if (pds) { 982 ddphys->dd_parent_obj = pds->dd_object; 983 984 /* update the filesystem counts */ 985 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx); 986 } 987 ddphys->dd_props_zapobj = zap_create(mos, 988 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 989 ddphys->dd_child_dir_zapobj = zap_create(mos, 990 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); 991 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) 992 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; 993 994 dmu_buf_rele(dbuf, FTAG); 995 996 return (ddobj); 997 } 998 999 boolean_t 1000 dsl_dir_is_clone(dsl_dir_t *dd) 1001 { 1002 return (dsl_dir_phys(dd)->dd_origin_obj && 1003 (dd->dd_pool->dp_origin_snap == NULL || 1004 dsl_dir_phys(dd)->dd_origin_obj != 1005 dd->dd_pool->dp_origin_snap->ds_object)); 1006 } 1007 1008 uint64_t 1009 dsl_dir_get_used(dsl_dir_t *dd) 1010 { 1011 return (dsl_dir_phys(dd)->dd_used_bytes); 1012 } 1013 1014 uint64_t 1015 dsl_dir_get_compressed(dsl_dir_t *dd) 1016 { 1017 return (dsl_dir_phys(dd)->dd_compressed_bytes); 1018 } 1019 1020 uint64_t 1021 dsl_dir_get_quota(dsl_dir_t *dd) 1022 { 1023 return (dsl_dir_phys(dd)->dd_quota); 1024 } 1025 1026 uint64_t 1027 dsl_dir_get_reservation(dsl_dir_t *dd) 1028 { 1029 return (dsl_dir_phys(dd)->dd_reserved); 1030 } 1031 1032 uint64_t 1033 dsl_dir_get_compressratio(dsl_dir_t *dd) 1034 { 1035 /* a fixed point number, 100x the ratio */ 1036 return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 : 1037 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 / 1038 dsl_dir_phys(dd)->dd_compressed_bytes)); 1039 } 1040 1041 uint64_t 1042 dsl_dir_get_logicalused(dsl_dir_t *dd) 1043 { 1044 return (dsl_dir_phys(dd)->dd_uncompressed_bytes); 1045 } 1046 1047 uint64_t 1048 dsl_dir_get_usedsnap(dsl_dir_t *dd) 1049 { 1050 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]); 1051 } 1052 1053 uint64_t 1054 dsl_dir_get_usedds(dsl_dir_t *dd) 1055 { 1056 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]); 1057 } 1058 1059 uint64_t 1060 dsl_dir_get_usedrefreserv(dsl_dir_t *dd) 1061 { 1062 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]); 1063 } 1064 1065 uint64_t 1066 dsl_dir_get_usedchild(dsl_dir_t *dd) 1067 { 1068 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] + 1069 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]); 1070 } 1071 1072 void 1073 dsl_dir_get_origin(dsl_dir_t *dd, char *buf) 1074 { 1075 dsl_dataset_t *ds; 1076 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 1077 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds)); 1078 1079 dsl_dataset_name(ds, buf); 1080 1081 dsl_dataset_rele(ds, FTAG); 1082 } 1083 1084 int 1085 dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count) 1086 { 1087 if (dsl_dir_is_zapified(dd)) { 1088 objset_t *os = dd->dd_pool->dp_meta_objset; 1089 return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 1090 sizeof (*count), 1, count)); 1091 } else { 1092 return (SET_ERROR(ENOENT)); 1093 } 1094 } 1095 1096 int 1097 dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count) 1098 { 1099 if (dsl_dir_is_zapified(dd)) { 1100 objset_t *os = dd->dd_pool->dp_meta_objset; 1101 return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 1102 sizeof (*count), 1, count)); 1103 } else { 1104 return (SET_ERROR(ENOENT)); 1105 } 1106 } 1107 1108 void 1109 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) 1110 { 1111 mutex_enter(&dd->dd_lock); 1112 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, 1113 dsl_dir_get_quota(dd)); 1114 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, 1115 dsl_dir_get_reservation(dd)); 1116 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED, 1117 dsl_dir_get_logicalused(dd)); 1118 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 1119 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, 1120 dsl_dir_get_usedsnap(dd)); 1121 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, 1122 dsl_dir_get_usedds(dd)); 1123 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, 1124 dsl_dir_get_usedrefreserv(dd)); 1125 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, 1126 dsl_dir_get_usedchild(dd)); 1127 } 1128 mutex_exit(&dd->dd_lock); 1129 1130 uint64_t count; 1131 if (dsl_dir_get_filesystem_count(dd, &count) == 0) { 1132 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT, 1133 count); 1134 } 1135 if (dsl_dir_get_snapshot_count(dd, &count) == 0) { 1136 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT, 1137 count); 1138 } 1139 1140 if (dsl_dir_is_clone(dd)) { 1141 char buf[ZFS_MAX_DATASET_NAME_LEN]; 1142 dsl_dir_get_origin(dd, buf); 1143 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); 1144 } 1145 1146 } 1147 1148 void 1149 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) 1150 { 1151 dsl_pool_t *dp = dd->dd_pool; 1152 1153 ASSERT(dsl_dir_phys(dd)); 1154 1155 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) { 1156 /* up the hold count until we can be written out */ 1157 dmu_buf_add_ref(dd->dd_dbuf, dd); 1158 } 1159 } 1160 1161 static int64_t 1162 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) 1163 { 1164 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved); 1165 uint64_t new_accounted = 1166 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved); 1167 return (new_accounted - old_accounted); 1168 } 1169 1170 void 1171 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) 1172 { 1173 ASSERT(dmu_tx_is_syncing(tx)); 1174 1175 mutex_enter(&dd->dd_lock); 1176 ASSERT0(dd->dd_tempreserved[tx->tx_txg & TXG_MASK]); 1177 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", (u_longlong_t)tx->tx_txg, 1178 (u_longlong_t)dd->dd_space_towrite[tx->tx_txg & TXG_MASK] / 1024); 1179 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] = 0; 1180 mutex_exit(&dd->dd_lock); 1181 1182 /* release the hold from dsl_dir_dirty */ 1183 dmu_buf_rele(dd->dd_dbuf, dd); 1184 } 1185 1186 static uint64_t 1187 dsl_dir_space_towrite(dsl_dir_t *dd) 1188 { 1189 uint64_t space = 0; 1190 1191 ASSERT(MUTEX_HELD(&dd->dd_lock)); 1192 1193 for (int i = 0; i < TXG_SIZE; i++) 1194 space += dd->dd_space_towrite[i & TXG_MASK]; 1195 1196 return (space); 1197 } 1198 1199 /* 1200 * How much space would dd have available if ancestor had delta applied 1201 * to it? If ondiskonly is set, we're only interested in what's 1202 * on-disk, not estimated pending changes. 1203 */ 1204 uint64_t 1205 dsl_dir_space_available(dsl_dir_t *dd, 1206 dsl_dir_t *ancestor, int64_t delta, int ondiskonly) 1207 { 1208 uint64_t parentspace, myspace, quota, used; 1209 1210 /* 1211 * If there are no restrictions otherwise, assume we have 1212 * unlimited space available. 1213 */ 1214 quota = UINT64_MAX; 1215 parentspace = UINT64_MAX; 1216 1217 if (dd->dd_parent != NULL) { 1218 parentspace = dsl_dir_space_available(dd->dd_parent, 1219 ancestor, delta, ondiskonly); 1220 } 1221 1222 mutex_enter(&dd->dd_lock); 1223 if (dsl_dir_phys(dd)->dd_quota != 0) 1224 quota = dsl_dir_phys(dd)->dd_quota; 1225 used = dsl_dir_phys(dd)->dd_used_bytes; 1226 if (!ondiskonly) 1227 used += dsl_dir_space_towrite(dd); 1228 1229 if (dd->dd_parent == NULL) { 1230 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, 1231 ZFS_SPACE_CHECK_NORMAL); 1232 quota = MIN(quota, poolsize); 1233 } 1234 1235 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) { 1236 /* 1237 * We have some space reserved, in addition to what our 1238 * parent gave us. 1239 */ 1240 parentspace += dsl_dir_phys(dd)->dd_reserved - used; 1241 } 1242 1243 if (dd == ancestor) { 1244 ASSERT(delta <= 0); 1245 ASSERT(used >= -delta); 1246 used += delta; 1247 if (parentspace != UINT64_MAX) 1248 parentspace -= delta; 1249 } 1250 1251 if (used > quota) { 1252 /* over quota */ 1253 myspace = 0; 1254 } else { 1255 /* 1256 * the lesser of the space provided by our parent and 1257 * the space left in our quota 1258 */ 1259 myspace = MIN(parentspace, quota - used); 1260 } 1261 1262 mutex_exit(&dd->dd_lock); 1263 1264 return (myspace); 1265 } 1266 1267 struct tempreserve { 1268 list_node_t tr_node; 1269 dsl_dir_t *tr_ds; 1270 uint64_t tr_size; 1271 }; 1272 1273 static int 1274 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, 1275 boolean_t ignorequota, list_t *tr_list, 1276 dmu_tx_t *tx, boolean_t first) 1277 { 1278 uint64_t txg; 1279 uint64_t quota; 1280 struct tempreserve *tr; 1281 int retval; 1282 uint64_t ext_quota; 1283 uint64_t ref_rsrv; 1284 1285 top_of_function: 1286 txg = tx->tx_txg; 1287 retval = EDQUOT; 1288 ref_rsrv = 0; 1289 1290 ASSERT3U(txg, !=, 0); 1291 ASSERT3S(asize, >, 0); 1292 1293 mutex_enter(&dd->dd_lock); 1294 1295 /* 1296 * Check against the dsl_dir's quota. We don't add in the delta 1297 * when checking for over-quota because they get one free hit. 1298 */ 1299 uint64_t est_inflight = dsl_dir_space_towrite(dd); 1300 for (int i = 0; i < TXG_SIZE; i++) 1301 est_inflight += dd->dd_tempreserved[i]; 1302 uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes; 1303 1304 /* 1305 * On the first iteration, fetch the dataset's used-on-disk and 1306 * refreservation values. Also, if checkrefquota is set, test if 1307 * allocating this space would exceed the dataset's refquota. 1308 */ 1309 if (first && tx->tx_objset) { 1310 int error; 1311 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset; 1312 1313 error = dsl_dataset_check_quota(ds, !netfree, 1314 asize, est_inflight, &used_on_disk, &ref_rsrv); 1315 if (error != 0) { 1316 mutex_exit(&dd->dd_lock); 1317 DMU_TX_STAT_BUMP(dmu_tx_quota); 1318 return (error); 1319 } 1320 } 1321 1322 /* 1323 * If this transaction will result in a net free of space, 1324 * we want to let it through. 1325 */ 1326 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0 || 1327 (tx->tx_objset && dmu_objset_type(tx->tx_objset) == DMU_OST_ZVOL && 1328 zvol_enforce_quotas == B_FALSE)) 1329 quota = UINT64_MAX; 1330 else 1331 quota = dsl_dir_phys(dd)->dd_quota; 1332 1333 /* 1334 * Adjust the quota against the actual pool size at the root 1335 * minus any outstanding deferred frees. 1336 * To ensure that it's possible to remove files from a full 1337 * pool without inducing transient overcommits, we throttle 1338 * netfree transactions against a quota that is slightly larger, 1339 * but still within the pool's allocation slop. In cases where 1340 * we're very close to full, this will allow a steady trickle of 1341 * removes to get through. 1342 */ 1343 if (dd->dd_parent == NULL) { 1344 uint64_t avail = dsl_pool_unreserved_space(dd->dd_pool, 1345 (netfree) ? 1346 ZFS_SPACE_CHECK_RESERVED : ZFS_SPACE_CHECK_NORMAL); 1347 1348 if (avail < quota) { 1349 quota = avail; 1350 retval = SET_ERROR(ENOSPC); 1351 } 1352 } 1353 1354 /* 1355 * If they are requesting more space, and our current estimate 1356 * is over quota, they get to try again unless the actual 1357 * on-disk is over quota and there are no pending changes 1358 * or deferred frees (which may free up space for us). 1359 */ 1360 ext_quota = quota >> 5; 1361 if (quota == UINT64_MAX) 1362 ext_quota = 0; 1363 1364 if (used_on_disk >= quota) { 1365 if (retval == ENOSPC && (used_on_disk - quota) < 1366 dsl_pool_deferred_space(dd->dd_pool)) { 1367 retval = SET_ERROR(ERESTART); 1368 } 1369 /* Quota exceeded */ 1370 mutex_exit(&dd->dd_lock); 1371 DMU_TX_STAT_BUMP(dmu_tx_quota); 1372 return (retval); 1373 } else if (used_on_disk + est_inflight >= quota + ext_quota) { 1374 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " 1375 "quota=%lluK tr=%lluK\n", 1376 (u_longlong_t)used_on_disk>>10, 1377 (u_longlong_t)est_inflight>>10, 1378 (u_longlong_t)quota>>10, (u_longlong_t)asize>>10); 1379 mutex_exit(&dd->dd_lock); 1380 DMU_TX_STAT_BUMP(dmu_tx_quota); 1381 return (SET_ERROR(ERESTART)); 1382 } 1383 1384 /* We need to up our estimated delta before dropping dd_lock */ 1385 dd->dd_tempreserved[txg & TXG_MASK] += asize; 1386 1387 uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, 1388 asize - ref_rsrv); 1389 mutex_exit(&dd->dd_lock); 1390 1391 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 1392 tr->tr_ds = dd; 1393 tr->tr_size = asize; 1394 list_insert_tail(tr_list, tr); 1395 1396 /* see if it's OK with our parent */ 1397 if (dd->dd_parent != NULL && parent_rsrv != 0) { 1398 /* 1399 * Recurse on our parent without recursion. This has been 1400 * observed to be potentially large stack usage even within 1401 * the test suite. Largest seen stack was 7632 bytes on linux. 1402 */ 1403 1404 dd = dd->dd_parent; 1405 asize = parent_rsrv; 1406 ignorequota = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 1407 first = B_FALSE; 1408 goto top_of_function; 1409 } 1410 1411 return (0); 1412 } 1413 1414 /* 1415 * Reserve space in this dsl_dir, to be used in this tx's txg. 1416 * After the space has been dirtied (and dsl_dir_willuse_space() 1417 * has been called), the reservation should be canceled, using 1418 * dsl_dir_tempreserve_clear(). 1419 */ 1420 int 1421 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, 1422 boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx) 1423 { 1424 int err; 1425 list_t *tr_list; 1426 1427 if (asize == 0) { 1428 *tr_cookiep = NULL; 1429 return (0); 1430 } 1431 1432 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP); 1433 list_create(tr_list, sizeof (struct tempreserve), 1434 offsetof(struct tempreserve, tr_node)); 1435 ASSERT3S(asize, >, 0); 1436 1437 err = arc_tempreserve_space(dd->dd_pool->dp_spa, lsize, tx->tx_txg); 1438 if (err == 0) { 1439 struct tempreserve *tr; 1440 1441 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 1442 tr->tr_size = lsize; 1443 list_insert_tail(tr_list, tr); 1444 } else { 1445 if (err == EAGAIN) { 1446 /* 1447 * If arc_memory_throttle() detected that pageout 1448 * is running and we are low on memory, we delay new 1449 * non-pageout transactions to give pageout an 1450 * advantage. 1451 * 1452 * It is unfortunate to be delaying while the caller's 1453 * locks are held. 1454 */ 1455 txg_delay(dd->dd_pool, tx->tx_txg, 1456 MSEC2NSEC(10), MSEC2NSEC(10)); 1457 err = SET_ERROR(ERESTART); 1458 } 1459 } 1460 1461 if (err == 0) { 1462 err = dsl_dir_tempreserve_impl(dd, asize, netfree, 1463 B_FALSE, tr_list, tx, B_TRUE); 1464 } 1465 1466 if (err != 0) 1467 dsl_dir_tempreserve_clear(tr_list, tx); 1468 else 1469 *tr_cookiep = tr_list; 1470 1471 return (err); 1472 } 1473 1474 /* 1475 * Clear a temporary reservation that we previously made with 1476 * dsl_dir_tempreserve_space(). 1477 */ 1478 void 1479 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) 1480 { 1481 int txgidx = tx->tx_txg & TXG_MASK; 1482 list_t *tr_list = tr_cookie; 1483 struct tempreserve *tr; 1484 1485 ASSERT3U(tx->tx_txg, !=, 0); 1486 1487 if (tr_cookie == NULL) 1488 return; 1489 1490 while ((tr = list_remove_head(tr_list)) != NULL) { 1491 if (tr->tr_ds) { 1492 mutex_enter(&tr->tr_ds->dd_lock); 1493 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, 1494 tr->tr_size); 1495 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; 1496 mutex_exit(&tr->tr_ds->dd_lock); 1497 } else { 1498 arc_tempreserve_clear(tr->tr_size); 1499 } 1500 kmem_free(tr, sizeof (struct tempreserve)); 1501 } 1502 1503 kmem_free(tr_list, sizeof (list_t)); 1504 } 1505 1506 /* 1507 * This should be called from open context when we think we're going to write 1508 * or free space, for example when dirtying data. Be conservative; it's okay 1509 * to write less space or free more, but we don't want to write more or free 1510 * less than the amount specified. 1511 * 1512 * NOTE: The behavior of this function is identical to the Illumos / FreeBSD 1513 * version however it has been adjusted to use an iterative rather than 1514 * recursive algorithm to minimize stack usage. 1515 */ 1516 void 1517 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 1518 { 1519 int64_t parent_space; 1520 uint64_t est_used; 1521 1522 do { 1523 mutex_enter(&dd->dd_lock); 1524 if (space > 0) 1525 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; 1526 1527 est_used = dsl_dir_space_towrite(dd) + 1528 dsl_dir_phys(dd)->dd_used_bytes; 1529 parent_space = parent_delta(dd, est_used, space); 1530 mutex_exit(&dd->dd_lock); 1531 1532 /* Make sure that we clean up dd_space_to* */ 1533 dsl_dir_dirty(dd, tx); 1534 1535 dd = dd->dd_parent; 1536 space = parent_space; 1537 } while (space && dd); 1538 } 1539 1540 /* call from syncing context when we actually write/free space for this dd */ 1541 void 1542 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, 1543 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) 1544 { 1545 int64_t accounted_delta; 1546 1547 ASSERT(dmu_tx_is_syncing(tx)); 1548 ASSERT(type < DD_USED_NUM); 1549 1550 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1551 1552 /* 1553 * dsl_dataset_set_refreservation_sync_impl() calls this with 1554 * dd_lock held, so that it can atomically update 1555 * ds->ds_reserved and the dsl_dir accounting, so that 1556 * dsl_dataset_check_quota() can see dataset and dir accounting 1557 * consistently. 1558 */ 1559 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 1560 if (needlock) 1561 mutex_enter(&dd->dd_lock); 1562 dsl_dir_phys_t *ddp = dsl_dir_phys(dd); 1563 accounted_delta = parent_delta(dd, ddp->dd_used_bytes, used); 1564 ASSERT(used >= 0 || ddp->dd_used_bytes >= -used); 1565 ASSERT(compressed >= 0 || ddp->dd_compressed_bytes >= -compressed); 1566 ASSERT(uncompressed >= 0 || 1567 ddp->dd_uncompressed_bytes >= -uncompressed); 1568 ddp->dd_used_bytes += used; 1569 ddp->dd_uncompressed_bytes += uncompressed; 1570 ddp->dd_compressed_bytes += compressed; 1571 1572 if (ddp->dd_flags & DD_FLAG_USED_BREAKDOWN) { 1573 ASSERT(used >= 0 || ddp->dd_used_breakdown[type] >= -used); 1574 ddp->dd_used_breakdown[type] += used; 1575 #ifdef ZFS_DEBUG 1576 { 1577 dd_used_t t; 1578 uint64_t u = 0; 1579 for (t = 0; t < DD_USED_NUM; t++) 1580 u += ddp->dd_used_breakdown[t]; 1581 ASSERT3U(u, ==, ddp->dd_used_bytes); 1582 } 1583 #endif 1584 } 1585 if (needlock) 1586 mutex_exit(&dd->dd_lock); 1587 1588 if (dd->dd_parent != NULL) { 1589 dsl_dir_diduse_transfer_space(dd->dd_parent, 1590 accounted_delta, compressed, uncompressed, 1591 used, DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); 1592 } 1593 } 1594 1595 void 1596 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, 1597 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) 1598 { 1599 ASSERT(dmu_tx_is_syncing(tx)); 1600 ASSERT(oldtype < DD_USED_NUM); 1601 ASSERT(newtype < DD_USED_NUM); 1602 1603 dsl_dir_phys_t *ddp = dsl_dir_phys(dd); 1604 if (delta == 0 || 1605 !(ddp->dd_flags & DD_FLAG_USED_BREAKDOWN)) 1606 return; 1607 1608 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1609 mutex_enter(&dd->dd_lock); 1610 ASSERT(delta > 0 ? 1611 ddp->dd_used_breakdown[oldtype] >= delta : 1612 ddp->dd_used_breakdown[newtype] >= -delta); 1613 ASSERT(ddp->dd_used_bytes >= ABS(delta)); 1614 ddp->dd_used_breakdown[oldtype] -= delta; 1615 ddp->dd_used_breakdown[newtype] += delta; 1616 mutex_exit(&dd->dd_lock); 1617 } 1618 1619 void 1620 dsl_dir_diduse_transfer_space(dsl_dir_t *dd, int64_t used, 1621 int64_t compressed, int64_t uncompressed, int64_t tonew, 1622 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) 1623 { 1624 int64_t accounted_delta; 1625 1626 ASSERT(dmu_tx_is_syncing(tx)); 1627 ASSERT(oldtype < DD_USED_NUM); 1628 ASSERT(newtype < DD_USED_NUM); 1629 1630 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1631 1632 mutex_enter(&dd->dd_lock); 1633 dsl_dir_phys_t *ddp = dsl_dir_phys(dd); 1634 accounted_delta = parent_delta(dd, ddp->dd_used_bytes, used); 1635 ASSERT(used >= 0 || ddp->dd_used_bytes >= -used); 1636 ASSERT(compressed >= 0 || ddp->dd_compressed_bytes >= -compressed); 1637 ASSERT(uncompressed >= 0 || 1638 ddp->dd_uncompressed_bytes >= -uncompressed); 1639 ddp->dd_used_bytes += used; 1640 ddp->dd_uncompressed_bytes += uncompressed; 1641 ddp->dd_compressed_bytes += compressed; 1642 1643 if (ddp->dd_flags & DD_FLAG_USED_BREAKDOWN) { 1644 ASSERT(tonew - used <= 0 || 1645 ddp->dd_used_breakdown[oldtype] >= tonew - used); 1646 ASSERT(tonew >= 0 || 1647 ddp->dd_used_breakdown[newtype] >= -tonew); 1648 ddp->dd_used_breakdown[oldtype] -= tonew - used; 1649 ddp->dd_used_breakdown[newtype] += tonew; 1650 #ifdef ZFS_DEBUG 1651 { 1652 dd_used_t t; 1653 uint64_t u = 0; 1654 for (t = 0; t < DD_USED_NUM; t++) 1655 u += ddp->dd_used_breakdown[t]; 1656 ASSERT3U(u, ==, ddp->dd_used_bytes); 1657 } 1658 #endif 1659 } 1660 mutex_exit(&dd->dd_lock); 1661 1662 if (dd->dd_parent != NULL) { 1663 dsl_dir_diduse_transfer_space(dd->dd_parent, 1664 accounted_delta, compressed, uncompressed, 1665 used, DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); 1666 } 1667 } 1668 1669 typedef struct dsl_dir_set_qr_arg { 1670 const char *ddsqra_name; 1671 zprop_source_t ddsqra_source; 1672 uint64_t ddsqra_value; 1673 } dsl_dir_set_qr_arg_t; 1674 1675 static int 1676 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx) 1677 { 1678 dsl_dir_set_qr_arg_t *ddsqra = arg; 1679 dsl_pool_t *dp = dmu_tx_pool(tx); 1680 dsl_dataset_t *ds; 1681 int error; 1682 uint64_t towrite, newval; 1683 1684 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 1685 if (error != 0) 1686 return (error); 1687 1688 error = dsl_prop_predict(ds->ds_dir, "quota", 1689 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 1690 if (error != 0) { 1691 dsl_dataset_rele(ds, FTAG); 1692 return (error); 1693 } 1694 1695 if (newval == 0) { 1696 dsl_dataset_rele(ds, FTAG); 1697 return (0); 1698 } 1699 1700 mutex_enter(&ds->ds_dir->dd_lock); 1701 /* 1702 * If we are doing the preliminary check in open context, and 1703 * there are pending changes, then don't fail it, since the 1704 * pending changes could under-estimate the amount of space to be 1705 * freed up. 1706 */ 1707 towrite = dsl_dir_space_towrite(ds->ds_dir); 1708 if ((dmu_tx_is_syncing(tx) || towrite == 0) && 1709 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved || 1710 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) { 1711 error = SET_ERROR(ENOSPC); 1712 } 1713 mutex_exit(&ds->ds_dir->dd_lock); 1714 dsl_dataset_rele(ds, FTAG); 1715 return (error); 1716 } 1717 1718 static void 1719 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx) 1720 { 1721 dsl_dir_set_qr_arg_t *ddsqra = arg; 1722 dsl_pool_t *dp = dmu_tx_pool(tx); 1723 dsl_dataset_t *ds; 1724 uint64_t newval; 1725 1726 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 1727 1728 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { 1729 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA), 1730 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 1731 &ddsqra->ddsqra_value, tx); 1732 1733 VERIFY0(dsl_prop_get_int_ds(ds, 1734 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval)); 1735 } else { 1736 newval = ddsqra->ddsqra_value; 1737 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", 1738 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval); 1739 } 1740 1741 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 1742 mutex_enter(&ds->ds_dir->dd_lock); 1743 dsl_dir_phys(ds->ds_dir)->dd_quota = newval; 1744 mutex_exit(&ds->ds_dir->dd_lock); 1745 dsl_dataset_rele(ds, FTAG); 1746 } 1747 1748 int 1749 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota) 1750 { 1751 dsl_dir_set_qr_arg_t ddsqra; 1752 1753 ddsqra.ddsqra_name = ddname; 1754 ddsqra.ddsqra_source = source; 1755 ddsqra.ddsqra_value = quota; 1756 1757 return (dsl_sync_task(ddname, dsl_dir_set_quota_check, 1758 dsl_dir_set_quota_sync, &ddsqra, 0, 1759 ZFS_SPACE_CHECK_EXTRA_RESERVED)); 1760 } 1761 1762 static int 1763 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx) 1764 { 1765 dsl_dir_set_qr_arg_t *ddsqra = arg; 1766 dsl_pool_t *dp = dmu_tx_pool(tx); 1767 dsl_dataset_t *ds; 1768 dsl_dir_t *dd; 1769 uint64_t newval, used, avail; 1770 int error; 1771 1772 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 1773 if (error != 0) 1774 return (error); 1775 dd = ds->ds_dir; 1776 1777 /* 1778 * If we are doing the preliminary check in open context, the 1779 * space estimates may be inaccurate. 1780 */ 1781 if (!dmu_tx_is_syncing(tx)) { 1782 dsl_dataset_rele(ds, FTAG); 1783 return (0); 1784 } 1785 1786 error = dsl_prop_predict(ds->ds_dir, 1787 zfs_prop_to_name(ZFS_PROP_RESERVATION), 1788 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 1789 if (error != 0) { 1790 dsl_dataset_rele(ds, FTAG); 1791 return (error); 1792 } 1793 1794 mutex_enter(&dd->dd_lock); 1795 used = dsl_dir_phys(dd)->dd_used_bytes; 1796 mutex_exit(&dd->dd_lock); 1797 1798 if (dd->dd_parent) { 1799 avail = dsl_dir_space_available(dd->dd_parent, 1800 NULL, 0, FALSE); 1801 } else { 1802 avail = dsl_pool_adjustedsize(dd->dd_pool, 1803 ZFS_SPACE_CHECK_NORMAL) - used; 1804 } 1805 1806 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) { 1807 uint64_t delta = MAX(used, newval) - 1808 MAX(used, dsl_dir_phys(dd)->dd_reserved); 1809 1810 if (delta > avail || 1811 (dsl_dir_phys(dd)->dd_quota > 0 && 1812 newval > dsl_dir_phys(dd)->dd_quota)) 1813 error = SET_ERROR(ENOSPC); 1814 } 1815 1816 dsl_dataset_rele(ds, FTAG); 1817 return (error); 1818 } 1819 1820 void 1821 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx) 1822 { 1823 uint64_t used; 1824 int64_t delta; 1825 1826 dmu_buf_will_dirty(dd->dd_dbuf, tx); 1827 1828 mutex_enter(&dd->dd_lock); 1829 used = dsl_dir_phys(dd)->dd_used_bytes; 1830 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved); 1831 dsl_dir_phys(dd)->dd_reserved = value; 1832 1833 if (dd->dd_parent != NULL) { 1834 /* Roll up this additional usage into our ancestors */ 1835 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 1836 delta, 0, 0, tx); 1837 } 1838 mutex_exit(&dd->dd_lock); 1839 } 1840 1841 static void 1842 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx) 1843 { 1844 dsl_dir_set_qr_arg_t *ddsqra = arg; 1845 dsl_pool_t *dp = dmu_tx_pool(tx); 1846 dsl_dataset_t *ds; 1847 uint64_t newval; 1848 1849 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 1850 1851 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { 1852 dsl_prop_set_sync_impl(ds, 1853 zfs_prop_to_name(ZFS_PROP_RESERVATION), 1854 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 1855 &ddsqra->ddsqra_value, tx); 1856 1857 VERIFY0(dsl_prop_get_int_ds(ds, 1858 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval)); 1859 } else { 1860 newval = ddsqra->ddsqra_value; 1861 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", 1862 zfs_prop_to_name(ZFS_PROP_RESERVATION), 1863 (longlong_t)newval); 1864 } 1865 1866 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx); 1867 dsl_dataset_rele(ds, FTAG); 1868 } 1869 1870 int 1871 dsl_dir_set_reservation(const char *ddname, zprop_source_t source, 1872 uint64_t reservation) 1873 { 1874 dsl_dir_set_qr_arg_t ddsqra; 1875 1876 ddsqra.ddsqra_name = ddname; 1877 ddsqra.ddsqra_source = source; 1878 ddsqra.ddsqra_value = reservation; 1879 1880 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check, 1881 dsl_dir_set_reservation_sync, &ddsqra, 0, 1882 ZFS_SPACE_CHECK_EXTRA_RESERVED)); 1883 } 1884 1885 static dsl_dir_t * 1886 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) 1887 { 1888 for (; ds1; ds1 = ds1->dd_parent) { 1889 dsl_dir_t *dd; 1890 for (dd = ds2; dd; dd = dd->dd_parent) { 1891 if (ds1 == dd) 1892 return (dd); 1893 } 1894 } 1895 return (NULL); 1896 } 1897 1898 /* 1899 * If delta is applied to dd, how much of that delta would be applied to 1900 * ancestor? Syncing context only. 1901 */ 1902 static int64_t 1903 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) 1904 { 1905 if (dd == ancestor) 1906 return (delta); 1907 1908 mutex_enter(&dd->dd_lock); 1909 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta); 1910 mutex_exit(&dd->dd_lock); 1911 return (would_change(dd->dd_parent, delta, ancestor)); 1912 } 1913 1914 typedef struct dsl_dir_rename_arg { 1915 const char *ddra_oldname; 1916 const char *ddra_newname; 1917 cred_t *ddra_cred; 1918 proc_t *ddra_proc; 1919 } dsl_dir_rename_arg_t; 1920 1921 typedef struct dsl_valid_rename_arg { 1922 int char_delta; 1923 int nest_delta; 1924 } dsl_valid_rename_arg_t; 1925 1926 static int 1927 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1928 { 1929 (void) dp; 1930 dsl_valid_rename_arg_t *dvra = arg; 1931 char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 1932 1933 dsl_dataset_name(ds, namebuf); 1934 1935 ASSERT3U(strnlen(namebuf, ZFS_MAX_DATASET_NAME_LEN), 1936 <, ZFS_MAX_DATASET_NAME_LEN); 1937 int namelen = strlen(namebuf) + dvra->char_delta; 1938 int depth = get_dataset_depth(namebuf) + dvra->nest_delta; 1939 1940 if (namelen >= ZFS_MAX_DATASET_NAME_LEN) 1941 return (SET_ERROR(ENAMETOOLONG)); 1942 if (dvra->nest_delta > 0 && depth >= zfs_max_dataset_nesting) 1943 return (SET_ERROR(ENAMETOOLONG)); 1944 return (0); 1945 } 1946 1947 static int 1948 dsl_dir_rename_check(void *arg, dmu_tx_t *tx) 1949 { 1950 dsl_dir_rename_arg_t *ddra = arg; 1951 dsl_pool_t *dp = dmu_tx_pool(tx); 1952 dsl_dir_t *dd, *newparent; 1953 dsl_valid_rename_arg_t dvra; 1954 dsl_dataset_t *parentds; 1955 objset_t *parentos; 1956 const char *mynewname; 1957 int error; 1958 1959 /* target dir should exist */ 1960 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL); 1961 if (error != 0) 1962 return (error); 1963 1964 /* new parent should exist */ 1965 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG, 1966 &newparent, &mynewname); 1967 if (error != 0) { 1968 dsl_dir_rele(dd, FTAG); 1969 return (error); 1970 } 1971 1972 /* can't rename to different pool */ 1973 if (dd->dd_pool != newparent->dd_pool) { 1974 dsl_dir_rele(newparent, FTAG); 1975 dsl_dir_rele(dd, FTAG); 1976 return (SET_ERROR(EXDEV)); 1977 } 1978 1979 /* new name should not already exist */ 1980 if (mynewname == NULL) { 1981 dsl_dir_rele(newparent, FTAG); 1982 dsl_dir_rele(dd, FTAG); 1983 return (SET_ERROR(EEXIST)); 1984 } 1985 1986 /* can't rename below anything but filesystems (eg. no ZVOLs) */ 1987 error = dsl_dataset_hold_obj(newparent->dd_pool, 1988 dsl_dir_phys(newparent)->dd_head_dataset_obj, FTAG, &parentds); 1989 if (error != 0) { 1990 dsl_dir_rele(newparent, FTAG); 1991 dsl_dir_rele(dd, FTAG); 1992 return (error); 1993 } 1994 error = dmu_objset_from_ds(parentds, &parentos); 1995 if (error != 0) { 1996 dsl_dataset_rele(parentds, FTAG); 1997 dsl_dir_rele(newparent, FTAG); 1998 dsl_dir_rele(dd, FTAG); 1999 return (error); 2000 } 2001 if (dmu_objset_type(parentos) != DMU_OST_ZFS) { 2002 dsl_dataset_rele(parentds, FTAG); 2003 dsl_dir_rele(newparent, FTAG); 2004 dsl_dir_rele(dd, FTAG); 2005 return (SET_ERROR(ZFS_ERR_WRONG_PARENT)); 2006 } 2007 dsl_dataset_rele(parentds, FTAG); 2008 2009 ASSERT3U(strnlen(ddra->ddra_newname, ZFS_MAX_DATASET_NAME_LEN), 2010 <, ZFS_MAX_DATASET_NAME_LEN); 2011 ASSERT3U(strnlen(ddra->ddra_oldname, ZFS_MAX_DATASET_NAME_LEN), 2012 <, ZFS_MAX_DATASET_NAME_LEN); 2013 dvra.char_delta = strlen(ddra->ddra_newname) 2014 - strlen(ddra->ddra_oldname); 2015 dvra.nest_delta = get_dataset_depth(ddra->ddra_newname) 2016 - get_dataset_depth(ddra->ddra_oldname); 2017 2018 /* if the name length is growing, validate child name lengths */ 2019 if (dvra.char_delta > 0 || dvra.nest_delta > 0) { 2020 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename, 2021 &dvra, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 2022 if (error != 0) { 2023 dsl_dir_rele(newparent, FTAG); 2024 dsl_dir_rele(dd, FTAG); 2025 return (error); 2026 } 2027 } 2028 2029 if (dmu_tx_is_syncing(tx)) { 2030 if (spa_feature_is_active(dp->dp_spa, 2031 SPA_FEATURE_FS_SS_LIMIT)) { 2032 /* 2033 * Although this is the check function and we don't 2034 * normally make on-disk changes in check functions, 2035 * we need to do that here. 2036 * 2037 * Ensure this portion of the tree's counts have been 2038 * initialized in case the new parent has limits set. 2039 */ 2040 dsl_dir_init_fs_ss_count(dd, tx); 2041 } 2042 } 2043 2044 if (newparent != dd->dd_parent) { 2045 /* is there enough space? */ 2046 uint64_t myspace = 2047 MAX(dsl_dir_phys(dd)->dd_used_bytes, 2048 dsl_dir_phys(dd)->dd_reserved); 2049 objset_t *os = dd->dd_pool->dp_meta_objset; 2050 uint64_t fs_cnt = 0; 2051 uint64_t ss_cnt = 0; 2052 2053 if (dsl_dir_is_zapified(dd)) { 2054 int err; 2055 2056 err = zap_lookup(os, dd->dd_object, 2057 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, 2058 &fs_cnt); 2059 if (err != ENOENT && err != 0) { 2060 dsl_dir_rele(newparent, FTAG); 2061 dsl_dir_rele(dd, FTAG); 2062 return (err); 2063 } 2064 2065 /* 2066 * have to add 1 for the filesystem itself that we're 2067 * moving 2068 */ 2069 fs_cnt++; 2070 2071 err = zap_lookup(os, dd->dd_object, 2072 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, 2073 &ss_cnt); 2074 if (err != ENOENT && err != 0) { 2075 dsl_dir_rele(newparent, FTAG); 2076 dsl_dir_rele(dd, FTAG); 2077 return (err); 2078 } 2079 } 2080 2081 /* check for encryption errors */ 2082 error = dsl_dir_rename_crypt_check(dd, newparent); 2083 if (error != 0) { 2084 dsl_dir_rele(newparent, FTAG); 2085 dsl_dir_rele(dd, FTAG); 2086 return (SET_ERROR(EACCES)); 2087 } 2088 2089 /* no rename into our descendant */ 2090 if (closest_common_ancestor(dd, newparent) == dd) { 2091 dsl_dir_rele(newparent, FTAG); 2092 dsl_dir_rele(dd, FTAG); 2093 return (SET_ERROR(EINVAL)); 2094 } 2095 2096 error = dsl_dir_transfer_possible(dd->dd_parent, 2097 newparent, fs_cnt, ss_cnt, myspace, 2098 ddra->ddra_cred, ddra->ddra_proc); 2099 if (error != 0) { 2100 dsl_dir_rele(newparent, FTAG); 2101 dsl_dir_rele(dd, FTAG); 2102 return (error); 2103 } 2104 } 2105 2106 dsl_dir_rele(newparent, FTAG); 2107 dsl_dir_rele(dd, FTAG); 2108 return (0); 2109 } 2110 2111 static void 2112 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx) 2113 { 2114 dsl_dir_rename_arg_t *ddra = arg; 2115 dsl_pool_t *dp = dmu_tx_pool(tx); 2116 dsl_dir_t *dd, *newparent; 2117 const char *mynewname; 2118 objset_t *mos = dp->dp_meta_objset; 2119 2120 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL)); 2121 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent, 2122 &mynewname)); 2123 2124 ASSERT3P(mynewname, !=, NULL); 2125 2126 /* Log this before we change the name. */ 2127 spa_history_log_internal_dd(dd, "rename", tx, 2128 "-> %s", ddra->ddra_newname); 2129 2130 if (newparent != dd->dd_parent) { 2131 objset_t *os = dd->dd_pool->dp_meta_objset; 2132 uint64_t fs_cnt = 0; 2133 uint64_t ss_cnt = 0; 2134 2135 /* 2136 * We already made sure the dd counts were initialized in the 2137 * check function. 2138 */ 2139 if (spa_feature_is_active(dp->dp_spa, 2140 SPA_FEATURE_FS_SS_LIMIT)) { 2141 VERIFY0(zap_lookup(os, dd->dd_object, 2142 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, 2143 &fs_cnt)); 2144 /* add 1 for the filesystem itself that we're moving */ 2145 fs_cnt++; 2146 2147 VERIFY0(zap_lookup(os, dd->dd_object, 2148 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, 2149 &ss_cnt)); 2150 } 2151 2152 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt, 2153 DD_FIELD_FILESYSTEM_COUNT, tx); 2154 dsl_fs_ss_count_adjust(newparent, fs_cnt, 2155 DD_FIELD_FILESYSTEM_COUNT, tx); 2156 2157 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt, 2158 DD_FIELD_SNAPSHOT_COUNT, tx); 2159 dsl_fs_ss_count_adjust(newparent, ss_cnt, 2160 DD_FIELD_SNAPSHOT_COUNT, tx); 2161 2162 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 2163 -dsl_dir_phys(dd)->dd_used_bytes, 2164 -dsl_dir_phys(dd)->dd_compressed_bytes, 2165 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx); 2166 dsl_dir_diduse_space(newparent, DD_USED_CHILD, 2167 dsl_dir_phys(dd)->dd_used_bytes, 2168 dsl_dir_phys(dd)->dd_compressed_bytes, 2169 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx); 2170 2171 if (dsl_dir_phys(dd)->dd_reserved > 2172 dsl_dir_phys(dd)->dd_used_bytes) { 2173 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved - 2174 dsl_dir_phys(dd)->dd_used_bytes; 2175 2176 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 2177 -unused_rsrv, 0, 0, tx); 2178 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV, 2179 unused_rsrv, 0, 0, tx); 2180 } 2181 } 2182 2183 dmu_buf_will_dirty(dd->dd_dbuf, tx); 2184 2185 /* remove from old parent zapobj */ 2186 VERIFY0(zap_remove(mos, 2187 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj, 2188 dd->dd_myname, tx)); 2189 2190 (void) strlcpy(dd->dd_myname, mynewname, 2191 sizeof (dd->dd_myname)); 2192 dsl_dir_rele(dd->dd_parent, dd); 2193 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object; 2194 VERIFY0(dsl_dir_hold_obj(dp, 2195 newparent->dd_object, NULL, dd, &dd->dd_parent)); 2196 2197 /* add to new parent zapobj */ 2198 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj, 2199 dd->dd_myname, 8, 1, &dd->dd_object, tx)); 2200 2201 /* TODO: A rename callback to avoid these layering violations. */ 2202 zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname); 2203 zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname, 2204 ddra->ddra_newname, B_TRUE); 2205 2206 dsl_prop_notify_all(dd); 2207 2208 dsl_dir_rele(newparent, FTAG); 2209 dsl_dir_rele(dd, FTAG); 2210 } 2211 2212 int 2213 dsl_dir_rename(const char *oldname, const char *newname) 2214 { 2215 dsl_dir_rename_arg_t ddra; 2216 2217 ddra.ddra_oldname = oldname; 2218 ddra.ddra_newname = newname; 2219 ddra.ddra_cred = CRED(); 2220 ddra.ddra_proc = curproc; 2221 2222 return (dsl_sync_task(oldname, 2223 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra, 2224 3, ZFS_SPACE_CHECK_RESERVED)); 2225 } 2226 2227 int 2228 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, 2229 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, 2230 cred_t *cr, proc_t *proc) 2231 { 2232 dsl_dir_t *ancestor; 2233 int64_t adelta; 2234 uint64_t avail; 2235 int err; 2236 2237 ancestor = closest_common_ancestor(sdd, tdd); 2238 adelta = would_change(sdd, -space, ancestor); 2239 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); 2240 if (avail < space) 2241 return (SET_ERROR(ENOSPC)); 2242 2243 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT, 2244 ancestor, cr, proc); 2245 if (err != 0) 2246 return (err); 2247 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT, 2248 ancestor, cr, proc); 2249 if (err != 0) 2250 return (err); 2251 2252 return (0); 2253 } 2254 2255 inode_timespec_t 2256 dsl_dir_snap_cmtime(dsl_dir_t *dd) 2257 { 2258 inode_timespec_t t; 2259 2260 mutex_enter(&dd->dd_lock); 2261 t = dd->dd_snap_cmtime; 2262 mutex_exit(&dd->dd_lock); 2263 2264 return (t); 2265 } 2266 2267 void 2268 dsl_dir_snap_cmtime_update(dsl_dir_t *dd, dmu_tx_t *tx) 2269 { 2270 dsl_pool_t *dp = dmu_tx_pool(tx); 2271 inode_timespec_t t; 2272 gethrestime(&t); 2273 2274 mutex_enter(&dd->dd_lock); 2275 dd->dd_snap_cmtime = t; 2276 if (spa_feature_is_enabled(dp->dp_spa, 2277 SPA_FEATURE_EXTENSIBLE_DATASET)) { 2278 objset_t *mos = dd->dd_pool->dp_meta_objset; 2279 uint64_t ddobj = dd->dd_object; 2280 dsl_dir_zapify(dd, tx); 2281 VERIFY0(zap_update(mos, ddobj, 2282 DD_FIELD_SNAPSHOTS_CHANGED, 2283 sizeof (uint64_t), 2284 sizeof (inode_timespec_t) / sizeof (uint64_t), 2285 &t, tx)); 2286 } 2287 mutex_exit(&dd->dd_lock); 2288 } 2289 2290 void 2291 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx) 2292 { 2293 objset_t *mos = dd->dd_pool->dp_meta_objset; 2294 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx); 2295 } 2296 2297 boolean_t 2298 dsl_dir_is_zapified(dsl_dir_t *dd) 2299 { 2300 dmu_object_info_t doi; 2301 2302 dmu_object_info_from_db(dd->dd_dbuf, &doi); 2303 return (doi.doi_type == DMU_OTN_ZAP_METADATA); 2304 } 2305 2306 int 2307 dsl_dir_livelist_open(dsl_dir_t *dd, uint64_t obj) 2308 { 2309 objset_t *mos = dd->dd_pool->dp_meta_objset; 2310 ASSERT(spa_feature_is_active(dd->dd_pool->dp_spa, 2311 SPA_FEATURE_LIVELIST)); 2312 int err = dsl_deadlist_open(&dd->dd_livelist, mos, obj); 2313 if (err != 0) 2314 return (err); 2315 bplist_create(&dd->dd_pending_allocs); 2316 bplist_create(&dd->dd_pending_frees); 2317 return (0); 2318 } 2319 2320 void 2321 dsl_dir_livelist_close(dsl_dir_t *dd) 2322 { 2323 dsl_deadlist_close(&dd->dd_livelist); 2324 bplist_destroy(&dd->dd_pending_allocs); 2325 bplist_destroy(&dd->dd_pending_frees); 2326 } 2327 2328 void 2329 dsl_dir_remove_livelist(dsl_dir_t *dd, dmu_tx_t *tx, boolean_t total) 2330 { 2331 uint64_t obj; 2332 dsl_pool_t *dp = dmu_tx_pool(tx); 2333 spa_t *spa = dp->dp_spa; 2334 livelist_condense_entry_t to_condense = spa->spa_to_condense; 2335 2336 if (!dsl_deadlist_is_open(&dd->dd_livelist)) 2337 return; 2338 2339 /* 2340 * If the livelist being removed is set to be condensed, stop the 2341 * condense zthr and indicate the cancellation in the spa_to_condense 2342 * struct in case the condense no-wait synctask has already started 2343 */ 2344 zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr; 2345 if (ll_condense_thread != NULL && 2346 (to_condense.ds != NULL) && (to_condense.ds->ds_dir == dd)) { 2347 /* 2348 * We use zthr_wait_cycle_done instead of zthr_cancel 2349 * because we don't want to destroy the zthr, just have 2350 * it skip its current task. 2351 */ 2352 spa->spa_to_condense.cancelled = B_TRUE; 2353 zthr_wait_cycle_done(ll_condense_thread); 2354 /* 2355 * If we've returned from zthr_wait_cycle_done without 2356 * clearing the to_condense data structure it's either 2357 * because the no-wait synctask has started (which is 2358 * indicated by 'syncing' field of to_condense) and we 2359 * can expect it to clear to_condense on its own. 2360 * Otherwise, we returned before the zthr ran. The 2361 * checkfunc will now fail as cancelled == B_TRUE so we 2362 * can safely NULL out ds, allowing a different dir's 2363 * livelist to be condensed. 2364 * 2365 * We can be sure that the to_condense struct will not 2366 * be repopulated at this stage because both this 2367 * function and dsl_livelist_try_condense execute in 2368 * syncing context. 2369 */ 2370 if ((spa->spa_to_condense.ds != NULL) && 2371 !spa->spa_to_condense.syncing) { 2372 dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf, 2373 spa); 2374 spa->spa_to_condense.ds = NULL; 2375 } 2376 } 2377 2378 dsl_dir_livelist_close(dd); 2379 VERIFY0(zap_lookup(dp->dp_meta_objset, dd->dd_object, 2380 DD_FIELD_LIVELIST, sizeof (uint64_t), 1, &obj)); 2381 VERIFY0(zap_remove(dp->dp_meta_objset, dd->dd_object, 2382 DD_FIELD_LIVELIST, tx)); 2383 if (total) { 2384 dsl_deadlist_free(dp->dp_meta_objset, obj, tx); 2385 spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx); 2386 } 2387 } 2388 2389 static int 2390 dsl_dir_activity_in_progress(dsl_dir_t *dd, dsl_dataset_t *ds, 2391 zfs_wait_activity_t activity, boolean_t *in_progress) 2392 { 2393 int error = 0; 2394 2395 ASSERT(MUTEX_HELD(&dd->dd_activity_lock)); 2396 2397 switch (activity) { 2398 case ZFS_WAIT_DELETEQ: { 2399 #ifdef _KERNEL 2400 objset_t *os; 2401 error = dmu_objset_from_ds(ds, &os); 2402 if (error != 0) 2403 break; 2404 2405 mutex_enter(&os->os_user_ptr_lock); 2406 void *user = dmu_objset_get_user(os); 2407 mutex_exit(&os->os_user_ptr_lock); 2408 if (dmu_objset_type(os) != DMU_OST_ZFS || 2409 user == NULL || zfs_get_vfs_flag_unmounted(os)) { 2410 *in_progress = B_FALSE; 2411 return (0); 2412 } 2413 2414 uint64_t readonly = B_FALSE; 2415 error = zfs_get_temporary_prop(ds, ZFS_PROP_READONLY, &readonly, 2416 NULL); 2417 2418 if (error != 0) 2419 break; 2420 2421 if (readonly || !spa_writeable(dd->dd_pool->dp_spa)) { 2422 *in_progress = B_FALSE; 2423 return (0); 2424 } 2425 2426 uint64_t count, unlinked_obj; 2427 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1, 2428 &unlinked_obj); 2429 if (error != 0) { 2430 dsl_dataset_rele(ds, FTAG); 2431 break; 2432 } 2433 error = zap_count(os, unlinked_obj, &count); 2434 2435 if (error == 0) 2436 *in_progress = (count != 0); 2437 break; 2438 #else 2439 /* 2440 * The delete queue is ZPL specific, and libzpool doesn't have 2441 * it. It doesn't make sense to wait for it. 2442 */ 2443 (void) ds; 2444 *in_progress = B_FALSE; 2445 break; 2446 #endif 2447 } 2448 default: 2449 panic("unrecognized value for activity %d", activity); 2450 } 2451 2452 return (error); 2453 } 2454 2455 int 2456 dsl_dir_wait(dsl_dir_t *dd, dsl_dataset_t *ds, zfs_wait_activity_t activity, 2457 boolean_t *waited) 2458 { 2459 int error = 0; 2460 boolean_t in_progress; 2461 dsl_pool_t *dp = dd->dd_pool; 2462 for (;;) { 2463 dsl_pool_config_enter(dp, FTAG); 2464 error = dsl_dir_activity_in_progress(dd, ds, activity, 2465 &in_progress); 2466 dsl_pool_config_exit(dp, FTAG); 2467 if (error != 0 || !in_progress) 2468 break; 2469 2470 *waited = B_TRUE; 2471 2472 if (cv_wait_sig(&dd->dd_activity_cv, &dd->dd_activity_lock) == 2473 0 || dd->dd_activity_cancelled) { 2474 error = SET_ERROR(EINTR); 2475 break; 2476 } 2477 } 2478 return (error); 2479 } 2480 2481 void 2482 dsl_dir_cancel_waiters(dsl_dir_t *dd) 2483 { 2484 mutex_enter(&dd->dd_activity_lock); 2485 dd->dd_activity_cancelled = B_TRUE; 2486 cv_broadcast(&dd->dd_activity_cv); 2487 while (dd->dd_activity_waiters > 0) 2488 cv_wait(&dd->dd_activity_cv, &dd->dd_activity_lock); 2489 mutex_exit(&dd->dd_activity_lock); 2490 } 2491 2492 #if defined(_KERNEL) 2493 EXPORT_SYMBOL(dsl_dir_set_quota); 2494 EXPORT_SYMBOL(dsl_dir_set_reservation); 2495 #endif 2496 2497 ZFS_MODULE_PARAM(zfs, , zvol_enforce_quotas, INT, ZMOD_RW, 2498 "Enable strict ZVOL quota enforcment"); 2499