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