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