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