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