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