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