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