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