dsl_prop.c revision 299431
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 24 * Copyright 2015, Joyent, Inc. 25 */ 26 27#include <sys/zfs_context.h> 28#include <sys/dmu.h> 29#include <sys/dmu_objset.h> 30#include <sys/dmu_tx.h> 31#include <sys/dsl_dataset.h> 32#include <sys/dsl_dir.h> 33#include <sys/dsl_prop.h> 34#include <sys/dsl_synctask.h> 35#include <sys/spa.h> 36#include <sys/zap.h> 37#include <sys/fs/zfs.h> 38 39#include "zfs_prop.h" 40 41#define ZPROP_INHERIT_SUFFIX "$inherit" 42#define ZPROP_RECVD_SUFFIX "$recvd" 43 44static int 45dodefault(zfs_prop_t prop, int intsz, int numints, void *buf) 46{ 47 /* 48 * The setonce properties are read-only, BUT they still 49 * have a default value that can be used as the initial 50 * value. 51 */ 52 if (prop == ZPROP_INVAL || 53 (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop))) 54 return (SET_ERROR(ENOENT)); 55 56 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 57 if (intsz != 1) 58 return (SET_ERROR(EOVERFLOW)); 59 (void) strncpy(buf, zfs_prop_default_string(prop), 60 numints); 61 } else { 62 if (intsz != 8 || numints < 1) 63 return (SET_ERROR(EOVERFLOW)); 64 65 *(uint64_t *)buf = zfs_prop_default_numeric(prop); 66 } 67 68 return (0); 69} 70 71int 72dsl_prop_get_dd(dsl_dir_t *dd, const char *propname, 73 int intsz, int numints, void *buf, char *setpoint, boolean_t snapshot) 74{ 75 int err = ENOENT; 76 dsl_dir_t *target = dd; 77 objset_t *mos = dd->dd_pool->dp_meta_objset; 78 zfs_prop_t prop; 79 boolean_t inheritable; 80 boolean_t inheriting = B_FALSE; 81 char *inheritstr; 82 char *recvdstr; 83 84 ASSERT(dsl_pool_config_held(dd->dd_pool)); 85 86 if (setpoint) 87 setpoint[0] = '\0'; 88 89 prop = zfs_name_to_prop(propname); 90 inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop)); 91 inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX); 92 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX); 93 94 /* 95 * Note: dd may become NULL, therefore we shouldn't dereference it 96 * after this loop. 97 */ 98 for (; dd != NULL; dd = dd->dd_parent) { 99 if (dd != target || snapshot) { 100 if (!inheritable) 101 break; 102 inheriting = B_TRUE; 103 } 104 105 /* Check for a local value. */ 106 err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj, 107 propname, intsz, numints, buf); 108 if (err != ENOENT) { 109 if (setpoint != NULL && err == 0) 110 dsl_dir_name(dd, setpoint); 111 break; 112 } 113 114 /* 115 * Skip the check for a received value if there is an explicit 116 * inheritance entry. 117 */ 118 err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj, 119 inheritstr); 120 if (err != 0 && err != ENOENT) 121 break; 122 123 if (err == ENOENT) { 124 /* Check for a received value. */ 125 err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj, 126 recvdstr, intsz, numints, buf); 127 if (err != ENOENT) { 128 if (setpoint != NULL && err == 0) { 129 if (inheriting) { 130 dsl_dir_name(dd, setpoint); 131 } else { 132 (void) strcpy(setpoint, 133 ZPROP_SOURCE_VAL_RECVD); 134 } 135 } 136 break; 137 } 138 } 139 140 /* 141 * If we found an explicit inheritance entry, err is zero even 142 * though we haven't yet found the value, so reinitializing err 143 * at the end of the loop (instead of at the beginning) ensures 144 * that err has a valid post-loop value. 145 */ 146 err = SET_ERROR(ENOENT); 147 } 148 149 if (err == ENOENT) 150 err = dodefault(prop, intsz, numints, buf); 151 152 strfree(inheritstr); 153 strfree(recvdstr); 154 155 return (err); 156} 157 158int 159dsl_prop_get_ds(dsl_dataset_t *ds, const char *propname, 160 int intsz, int numints, void *buf, char *setpoint) 161{ 162 zfs_prop_t prop = zfs_name_to_prop(propname); 163 boolean_t inheritable; 164 uint64_t zapobj; 165 166 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 167 inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop)); 168 zapobj = dsl_dataset_phys(ds)->ds_props_obj; 169 170 if (zapobj != 0) { 171 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 172 int err; 173 174 ASSERT(ds->ds_is_snapshot); 175 176 /* Check for a local value. */ 177 err = zap_lookup(mos, zapobj, propname, intsz, numints, buf); 178 if (err != ENOENT) { 179 if (setpoint != NULL && err == 0) 180 dsl_dataset_name(ds, setpoint); 181 return (err); 182 } 183 184 /* 185 * Skip the check for a received value if there is an explicit 186 * inheritance entry. 187 */ 188 if (inheritable) { 189 char *inheritstr = kmem_asprintf("%s%s", propname, 190 ZPROP_INHERIT_SUFFIX); 191 err = zap_contains(mos, zapobj, inheritstr); 192 strfree(inheritstr); 193 if (err != 0 && err != ENOENT) 194 return (err); 195 } 196 197 if (err == ENOENT) { 198 /* Check for a received value. */ 199 char *recvdstr = kmem_asprintf("%s%s", propname, 200 ZPROP_RECVD_SUFFIX); 201 err = zap_lookup(mos, zapobj, recvdstr, 202 intsz, numints, buf); 203 strfree(recvdstr); 204 if (err != ENOENT) { 205 if (setpoint != NULL && err == 0) 206 (void) strcpy(setpoint, 207 ZPROP_SOURCE_VAL_RECVD); 208 return (err); 209 } 210 } 211 } 212 213 return (dsl_prop_get_dd(ds->ds_dir, propname, 214 intsz, numints, buf, setpoint, ds->ds_is_snapshot)); 215} 216 217static dsl_prop_record_t * 218dsl_prop_record_find(dsl_dir_t *dd, const char *propname) 219{ 220 dsl_prop_record_t *pr = NULL; 221 222 ASSERT(MUTEX_HELD(&dd->dd_lock)); 223 224 for (pr = list_head(&dd->dd_props); 225 pr != NULL; pr = list_next(&dd->dd_props, pr)) { 226 if (strcmp(pr->pr_propname, propname) == 0) 227 break; 228 } 229 230 return (pr); 231} 232 233static dsl_prop_record_t * 234dsl_prop_record_create(dsl_dir_t *dd, const char *propname) 235{ 236 dsl_prop_record_t *pr; 237 238 ASSERT(MUTEX_HELD(&dd->dd_lock)); 239 240 pr = kmem_alloc(sizeof (dsl_prop_record_t), KM_SLEEP); 241 pr->pr_propname = spa_strdup(propname); 242 list_create(&pr->pr_cbs, sizeof (dsl_prop_cb_record_t), 243 offsetof(dsl_prop_cb_record_t, cbr_pr_node)); 244 list_insert_head(&dd->dd_props, pr); 245 246 return (pr); 247} 248 249void 250dsl_prop_init(dsl_dir_t *dd) 251{ 252 list_create(&dd->dd_props, sizeof (dsl_prop_record_t), 253 offsetof(dsl_prop_record_t, pr_node)); 254} 255 256void 257dsl_prop_fini(dsl_dir_t *dd) 258{ 259 dsl_prop_record_t *pr; 260 261 while ((pr = list_remove_head(&dd->dd_props)) != NULL) { 262 list_destroy(&pr->pr_cbs); 263 strfree((char *)pr->pr_propname); 264 kmem_free(pr, sizeof (dsl_prop_record_t)); 265 } 266 list_destroy(&dd->dd_props); 267} 268 269/* 270 * Register interest in the named property. We'll call the callback 271 * once to notify it of the current property value, and again each time 272 * the property changes, until this callback is unregistered. 273 * 274 * Return 0 on success, errno if the prop is not an integer value. 275 */ 276int 277dsl_prop_register(dsl_dataset_t *ds, const char *propname, 278 dsl_prop_changed_cb_t *callback, void *cbarg) 279{ 280 dsl_dir_t *dd = ds->ds_dir; 281 dsl_pool_t *dp = dd->dd_pool; 282 uint64_t value; 283 dsl_prop_record_t *pr; 284 dsl_prop_cb_record_t *cbr; 285 int err; 286 287 ASSERT(dsl_pool_config_held(dp)); 288 289 err = dsl_prop_get_int_ds(ds, propname, &value); 290 if (err != 0) 291 return (err); 292 293 cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP); 294 cbr->cbr_ds = ds; 295 cbr->cbr_func = callback; 296 cbr->cbr_arg = cbarg; 297 298 mutex_enter(&dd->dd_lock); 299 pr = dsl_prop_record_find(dd, propname); 300 if (pr == NULL) 301 pr = dsl_prop_record_create(dd, propname); 302 cbr->cbr_pr = pr; 303 list_insert_head(&pr->pr_cbs, cbr); 304 list_insert_head(&ds->ds_prop_cbs, cbr); 305 mutex_exit(&dd->dd_lock); 306 307 cbr->cbr_func(cbr->cbr_arg, value); 308 return (0); 309} 310 311int 312dsl_prop_get(const char *dsname, const char *propname, 313 int intsz, int numints, void *buf, char *setpoint) 314{ 315 objset_t *os; 316 int error; 317 318 error = dmu_objset_hold(dsname, FTAG, &os); 319 if (error != 0) 320 return (error); 321 322 error = dsl_prop_get_ds(dmu_objset_ds(os), propname, 323 intsz, numints, buf, setpoint); 324 325 dmu_objset_rele(os, FTAG); 326 return (error); 327} 328 329/* 330 * Get the current property value. It may have changed by the time this 331 * function returns, so it is NOT safe to follow up with 332 * dsl_prop_register() and assume that the value has not changed in 333 * between. 334 * 335 * Return 0 on success, ENOENT if ddname is invalid. 336 */ 337int 338dsl_prop_get_integer(const char *ddname, const char *propname, 339 uint64_t *valuep, char *setpoint) 340{ 341 return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint)); 342} 343 344int 345dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname, 346 uint64_t *valuep) 347{ 348 return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL)); 349} 350 351/* 352 * Predict the effective value of the given special property if it were set with 353 * the given value and source. This is not a general purpose function. It exists 354 * only to handle the special requirements of the quota and reservation 355 * properties. The fact that these properties are non-inheritable greatly 356 * simplifies the prediction logic. 357 * 358 * Returns 0 on success, a positive error code on failure, or -1 if called with 359 * a property not handled by this function. 360 */ 361int 362dsl_prop_predict(dsl_dir_t *dd, const char *propname, 363 zprop_source_t source, uint64_t value, uint64_t *newvalp) 364{ 365 zfs_prop_t prop = zfs_name_to_prop(propname); 366 objset_t *mos; 367 uint64_t zapobj; 368 uint64_t version; 369 char *recvdstr; 370 int err = 0; 371 372 switch (prop) { 373 case ZFS_PROP_QUOTA: 374 case ZFS_PROP_RESERVATION: 375 case ZFS_PROP_REFQUOTA: 376 case ZFS_PROP_REFRESERVATION: 377 break; 378 default: 379 return (-1); 380 } 381 382 mos = dd->dd_pool->dp_meta_objset; 383 zapobj = dsl_dir_phys(dd)->dd_props_zapobj; 384 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX); 385 386 version = spa_version(dd->dd_pool->dp_spa); 387 if (version < SPA_VERSION_RECVD_PROPS) { 388 if (source & ZPROP_SRC_NONE) 389 source = ZPROP_SRC_NONE; 390 else if (source & ZPROP_SRC_RECEIVED) 391 source = ZPROP_SRC_LOCAL; 392 } 393 394 switch (source) { 395 case ZPROP_SRC_NONE: 396 /* Revert to the received value, if any. */ 397 err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp); 398 if (err == ENOENT) 399 *newvalp = 0; 400 break; 401 case ZPROP_SRC_LOCAL: 402 *newvalp = value; 403 break; 404 case ZPROP_SRC_RECEIVED: 405 /* 406 * If there's no local setting, then the new received value will 407 * be the effective value. 408 */ 409 err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp); 410 if (err == ENOENT) 411 *newvalp = value; 412 break; 413 case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED): 414 /* 415 * We're clearing the received value, so the local setting (if 416 * it exists) remains the effective value. 417 */ 418 err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp); 419 if (err == ENOENT) 420 *newvalp = 0; 421 break; 422 default: 423 panic("unexpected property source: %d", source); 424 } 425 426 strfree(recvdstr); 427 428 if (err == ENOENT) 429 return (0); 430 431 return (err); 432} 433 434/* 435 * Unregister all callbacks that are registered with the 436 * given callback argument. 437 */ 438void 439dsl_prop_unregister_all(dsl_dataset_t *ds, void *cbarg) 440{ 441 dsl_prop_cb_record_t *cbr, *next_cbr; 442 443 dsl_dir_t *dd = ds->ds_dir; 444 445 mutex_enter(&dd->dd_lock); 446 next_cbr = list_head(&ds->ds_prop_cbs); 447 while (next_cbr != NULL) { 448 cbr = next_cbr; 449 next_cbr = list_next(&ds->ds_prop_cbs, cbr); 450 if (cbr->cbr_arg == cbarg) { 451 list_remove(&ds->ds_prop_cbs, cbr); 452 list_remove(&cbr->cbr_pr->pr_cbs, cbr); 453 kmem_free(cbr, sizeof (dsl_prop_cb_record_t)); 454 } 455 } 456 mutex_exit(&dd->dd_lock); 457} 458 459boolean_t 460dsl_prop_hascb(dsl_dataset_t *ds) 461{ 462 return (!list_is_empty(&ds->ds_prop_cbs)); 463} 464 465/* ARGSUSED */ 466static int 467dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 468{ 469 dsl_dir_t *dd = ds->ds_dir; 470 dsl_prop_record_t *pr; 471 dsl_prop_cb_record_t *cbr; 472 473 mutex_enter(&dd->dd_lock); 474 for (pr = list_head(&dd->dd_props); 475 pr; pr = list_next(&dd->dd_props, pr)) { 476 for (cbr = list_head(&pr->pr_cbs); cbr; 477 cbr = list_next(&pr->pr_cbs, cbr)) { 478 uint64_t value; 479 480 /* 481 * Callback entries do not have holds on their 482 * datasets so that datasets with registered 483 * callbacks are still eligible for eviction. 484 * Unlike operations to update properties on a 485 * single dataset, we are performing a recursive 486 * descent of related head datasets. The caller 487 * of this function only has a dataset hold on 488 * the passed in head dataset, not the snapshots 489 * associated with this dataset. Without a hold, 490 * the dataset pointer within callback records 491 * for snapshots can be invalidated by eviction 492 * at any time. 493 * 494 * Use dsl_dataset_try_add_ref() to verify 495 * that the dataset for a snapshot has not 496 * begun eviction processing and to prevent 497 * eviction from occurring for the duration of 498 * the callback. If the hold attempt fails, 499 * this object is already being evicted and the 500 * callback can be safely ignored. 501 */ 502 if (ds != cbr->cbr_ds && 503 !dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG)) 504 continue; 505 506 if (dsl_prop_get_ds(cbr->cbr_ds, 507 cbr->cbr_pr->pr_propname, sizeof (value), 1, 508 &value, NULL) == 0) 509 cbr->cbr_func(cbr->cbr_arg, value); 510 511 if (ds != cbr->cbr_ds) 512 dsl_dataset_rele(cbr->cbr_ds, FTAG); 513 } 514 } 515 mutex_exit(&dd->dd_lock); 516 517 return (0); 518} 519 520/* 521 * Update all property values for ddobj & its descendants. This is used 522 * when renaming the dir. 523 */ 524void 525dsl_prop_notify_all(dsl_dir_t *dd) 526{ 527 dsl_pool_t *dp = dd->dd_pool; 528 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 529 (void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb, 530 NULL, DS_FIND_CHILDREN); 531} 532 533static void 534dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj, 535 const char *propname, uint64_t value, int first) 536{ 537 dsl_dir_t *dd; 538 dsl_prop_record_t *pr; 539 dsl_prop_cb_record_t *cbr; 540 objset_t *mos = dp->dp_meta_objset; 541 zap_cursor_t zc; 542 zap_attribute_t *za; 543 int err; 544 545 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 546 err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd); 547 if (err) 548 return; 549 550 if (!first) { 551 /* 552 * If the prop is set here, then this change is not 553 * being inherited here or below; stop the recursion. 554 */ 555 err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj, 556 propname); 557 if (err == 0) { 558 dsl_dir_rele(dd, FTAG); 559 return; 560 } 561 ASSERT3U(err, ==, ENOENT); 562 } 563 564 mutex_enter(&dd->dd_lock); 565 pr = dsl_prop_record_find(dd, propname); 566 if (pr != NULL) { 567 for (cbr = list_head(&pr->pr_cbs); cbr; 568 cbr = list_next(&pr->pr_cbs, cbr)) { 569 uint64_t propobj; 570 571 /* 572 * cbr->cbr_ds may be invalidated due to eviction, 573 * requiring the use of dsl_dataset_try_add_ref(). 574 * See comment block in dsl_prop_notify_all_cb() 575 * for details. 576 */ 577 if (!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG)) 578 continue; 579 580 propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj; 581 582 /* 583 * If the property is not set on this ds, then it is 584 * inherited here; call the callback. 585 */ 586 if (propobj == 0 || 587 zap_contains(mos, propobj, propname) != 0) 588 cbr->cbr_func(cbr->cbr_arg, value); 589 590 dsl_dataset_rele(cbr->cbr_ds, FTAG); 591 } 592 } 593 mutex_exit(&dd->dd_lock); 594 595 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 596 for (zap_cursor_init(&zc, mos, 597 dsl_dir_phys(dd)->dd_child_dir_zapobj); 598 zap_cursor_retrieve(&zc, za) == 0; 599 zap_cursor_advance(&zc)) { 600 dsl_prop_changed_notify(dp, za->za_first_integer, 601 propname, value, FALSE); 602 } 603 kmem_free(za, sizeof (zap_attribute_t)); 604 zap_cursor_fini(&zc); 605 dsl_dir_rele(dd, FTAG); 606} 607 608void 609dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname, 610 zprop_source_t source, int intsz, int numints, const void *value, 611 dmu_tx_t *tx) 612{ 613 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 614 uint64_t zapobj, intval, dummy; 615 int isint; 616 char valbuf[32]; 617 const char *valstr = NULL; 618 char *inheritstr; 619 char *recvdstr; 620 char *tbuf = NULL; 621 int err; 622 uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa); 623 624 isint = (dodefault(zfs_name_to_prop(propname), 8, 1, &intval) == 0); 625 626 if (ds->ds_is_snapshot) { 627 ASSERT(version >= SPA_VERSION_SNAP_PROPS); 628 if (dsl_dataset_phys(ds)->ds_props_obj == 0) { 629 dmu_buf_will_dirty(ds->ds_dbuf, tx); 630 dsl_dataset_phys(ds)->ds_props_obj = 631 zap_create(mos, 632 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 633 } 634 zapobj = dsl_dataset_phys(ds)->ds_props_obj; 635 } else { 636 zapobj = dsl_dir_phys(ds->ds_dir)->dd_props_zapobj; 637 } 638 639 if (version < SPA_VERSION_RECVD_PROPS) { 640 if (source & ZPROP_SRC_NONE) 641 source = ZPROP_SRC_NONE; 642 else if (source & ZPROP_SRC_RECEIVED) 643 source = ZPROP_SRC_LOCAL; 644 } 645 646 inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX); 647 recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX); 648 649 switch (source) { 650 case ZPROP_SRC_NONE: 651 /* 652 * revert to received value, if any (inherit -S) 653 * - remove propname 654 * - remove propname$inherit 655 */ 656 err = zap_remove(mos, zapobj, propname, tx); 657 ASSERT(err == 0 || err == ENOENT); 658 err = zap_remove(mos, zapobj, inheritstr, tx); 659 ASSERT(err == 0 || err == ENOENT); 660 break; 661 case ZPROP_SRC_LOCAL: 662 /* 663 * remove propname$inherit 664 * set propname -> value 665 */ 666 err = zap_remove(mos, zapobj, inheritstr, tx); 667 ASSERT(err == 0 || err == ENOENT); 668 VERIFY0(zap_update(mos, zapobj, propname, 669 intsz, numints, value, tx)); 670 break; 671 case ZPROP_SRC_INHERITED: 672 /* 673 * explicitly inherit 674 * - remove propname 675 * - set propname$inherit 676 */ 677 err = zap_remove(mos, zapobj, propname, tx); 678 ASSERT(err == 0 || err == ENOENT); 679 if (version >= SPA_VERSION_RECVD_PROPS && 680 dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) { 681 dummy = 0; 682 VERIFY0(zap_update(mos, zapobj, inheritstr, 683 8, 1, &dummy, tx)); 684 } 685 break; 686 case ZPROP_SRC_RECEIVED: 687 /* 688 * set propname$recvd -> value 689 */ 690 err = zap_update(mos, zapobj, recvdstr, 691 intsz, numints, value, tx); 692 ASSERT(err == 0); 693 break; 694 case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED): 695 /* 696 * clear local and received settings 697 * - remove propname 698 * - remove propname$inherit 699 * - remove propname$recvd 700 */ 701 err = zap_remove(mos, zapobj, propname, tx); 702 ASSERT(err == 0 || err == ENOENT); 703 err = zap_remove(mos, zapobj, inheritstr, tx); 704 ASSERT(err == 0 || err == ENOENT); 705 /* FALLTHRU */ 706 case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED): 707 /* 708 * remove propname$recvd 709 */ 710 err = zap_remove(mos, zapobj, recvdstr, tx); 711 ASSERT(err == 0 || err == ENOENT); 712 break; 713 default: 714 cmn_err(CE_PANIC, "unexpected property source: %d", source); 715 } 716 717 strfree(inheritstr); 718 strfree(recvdstr); 719 720 if (isint) { 721 VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval)); 722 723 if (ds->ds_is_snapshot) { 724 dsl_prop_cb_record_t *cbr; 725 /* 726 * It's a snapshot; nothing can inherit this 727 * property, so just look for callbacks on this 728 * ds here. 729 */ 730 mutex_enter(&ds->ds_dir->dd_lock); 731 for (cbr = list_head(&ds->ds_prop_cbs); cbr; 732 cbr = list_next(&ds->ds_prop_cbs, cbr)) { 733 if (strcmp(cbr->cbr_pr->pr_propname, 734 propname) == 0) 735 cbr->cbr_func(cbr->cbr_arg, intval); 736 } 737 mutex_exit(&ds->ds_dir->dd_lock); 738 } else { 739 dsl_prop_changed_notify(ds->ds_dir->dd_pool, 740 ds->ds_dir->dd_object, propname, intval, TRUE); 741 } 742 743 (void) snprintf(valbuf, sizeof (valbuf), 744 "%lld", (longlong_t)intval); 745 valstr = valbuf; 746 } else { 747 if (source == ZPROP_SRC_LOCAL) { 748 valstr = value; 749 } else { 750 tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP); 751 if (dsl_prop_get_ds(ds, propname, 1, 752 ZAP_MAXVALUELEN, tbuf, NULL) == 0) 753 valstr = tbuf; 754 } 755 } 756 757 spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE || 758 source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx, 759 "%s=%s", propname, (valstr == NULL ? "" : valstr)); 760 761 if (tbuf != NULL) 762 kmem_free(tbuf, ZAP_MAXVALUELEN); 763} 764 765int 766dsl_prop_set_int(const char *dsname, const char *propname, 767 zprop_source_t source, uint64_t value) 768{ 769 nvlist_t *nvl = fnvlist_alloc(); 770 int error; 771 772 fnvlist_add_uint64(nvl, propname, value); 773 error = dsl_props_set(dsname, source, nvl); 774 fnvlist_free(nvl); 775 return (error); 776} 777 778int 779dsl_prop_set_string(const char *dsname, const char *propname, 780 zprop_source_t source, const char *value) 781{ 782 nvlist_t *nvl = fnvlist_alloc(); 783 int error; 784 785 fnvlist_add_string(nvl, propname, value); 786 error = dsl_props_set(dsname, source, nvl); 787 fnvlist_free(nvl); 788 return (error); 789} 790 791int 792dsl_prop_inherit(const char *dsname, const char *propname, 793 zprop_source_t source) 794{ 795 nvlist_t *nvl = fnvlist_alloc(); 796 int error; 797 798 fnvlist_add_boolean(nvl, propname); 799 error = dsl_props_set(dsname, source, nvl); 800 fnvlist_free(nvl); 801 return (error); 802} 803 804typedef struct dsl_props_set_arg { 805 const char *dpsa_dsname; 806 zprop_source_t dpsa_source; 807 nvlist_t *dpsa_props; 808} dsl_props_set_arg_t; 809 810static int 811dsl_props_set_check(void *arg, dmu_tx_t *tx) 812{ 813 dsl_props_set_arg_t *dpsa = arg; 814 dsl_pool_t *dp = dmu_tx_pool(tx); 815 dsl_dataset_t *ds; 816 uint64_t version; 817 nvpair_t *elem = NULL; 818 int err; 819 820 err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds); 821 if (err != 0) 822 return (err); 823 824 version = spa_version(ds->ds_dir->dd_pool->dp_spa); 825 while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) { 826 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 827 dsl_dataset_rele(ds, FTAG); 828 return (SET_ERROR(ENAMETOOLONG)); 829 } 830 if (nvpair_type(elem) == DATA_TYPE_STRING) { 831 char *valstr = fnvpair_value_string(elem); 832 if (strlen(valstr) >= (version < 833 SPA_VERSION_STMF_PROP ? 834 ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) { 835 dsl_dataset_rele(ds, FTAG); 836 return (E2BIG); 837 } 838 } 839 } 840 841 if (ds->ds_is_snapshot && version < SPA_VERSION_SNAP_PROPS) { 842 dsl_dataset_rele(ds, FTAG); 843 return (SET_ERROR(ENOTSUP)); 844 } 845 dsl_dataset_rele(ds, FTAG); 846 return (0); 847} 848 849void 850dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source, 851 nvlist_t *props, dmu_tx_t *tx) 852{ 853 nvpair_t *elem = NULL; 854 855 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 856 nvpair_t *pair = elem; 857 858 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 859 /* 860 * dsl_prop_get_all_impl() returns properties in this 861 * format. 862 */ 863 nvlist_t *attrs = fnvpair_value_nvlist(pair); 864 pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE); 865 } 866 867 if (nvpair_type(pair) == DATA_TYPE_STRING) { 868 const char *value = fnvpair_value_string(pair); 869 dsl_prop_set_sync_impl(ds, nvpair_name(pair), 870 source, 1, strlen(value) + 1, value, tx); 871 } else if (nvpair_type(pair) == DATA_TYPE_UINT64) { 872 uint64_t intval = fnvpair_value_uint64(pair); 873 dsl_prop_set_sync_impl(ds, nvpair_name(pair), 874 source, sizeof (intval), 1, &intval, tx); 875 } else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) { 876 dsl_prop_set_sync_impl(ds, nvpair_name(pair), 877 source, 0, 0, NULL, tx); 878 } else { 879 panic("invalid nvpair type"); 880 } 881 } 882} 883 884static void 885dsl_props_set_sync(void *arg, dmu_tx_t *tx) 886{ 887 dsl_props_set_arg_t *dpsa = arg; 888 dsl_pool_t *dp = dmu_tx_pool(tx); 889 dsl_dataset_t *ds; 890 891 VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds)); 892 dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx); 893 dsl_dataset_rele(ds, FTAG); 894} 895 896/* 897 * All-or-nothing; if any prop can't be set, nothing will be modified. 898 */ 899int 900dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props) 901{ 902 dsl_props_set_arg_t dpsa; 903 int nblks = 0; 904 905 dpsa.dpsa_dsname = dsname; 906 dpsa.dpsa_source = source; 907 dpsa.dpsa_props = props; 908 909 /* 910 * If the source includes NONE, then we will only be removing entries 911 * from the ZAP object. In that case don't check for ENOSPC. 912 */ 913 if ((source & ZPROP_SRC_NONE) == 0) 914 nblks = 2 * fnvlist_num_pairs(props); 915 916 return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync, 917 &dpsa, nblks, ZFS_SPACE_CHECK_RESERVED)); 918} 919 920typedef enum dsl_prop_getflags { 921 DSL_PROP_GET_INHERITING = 0x1, /* searching parent of target ds */ 922 DSL_PROP_GET_SNAPSHOT = 0x2, /* snapshot dataset */ 923 DSL_PROP_GET_LOCAL = 0x4, /* local properties */ 924 DSL_PROP_GET_RECEIVED = 0x8 /* received properties */ 925} dsl_prop_getflags_t; 926 927static int 928dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj, 929 const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv) 930{ 931 zap_cursor_t zc; 932 zap_attribute_t za; 933 int err = 0; 934 935 for (zap_cursor_init(&zc, mos, propobj); 936 (err = zap_cursor_retrieve(&zc, &za)) == 0; 937 zap_cursor_advance(&zc)) { 938 nvlist_t *propval; 939 zfs_prop_t prop; 940 char buf[ZAP_MAXNAMELEN]; 941 char *valstr; 942 const char *suffix; 943 const char *propname; 944 const char *source; 945 946 suffix = strchr(za.za_name, '$'); 947 948 if (suffix == NULL) { 949 /* 950 * Skip local properties if we only want received 951 * properties. 952 */ 953 if (flags & DSL_PROP_GET_RECEIVED) 954 continue; 955 956 propname = za.za_name; 957 source = setpoint; 958 } else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) { 959 /* Skip explicitly inherited entries. */ 960 continue; 961 } else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) { 962 if (flags & DSL_PROP_GET_LOCAL) 963 continue; 964 965 (void) strncpy(buf, za.za_name, (suffix - za.za_name)); 966 buf[suffix - za.za_name] = '\0'; 967 propname = buf; 968 969 if (!(flags & DSL_PROP_GET_RECEIVED)) { 970 /* Skip if locally overridden. */ 971 err = zap_contains(mos, propobj, propname); 972 if (err == 0) 973 continue; 974 if (err != ENOENT) 975 break; 976 977 /* Skip if explicitly inherited. */ 978 valstr = kmem_asprintf("%s%s", propname, 979 ZPROP_INHERIT_SUFFIX); 980 err = zap_contains(mos, propobj, valstr); 981 strfree(valstr); 982 if (err == 0) 983 continue; 984 if (err != ENOENT) 985 break; 986 } 987 988 source = ((flags & DSL_PROP_GET_INHERITING) ? 989 setpoint : ZPROP_SOURCE_VAL_RECVD); 990 } else { 991 /* 992 * For backward compatibility, skip suffixes we don't 993 * recognize. 994 */ 995 continue; 996 } 997 998 prop = zfs_name_to_prop(propname); 999 1000 /* Skip non-inheritable properties. */ 1001 if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL && 1002 !zfs_prop_inheritable(prop)) 1003 continue; 1004 1005 /* Skip properties not valid for this type. */ 1006 if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL && 1007 !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT)) 1008 continue; 1009 1010 /* Skip properties already defined. */ 1011 if (nvlist_exists(nv, propname)) 1012 continue; 1013 1014 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1015 if (za.za_integer_length == 1) { 1016 /* 1017 * String property 1018 */ 1019 char *tmp = kmem_alloc(za.za_num_integers, 1020 KM_SLEEP); 1021 err = zap_lookup(mos, propobj, 1022 za.za_name, 1, za.za_num_integers, tmp); 1023 if (err != 0) { 1024 kmem_free(tmp, za.za_num_integers); 1025 break; 1026 } 1027 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, 1028 tmp) == 0); 1029 kmem_free(tmp, za.za_num_integers); 1030 } else { 1031 /* 1032 * Integer property 1033 */ 1034 ASSERT(za.za_integer_length == 8); 1035 (void) nvlist_add_uint64(propval, ZPROP_VALUE, 1036 za.za_first_integer); 1037 } 1038 1039 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0); 1040 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 1041 nvlist_free(propval); 1042 } 1043 zap_cursor_fini(&zc); 1044 if (err == ENOENT) 1045 err = 0; 1046 return (err); 1047} 1048 1049/* 1050 * Iterate over all properties for this dataset and return them in an nvlist. 1051 */ 1052static int 1053dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp, 1054 dsl_prop_getflags_t flags) 1055{ 1056 dsl_dir_t *dd = ds->ds_dir; 1057 dsl_pool_t *dp = dd->dd_pool; 1058 objset_t *mos = dp->dp_meta_objset; 1059 int err = 0; 1060 char setpoint[MAXNAMELEN]; 1061 1062 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1063 1064 if (ds->ds_is_snapshot) 1065 flags |= DSL_PROP_GET_SNAPSHOT; 1066 1067 ASSERT(dsl_pool_config_held(dp)); 1068 1069 if (dsl_dataset_phys(ds)->ds_props_obj != 0) { 1070 ASSERT(flags & DSL_PROP_GET_SNAPSHOT); 1071 dsl_dataset_name(ds, setpoint); 1072 err = dsl_prop_get_all_impl(mos, 1073 dsl_dataset_phys(ds)->ds_props_obj, setpoint, flags, *nvp); 1074 if (err) 1075 goto out; 1076 } 1077 1078 for (; dd != NULL; dd = dd->dd_parent) { 1079 if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) { 1080 if (flags & (DSL_PROP_GET_LOCAL | 1081 DSL_PROP_GET_RECEIVED)) 1082 break; 1083 flags |= DSL_PROP_GET_INHERITING; 1084 } 1085 dsl_dir_name(dd, setpoint); 1086 err = dsl_prop_get_all_impl(mos, 1087 dsl_dir_phys(dd)->dd_props_zapobj, setpoint, flags, *nvp); 1088 if (err) 1089 break; 1090 } 1091out: 1092 return (err); 1093} 1094 1095boolean_t 1096dsl_prop_get_hasrecvd(const char *dsname) 1097{ 1098 uint64_t dummy; 1099 1100 return (0 == 1101 dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL)); 1102} 1103 1104static int 1105dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source) 1106{ 1107 uint64_t version; 1108 spa_t *spa; 1109 int error = 0; 1110 1111 VERIFY0(spa_open(dsname, &spa, FTAG)); 1112 version = spa_version(spa); 1113 spa_close(spa, FTAG); 1114 1115 if (version >= SPA_VERSION_RECVD_PROPS) 1116 error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0); 1117 return (error); 1118} 1119 1120/* 1121 * Call after successfully receiving properties to ensure that only the first 1122 * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties. 1123 */ 1124int 1125dsl_prop_set_hasrecvd(const char *dsname) 1126{ 1127 int error = 0; 1128 if (!dsl_prop_get_hasrecvd(dsname)) 1129 error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL); 1130 return (error); 1131} 1132 1133void 1134dsl_prop_unset_hasrecvd(const char *dsname) 1135{ 1136 VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE)); 1137} 1138 1139int 1140dsl_prop_get_all(objset_t *os, nvlist_t **nvp) 1141{ 1142 return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0)); 1143} 1144 1145int 1146dsl_prop_get_received(const char *dsname, nvlist_t **nvp) 1147{ 1148 objset_t *os; 1149 int error; 1150 1151 /* 1152 * Received properties are not distinguishable from local properties 1153 * until the dataset has received properties on or after 1154 * SPA_VERSION_RECVD_PROPS. 1155 */ 1156 dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ? 1157 DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL); 1158 1159 error = dmu_objset_hold(dsname, FTAG, &os); 1160 if (error != 0) 1161 return (error); 1162 error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags); 1163 dmu_objset_rele(os, FTAG); 1164 return (error); 1165} 1166 1167void 1168dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value) 1169{ 1170 nvlist_t *propval; 1171 const char *propname = zfs_prop_to_name(prop); 1172 uint64_t default_value; 1173 1174 if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) { 1175 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0); 1176 return; 1177 } 1178 1179 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1180 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0); 1181 /* Indicate the default source if we can. */ 1182 if (dodefault(prop, 8, 1, &default_value) == 0 && 1183 value == default_value) { 1184 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0); 1185 } 1186 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 1187 nvlist_free(propval); 1188} 1189 1190void 1191dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value) 1192{ 1193 nvlist_t *propval; 1194 const char *propname = zfs_prop_to_name(prop); 1195 1196 if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) { 1197 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0); 1198 return; 1199 } 1200 1201 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1202 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0); 1203 VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 1204 nvlist_free(propval); 1205} 1206