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.
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.
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]
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.
27 #include <sys/zfs_context.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>
37 #include <sys/fs/zfs.h>
41 #define ZPROP_INHERIT_SUFFIX "$inherit"
42 #define ZPROP_RECVD_SUFFIX "$recvd"
45 dodefault(const char *propname
, int intsz
, int numints
, void *buf
)
50 * The setonce properties are read-only, BUT they still
51 * have a default value that can be used as the initial
54 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
||
55 (zfs_prop_readonly(prop
) && !zfs_prop_setonce(prop
)))
56 return (SET_ERROR(ENOENT
));
58 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
60 return (SET_ERROR(EOVERFLOW
));
61 (void) strncpy(buf
, zfs_prop_default_string(prop
),
64 if (intsz
!= 8 || numints
< 1)
65 return (SET_ERROR(EOVERFLOW
));
67 *(uint64_t *)buf
= zfs_prop_default_numeric(prop
);
74 dsl_prop_get_dd(dsl_dir_t
*dd
, const char *propname
,
75 int intsz
, int numints
, void *buf
, char *setpoint
, boolean_t snapshot
)
78 dsl_dir_t
*target
= dd
;
79 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
81 boolean_t inheritable
;
82 boolean_t inheriting
= B_FALSE
;
86 ASSERT(dsl_pool_config_held(dd
->dd_pool
));
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
);
97 * Note: dd may become NULL, therefore we shouldn't dereference it
100 for (; dd
!= NULL
; dd
= dd
->dd_parent
) {
101 if (dd
!= target
|| snapshot
) {
107 /* Check for a local value. */
108 err
= zap_lookup(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
,
109 propname
, intsz
, numints
, buf
);
111 if (setpoint
!= NULL
&& err
== 0)
112 dsl_dir_name(dd
, setpoint
);
117 * Skip the check for a received value if there is an explicit
120 err
= zap_contains(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
,
122 if (err
!= 0 && 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
);
130 if (setpoint
!= NULL
&& err
== 0) {
132 dsl_dir_name(dd
, setpoint
);
134 (void) strcpy(setpoint
,
135 ZPROP_SOURCE_VAL_RECVD
);
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.
148 err
= SET_ERROR(ENOENT
);
152 err
= dodefault(propname
, intsz
, numints
, buf
);
161 dsl_prop_get_ds(dsl_dataset_t
*ds
, const char *propname
,
162 int intsz
, int numints
, void *buf
, char *setpoint
)
164 zfs_prop_t prop
= zfs_name_to_prop(propname
);
165 boolean_t inheritable
;
168 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
169 inheritable
= (prop
== ZPROP_INVAL
|| zfs_prop_inheritable(prop
));
170 zapobj
= dsl_dataset_phys(ds
)->ds_props_obj
;
173 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
176 ASSERT(ds
->ds_is_snapshot
);
178 /* Check for a local value. */
179 err
= zap_lookup(mos
, zapobj
, propname
, intsz
, numints
, buf
);
181 if (setpoint
!= NULL
&& err
== 0)
182 dsl_dataset_name(ds
, setpoint
);
187 * Skip the check for a received value if there is an explicit
191 char *inheritstr
= kmem_asprintf("%s%s", propname
,
192 ZPROP_INHERIT_SUFFIX
);
193 err
= zap_contains(mos
, zapobj
, inheritstr
);
195 if (err
!= 0 && err
!= ENOENT
)
200 /* Check for a received value. */
201 char *recvdstr
= kmem_asprintf("%s%s", propname
,
203 err
= zap_lookup(mos
, zapobj
, recvdstr
,
204 intsz
, numints
, buf
);
207 if (setpoint
!= NULL
&& err
== 0)
208 (void) strcpy(setpoint
,
209 ZPROP_SOURCE_VAL_RECVD
);
215 return (dsl_prop_get_dd(ds
->ds_dir
, propname
,
216 intsz
, numints
, buf
, setpoint
, ds
->ds_is_snapshot
));
220 * Register interest in the named property. We'll call the callback
221 * once to notify it of the current property value, and again each time
222 * the property changes, until this callback is unregistered.
224 * Return 0 on success, errno if the prop is not an integer value.
227 dsl_prop_register(dsl_dataset_t
*ds
, const char *propname
,
228 dsl_prop_changed_cb_t
*callback
, void *cbarg
)
230 dsl_dir_t
*dd
= ds
->ds_dir
;
232 dsl_prop_cb_record_t
*cbr
;
234 ASSERTV(dsl_pool_t
*dp
= dd
->dd_pool
);
236 ASSERT(dsl_pool_config_held(dp
));
238 err
= dsl_prop_get_int_ds(ds
, propname
, &value
);
242 cbr
= kmem_alloc(sizeof (dsl_prop_cb_record_t
), KM_SLEEP
);
244 cbr
->cbr_propname
= kmem_alloc(strlen(propname
)+1, KM_SLEEP
);
245 (void) strcpy((char *)cbr
->cbr_propname
, propname
);
246 cbr
->cbr_func
= callback
;
247 cbr
->cbr_arg
= cbarg
;
248 mutex_enter(&dd
->dd_lock
);
249 list_insert_head(&dd
->dd_prop_cbs
, cbr
);
250 mutex_exit(&dd
->dd_lock
);
252 cbr
->cbr_func(cbr
->cbr_arg
, value
);
257 dsl_prop_get(const char *dsname
, const char *propname
,
258 int intsz
, int numints
, void *buf
, char *setpoint
)
263 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
267 error
= dsl_prop_get_ds(dmu_objset_ds(os
), propname
,
268 intsz
, numints
, buf
, setpoint
);
270 dmu_objset_rele(os
, FTAG
);
275 * Get the current property value. It may have changed by the time this
276 * function returns, so it is NOT safe to follow up with
277 * dsl_prop_register() and assume that the value has not changed in
280 * Return 0 on success, ENOENT if ddname is invalid.
283 dsl_prop_get_integer(const char *ddname
, const char *propname
,
284 uint64_t *valuep
, char *setpoint
)
286 return (dsl_prop_get(ddname
, propname
, 8, 1, valuep
, setpoint
));
290 dsl_prop_get_int_ds(dsl_dataset_t
*ds
, const char *propname
,
293 return (dsl_prop_get_ds(ds
, propname
, 8, 1, valuep
, NULL
));
297 * Predict the effective value of the given special property if it were set with
298 * the given value and source. This is not a general purpose function. It exists
299 * only to handle the special requirements of the quota and reservation
300 * properties. The fact that these properties are non-inheritable greatly
301 * simplifies the prediction logic.
303 * Returns 0 on success, a positive error code on failure, or -1 if called with
304 * a property not handled by this function.
307 dsl_prop_predict(dsl_dir_t
*dd
, const char *propname
,
308 zprop_source_t source
, uint64_t value
, uint64_t *newvalp
)
310 zfs_prop_t prop
= zfs_name_to_prop(propname
);
319 case ZFS_PROP_RESERVATION
:
320 case ZFS_PROP_REFQUOTA
:
321 case ZFS_PROP_REFRESERVATION
:
327 mos
= dd
->dd_pool
->dp_meta_objset
;
328 zapobj
= dsl_dir_phys(dd
)->dd_props_zapobj
;
329 recvdstr
= kmem_asprintf("%s%s", propname
, ZPROP_RECVD_SUFFIX
);
331 version
= spa_version(dd
->dd_pool
->dp_spa
);
332 if (version
< SPA_VERSION_RECVD_PROPS
) {
333 if (source
& ZPROP_SRC_NONE
)
334 source
= ZPROP_SRC_NONE
;
335 else if (source
& ZPROP_SRC_RECEIVED
)
336 source
= ZPROP_SRC_LOCAL
;
339 switch ((int)source
) {
341 /* Revert to the received value, if any. */
342 err
= zap_lookup(mos
, zapobj
, recvdstr
, 8, 1, newvalp
);
346 case ZPROP_SRC_LOCAL
:
349 case ZPROP_SRC_RECEIVED
:
351 * If there's no local setting, then the new received value will
352 * be the effective value.
354 err
= zap_lookup(mos
, zapobj
, propname
, 8, 1, newvalp
);
358 case (ZPROP_SRC_NONE
| ZPROP_SRC_RECEIVED
):
360 * We're clearing the received value, so the local setting (if
361 * it exists) remains the effective value.
363 err
= zap_lookup(mos
, zapobj
, propname
, 8, 1, newvalp
);
368 panic("unexpected property source: %d", source
);
380 * Unregister this callback. Return 0 on success, ENOENT if ddname is
381 * invalid, or ENOMSG if no matching callback registered.
384 dsl_prop_unregister(dsl_dataset_t
*ds
, const char *propname
,
385 dsl_prop_changed_cb_t
*callback
, void *cbarg
)
387 dsl_dir_t
*dd
= ds
->ds_dir
;
388 dsl_prop_cb_record_t
*cbr
;
390 mutex_enter(&dd
->dd_lock
);
391 for (cbr
= list_head(&dd
->dd_prop_cbs
);
392 cbr
; cbr
= list_next(&dd
->dd_prop_cbs
, cbr
)) {
393 if (cbr
->cbr_ds
== ds
&&
394 cbr
->cbr_func
== callback
&&
395 cbr
->cbr_arg
== cbarg
&&
396 strcmp(cbr
->cbr_propname
, propname
) == 0)
401 mutex_exit(&dd
->dd_lock
);
402 return (SET_ERROR(ENOMSG
));
405 list_remove(&dd
->dd_prop_cbs
, cbr
);
406 mutex_exit(&dd
->dd_lock
);
407 kmem_free((void*)cbr
->cbr_propname
, strlen(cbr
->cbr_propname
)+1);
408 kmem_free(cbr
, sizeof (dsl_prop_cb_record_t
));
414 dsl_prop_hascb(dsl_dataset_t
*ds
)
416 dsl_dir_t
*dd
= ds
->ds_dir
;
417 boolean_t rv
= B_FALSE
;
418 dsl_prop_cb_record_t
*cbr
;
420 mutex_enter(&dd
->dd_lock
);
421 for (cbr
= list_head(&dd
->dd_prop_cbs
); cbr
;
422 cbr
= list_next(&dd
->dd_prop_cbs
, cbr
)) {
423 if (cbr
->cbr_ds
== ds
) {
428 mutex_exit(&dd
->dd_lock
);
434 dsl_prop_notify_all_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
436 dsl_dir_t
*dd
= ds
->ds_dir
;
437 dsl_prop_cb_record_t
*cbr
;
439 mutex_enter(&dd
->dd_lock
);
440 for (cbr
= list_head(&dd
->dd_prop_cbs
); cbr
;
441 cbr
= list_next(&dd
->dd_prop_cbs
, cbr
)) {
445 * Callback entries do not have holds on their datasets
446 * so that datasets with registered callbacks are still
447 * eligible for eviction. Unlike operations on callbacks
448 * for a single dataset, we are performing a recursive
449 * descent of related datasets and the calling context
450 * for this iteration only has a dataset hold on the root.
451 * Without a hold, the callback's pointer to the dataset
452 * could be invalidated by eviction at any time.
454 * Use dsl_dataset_try_add_ref() to verify that the
455 * dataset has not begun eviction processing and to
456 * prevent eviction from occurring for the duration
457 * of the callback. If the hold attempt fails, this
458 * object is already being evicted and the callback can
461 if (!dsl_dataset_try_add_ref(dp
, cbr
->cbr_ds
, FTAG
))
464 if (dsl_prop_get_ds(cbr
->cbr_ds
, cbr
->cbr_propname
,
465 sizeof (value
), 1, &value
, NULL
) == 0)
466 cbr
->cbr_func(cbr
->cbr_arg
, value
);
468 dsl_dataset_rele(cbr
->cbr_ds
, FTAG
);
470 mutex_exit(&dd
->dd_lock
);
476 * Update all property values for ddobj & its descendants. This is used
477 * when renaming the dir.
480 dsl_prop_notify_all(dsl_dir_t
*dd
)
482 dsl_pool_t
*dp
= dd
->dd_pool
;
483 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
484 (void) dmu_objset_find_dp(dp
, dd
->dd_object
, dsl_prop_notify_all_cb
,
485 NULL
, DS_FIND_CHILDREN
);
489 dsl_prop_changed_notify(dsl_pool_t
*dp
, uint64_t ddobj
,
490 const char *propname
, uint64_t value
, int first
)
493 dsl_prop_cb_record_t
*cbr
;
494 objset_t
*mos
= dp
->dp_meta_objset
;
499 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
500 err
= dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
);
506 * If the prop is set here, then this change is not
507 * being inherited here or below; stop the recursion.
509 err
= zap_contains(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
,
512 dsl_dir_rele(dd
, FTAG
);
515 ASSERT3U(err
, ==, ENOENT
);
518 mutex_enter(&dd
->dd_lock
);
519 for (cbr
= list_head(&dd
->dd_prop_cbs
); cbr
;
520 cbr
= list_next(&dd
->dd_prop_cbs
, cbr
)) {
524 * cbr->cbf_ds may be invalidated due to eviction,
525 * requiring the use of dsl_dataset_try_add_ref().
526 * See comment block in dsl_prop_notify_all_cb()
529 if (strcmp(cbr
->cbr_propname
, propname
) != 0 ||
530 !dsl_dataset_try_add_ref(dp
, cbr
->cbr_ds
, FTAG
))
533 propobj
= dsl_dataset_phys(cbr
->cbr_ds
)->ds_props_obj
;
536 * If the property is not set on this ds, then it is
537 * inherited here; call the callback.
539 if (propobj
== 0 || zap_contains(mos
, propobj
, propname
) != 0)
540 cbr
->cbr_func(cbr
->cbr_arg
, value
);
542 dsl_dataset_rele(cbr
->cbr_ds
, FTAG
);
544 mutex_exit(&dd
->dd_lock
);
546 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
547 for (zap_cursor_init(&zc
, mos
,
548 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
549 zap_cursor_retrieve(&zc
, za
) == 0;
550 zap_cursor_advance(&zc
)) {
551 dsl_prop_changed_notify(dp
, za
->za_first_integer
,
552 propname
, value
, FALSE
);
554 kmem_free(za
, sizeof (zap_attribute_t
));
555 zap_cursor_fini(&zc
);
556 dsl_dir_rele(dd
, FTAG
);
560 dsl_prop_set_sync_impl(dsl_dataset_t
*ds
, const char *propname
,
561 zprop_source_t source
, int intsz
, int numints
, const void *value
,
564 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
565 uint64_t zapobj
, intval
, dummy
;
568 const char *valstr
= NULL
;
573 uint64_t version
= spa_version(ds
->ds_dir
->dd_pool
->dp_spa
);
575 isint
= (dodefault(propname
, 8, 1, &intval
) == 0);
577 if (ds
->ds_is_snapshot
) {
578 ASSERT(version
>= SPA_VERSION_SNAP_PROPS
);
579 if (dsl_dataset_phys(ds
)->ds_props_obj
== 0) {
580 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
581 dsl_dataset_phys(ds
)->ds_props_obj
=
583 DMU_OT_DSL_PROPS
, DMU_OT_NONE
, 0, tx
);
585 zapobj
= dsl_dataset_phys(ds
)->ds_props_obj
;
587 zapobj
= dsl_dir_phys(ds
->ds_dir
)->dd_props_zapobj
;
590 if (version
< SPA_VERSION_RECVD_PROPS
) {
591 if (source
& ZPROP_SRC_NONE
)
592 source
= ZPROP_SRC_NONE
;
593 else if (source
& ZPROP_SRC_RECEIVED
)
594 source
= ZPROP_SRC_LOCAL
;
597 inheritstr
= kmem_asprintf("%s%s", propname
, ZPROP_INHERIT_SUFFIX
);
598 recvdstr
= kmem_asprintf("%s%s", propname
, ZPROP_RECVD_SUFFIX
);
600 switch ((int)source
) {
603 * revert to received value, if any (inherit -S)
605 * - remove propname$inherit
607 err
= zap_remove(mos
, zapobj
, propname
, tx
);
608 ASSERT(err
== 0 || err
== ENOENT
);
609 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
610 ASSERT(err
== 0 || err
== ENOENT
);
612 case ZPROP_SRC_LOCAL
:
614 * remove propname$inherit
615 * set propname -> value
617 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
618 ASSERT(err
== 0 || err
== ENOENT
);
619 VERIFY0(zap_update(mos
, zapobj
, propname
,
620 intsz
, numints
, value
, tx
));
622 case ZPROP_SRC_INHERITED
:
626 * - set propname$inherit
628 err
= zap_remove(mos
, zapobj
, propname
, tx
);
629 ASSERT(err
== 0 || err
== ENOENT
);
630 if (version
>= SPA_VERSION_RECVD_PROPS
&&
631 dsl_prop_get_int_ds(ds
, ZPROP_HAS_RECVD
, &dummy
) == 0) {
633 VERIFY0(zap_update(mos
, zapobj
, inheritstr
,
637 case ZPROP_SRC_RECEIVED
:
639 * set propname$recvd -> value
641 err
= zap_update(mos
, zapobj
, recvdstr
,
642 intsz
, numints
, value
, tx
);
645 case (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
):
647 * clear local and received settings
649 * - remove propname$inherit
650 * - remove propname$recvd
652 err
= zap_remove(mos
, zapobj
, propname
, tx
);
653 ASSERT(err
== 0 || err
== ENOENT
);
654 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
655 ASSERT(err
== 0 || err
== ENOENT
);
657 case (ZPROP_SRC_NONE
| ZPROP_SRC_RECEIVED
):
659 * remove propname$recvd
661 err
= zap_remove(mos
, zapobj
, recvdstr
, tx
);
662 ASSERT(err
== 0 || err
== ENOENT
);
665 cmn_err(CE_PANIC
, "unexpected property source: %d", source
);
672 VERIFY0(dsl_prop_get_int_ds(ds
, propname
, &intval
));
674 if (ds
->ds_is_snapshot
) {
675 dsl_prop_cb_record_t
*cbr
;
677 * It's a snapshot; nothing can inherit this
678 * property, so just look for callbacks on this
681 mutex_enter(&ds
->ds_dir
->dd_lock
);
682 for (cbr
= list_head(&ds
->ds_dir
->dd_prop_cbs
); cbr
;
683 cbr
= list_next(&ds
->ds_dir
->dd_prop_cbs
, cbr
)) {
684 if (cbr
->cbr_ds
== ds
&&
685 strcmp(cbr
->cbr_propname
, propname
) == 0)
686 cbr
->cbr_func(cbr
->cbr_arg
, intval
);
688 mutex_exit(&ds
->ds_dir
->dd_lock
);
690 dsl_prop_changed_notify(ds
->ds_dir
->dd_pool
,
691 ds
->ds_dir
->dd_object
, propname
, intval
, TRUE
);
694 (void) snprintf(valbuf
, sizeof (valbuf
),
695 "%lld", (longlong_t
)intval
);
698 if (source
== ZPROP_SRC_LOCAL
) {
701 tbuf
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
702 if (dsl_prop_get_ds(ds
, propname
, 1,
703 ZAP_MAXVALUELEN
, tbuf
, NULL
) == 0)
708 spa_history_log_internal_ds(ds
, (source
== ZPROP_SRC_NONE
||
709 source
== ZPROP_SRC_INHERITED
) ? "inherit" : "set", tx
,
710 "%s=%s", propname
, (valstr
== NULL
? "" : valstr
));
713 kmem_free(tbuf
, ZAP_MAXVALUELEN
);
717 dsl_prop_set_int(const char *dsname
, const char *propname
,
718 zprop_source_t source
, uint64_t value
)
720 nvlist_t
*nvl
= fnvlist_alloc();
723 fnvlist_add_uint64(nvl
, propname
, value
);
724 error
= dsl_props_set(dsname
, source
, nvl
);
730 dsl_prop_set_string(const char *dsname
, const char *propname
,
731 zprop_source_t source
, const char *value
)
733 nvlist_t
*nvl
= fnvlist_alloc();
736 fnvlist_add_string(nvl
, propname
, value
);
737 error
= dsl_props_set(dsname
, source
, nvl
);
743 dsl_prop_inherit(const char *dsname
, const char *propname
,
744 zprop_source_t source
)
746 nvlist_t
*nvl
= fnvlist_alloc();
749 fnvlist_add_boolean(nvl
, propname
);
750 error
= dsl_props_set(dsname
, source
, nvl
);
755 typedef struct dsl_props_set_arg
{
756 const char *dpsa_dsname
;
757 zprop_source_t dpsa_source
;
758 nvlist_t
*dpsa_props
;
759 } dsl_props_set_arg_t
;
762 dsl_props_set_check(void *arg
, dmu_tx_t
*tx
)
764 dsl_props_set_arg_t
*dpsa
= arg
;
765 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
768 nvpair_t
*elem
= NULL
;
771 err
= dsl_dataset_hold(dp
, dpsa
->dpsa_dsname
, FTAG
, &ds
);
775 version
= spa_version(ds
->ds_dir
->dd_pool
->dp_spa
);
776 while ((elem
= nvlist_next_nvpair(dpsa
->dpsa_props
, elem
)) != NULL
) {
777 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
778 dsl_dataset_rele(ds
, FTAG
);
779 return (SET_ERROR(ENAMETOOLONG
));
781 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
782 char *valstr
= fnvpair_value_string(elem
);
783 if (strlen(valstr
) >= (version
<
784 SPA_VERSION_STMF_PROP
?
785 ZAP_OLDMAXVALUELEN
: ZAP_MAXVALUELEN
)) {
786 dsl_dataset_rele(ds
, FTAG
);
792 if (ds
->ds_is_snapshot
&& version
< SPA_VERSION_SNAP_PROPS
) {
793 dsl_dataset_rele(ds
, FTAG
);
794 return (SET_ERROR(ENOTSUP
));
796 dsl_dataset_rele(ds
, FTAG
);
801 dsl_props_set_sync_impl(dsl_dataset_t
*ds
, zprop_source_t source
,
802 nvlist_t
*props
, dmu_tx_t
*tx
)
804 nvpair_t
*elem
= NULL
;
806 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
807 nvpair_t
*pair
= elem
;
809 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
811 * dsl_prop_get_all_impl() returns properties in this
814 nvlist_t
*attrs
= fnvpair_value_nvlist(pair
);
815 pair
= fnvlist_lookup_nvpair(attrs
, ZPROP_VALUE
);
818 if (nvpair_type(pair
) == DATA_TYPE_STRING
) {
819 const char *value
= fnvpair_value_string(pair
);
820 dsl_prop_set_sync_impl(ds
, nvpair_name(pair
),
821 source
, 1, strlen(value
) + 1, value
, tx
);
822 } else if (nvpair_type(pair
) == DATA_TYPE_UINT64
) {
823 uint64_t intval
= fnvpair_value_uint64(pair
);
824 dsl_prop_set_sync_impl(ds
, nvpair_name(pair
),
825 source
, sizeof (intval
), 1, &intval
, tx
);
826 } else if (nvpair_type(pair
) == DATA_TYPE_BOOLEAN
) {
827 dsl_prop_set_sync_impl(ds
, nvpair_name(pair
),
828 source
, 0, 0, NULL
, tx
);
830 panic("invalid nvpair type");
836 dsl_props_set_sync(void *arg
, dmu_tx_t
*tx
)
838 dsl_props_set_arg_t
*dpsa
= arg
;
839 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
842 VERIFY0(dsl_dataset_hold(dp
, dpsa
->dpsa_dsname
, FTAG
, &ds
));
843 dsl_props_set_sync_impl(ds
, dpsa
->dpsa_source
, dpsa
->dpsa_props
, tx
);
844 dsl_dataset_rele(ds
, FTAG
);
848 * All-or-nothing; if any prop can't be set, nothing will be modified.
851 dsl_props_set(const char *dsname
, zprop_source_t source
, nvlist_t
*props
)
853 dsl_props_set_arg_t dpsa
;
856 dpsa
.dpsa_dsname
= dsname
;
857 dpsa
.dpsa_source
= source
;
858 dpsa
.dpsa_props
= props
;
861 * If the source includes NONE, then we will only be removing entries
862 * from the ZAP object. In that case don't check for ENOSPC.
864 if ((source
& ZPROP_SRC_NONE
) == 0)
865 nblks
= 2 * fnvlist_num_pairs(props
);
867 return (dsl_sync_task(dsname
, dsl_props_set_check
, dsl_props_set_sync
,
868 &dpsa
, nblks
, ZFS_SPACE_CHECK_RESERVED
));
871 typedef enum dsl_prop_getflags
{
872 DSL_PROP_GET_INHERITING
= 0x1, /* searching parent of target ds */
873 DSL_PROP_GET_SNAPSHOT
= 0x2, /* snapshot dataset */
874 DSL_PROP_GET_LOCAL
= 0x4, /* local properties */
875 DSL_PROP_GET_RECEIVED
= 0x8 /* received properties */
876 } dsl_prop_getflags_t
;
879 dsl_prop_get_all_impl(objset_t
*mos
, uint64_t propobj
,
880 const char *setpoint
, dsl_prop_getflags_t flags
, nvlist_t
*nv
)
886 for (zap_cursor_init(&zc
, mos
, propobj
);
887 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
888 zap_cursor_advance(&zc
)) {
891 char buf
[ZAP_MAXNAMELEN
];
894 const char *propname
;
897 suffix
= strchr(za
.za_name
, '$');
899 if (suffix
== NULL
) {
901 * Skip local properties if we only want received
904 if (flags
& DSL_PROP_GET_RECEIVED
)
907 propname
= za
.za_name
;
909 } else if (strcmp(suffix
, ZPROP_INHERIT_SUFFIX
) == 0) {
910 /* Skip explicitly inherited entries. */
912 } else if (strcmp(suffix
, ZPROP_RECVD_SUFFIX
) == 0) {
913 if (flags
& DSL_PROP_GET_LOCAL
)
916 (void) strncpy(buf
, za
.za_name
, (suffix
- za
.za_name
));
917 buf
[suffix
- za
.za_name
] = '\0';
920 if (!(flags
& DSL_PROP_GET_RECEIVED
)) {
921 /* Skip if locally overridden. */
922 err
= zap_contains(mos
, propobj
, propname
);
928 /* Skip if explicitly inherited. */
929 valstr
= kmem_asprintf("%s%s", propname
,
930 ZPROP_INHERIT_SUFFIX
);
931 err
= zap_contains(mos
, propobj
, valstr
);
939 source
= ((flags
& DSL_PROP_GET_INHERITING
) ?
940 setpoint
: ZPROP_SOURCE_VAL_RECVD
);
943 * For backward compatibility, skip suffixes we don't
949 prop
= zfs_name_to_prop(propname
);
951 /* Skip non-inheritable properties. */
952 if ((flags
& DSL_PROP_GET_INHERITING
) && prop
!= ZPROP_INVAL
&&
953 !zfs_prop_inheritable(prop
))
956 /* Skip properties not valid for this type. */
957 if ((flags
& DSL_PROP_GET_SNAPSHOT
) && prop
!= ZPROP_INVAL
&&
958 !zfs_prop_valid_for_type(prop
, ZFS_TYPE_SNAPSHOT
, B_FALSE
))
961 /* Skip properties already defined. */
962 if (nvlist_exists(nv
, propname
))
965 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
966 if (za
.za_integer_length
== 1) {
970 char *tmp
= kmem_alloc(za
.za_num_integers
,
972 err
= zap_lookup(mos
, propobj
,
973 za
.za_name
, 1, za
.za_num_integers
, tmp
);
975 kmem_free(tmp
, za
.za_num_integers
);
978 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
,
980 kmem_free(tmp
, za
.za_num_integers
);
985 ASSERT(za
.za_integer_length
== 8);
986 (void) nvlist_add_uint64(propval
, ZPROP_VALUE
,
987 za
.za_first_integer
);
990 VERIFY(nvlist_add_string(propval
, ZPROP_SOURCE
, source
) == 0);
991 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
992 nvlist_free(propval
);
994 zap_cursor_fini(&zc
);
1001 * Iterate over all properties for this dataset and return them in an nvlist.
1004 dsl_prop_get_all_ds(dsl_dataset_t
*ds
, nvlist_t
**nvp
,
1005 dsl_prop_getflags_t flags
)
1007 dsl_dir_t
*dd
= ds
->ds_dir
;
1008 dsl_pool_t
*dp
= dd
->dd_pool
;
1009 objset_t
*mos
= dp
->dp_meta_objset
;
1011 char setpoint
[MAXNAMELEN
];
1013 VERIFY(nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1015 if (ds
->ds_is_snapshot
)
1016 flags
|= DSL_PROP_GET_SNAPSHOT
;
1018 ASSERT(dsl_pool_config_held(dp
));
1020 if (dsl_dataset_phys(ds
)->ds_props_obj
!= 0) {
1021 ASSERT(flags
& DSL_PROP_GET_SNAPSHOT
);
1022 dsl_dataset_name(ds
, setpoint
);
1023 err
= dsl_prop_get_all_impl(mos
,
1024 dsl_dataset_phys(ds
)->ds_props_obj
, setpoint
, flags
, *nvp
);
1029 for (; dd
!= NULL
; dd
= dd
->dd_parent
) {
1030 if (dd
!= ds
->ds_dir
|| (flags
& DSL_PROP_GET_SNAPSHOT
)) {
1031 if (flags
& (DSL_PROP_GET_LOCAL
|
1032 DSL_PROP_GET_RECEIVED
))
1034 flags
|= DSL_PROP_GET_INHERITING
;
1036 dsl_dir_name(dd
, setpoint
);
1037 err
= dsl_prop_get_all_impl(mos
,
1038 dsl_dir_phys(dd
)->dd_props_zapobj
, setpoint
, flags
, *nvp
);
1047 dsl_prop_get_hasrecvd(const char *dsname
)
1052 dsl_prop_get_integer(dsname
, ZPROP_HAS_RECVD
, &dummy
, NULL
));
1056 dsl_prop_set_hasrecvd_impl(const char *dsname
, zprop_source_t source
)
1062 VERIFY0(spa_open(dsname
, &spa
, FTAG
));
1063 version
= spa_version(spa
);
1064 spa_close(spa
, FTAG
);
1066 if (version
>= SPA_VERSION_RECVD_PROPS
)
1067 error
= dsl_prop_set_int(dsname
, ZPROP_HAS_RECVD
, source
, 0);
1072 * Call after successfully receiving properties to ensure that only the first
1073 * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
1076 dsl_prop_set_hasrecvd(const char *dsname
)
1079 if (!dsl_prop_get_hasrecvd(dsname
))
1080 error
= dsl_prop_set_hasrecvd_impl(dsname
, ZPROP_SRC_LOCAL
);
1085 dsl_prop_unset_hasrecvd(const char *dsname
)
1087 VERIFY0(dsl_prop_set_hasrecvd_impl(dsname
, ZPROP_SRC_NONE
));
1091 dsl_prop_get_all(objset_t
*os
, nvlist_t
**nvp
)
1093 return (dsl_prop_get_all_ds(os
->os_dsl_dataset
, nvp
, 0));
1097 dsl_prop_get_received(const char *dsname
, nvlist_t
**nvp
)
1103 * Received properties are not distinguishable from local properties
1104 * until the dataset has received properties on or after
1105 * SPA_VERSION_RECVD_PROPS.
1107 dsl_prop_getflags_t flags
= (dsl_prop_get_hasrecvd(dsname
) ?
1108 DSL_PROP_GET_RECEIVED
: DSL_PROP_GET_LOCAL
);
1110 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1113 error
= dsl_prop_get_all_ds(os
->os_dsl_dataset
, nvp
, flags
);
1114 dmu_objset_rele(os
, FTAG
);
1119 dsl_prop_nvlist_add_uint64(nvlist_t
*nv
, zfs_prop_t prop
, uint64_t value
)
1122 const char *propname
= zfs_prop_to_name(prop
);
1123 uint64_t default_value
;
1125 if (nvlist_lookup_nvlist(nv
, propname
, &propval
) == 0) {
1126 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, value
) == 0);
1130 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1131 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, value
) == 0);
1132 /* Indicate the default source if we can. */
1133 if (dodefault(propname
, 8, 1, &default_value
) == 0 &&
1134 value
== default_value
) {
1135 VERIFY(nvlist_add_string(propval
, ZPROP_SOURCE
, "") == 0);
1137 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
1138 nvlist_free(propval
);
1142 dsl_prop_nvlist_add_string(nvlist_t
*nv
, zfs_prop_t prop
, const char *value
)
1145 const char *propname
= zfs_prop_to_name(prop
);
1147 if (nvlist_lookup_nvlist(nv
, propname
, &propval
) == 0) {
1148 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, value
) == 0);
1152 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1153 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, value
) == 0);
1154 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
1155 nvlist_free(propval
);
1158 #if defined(_KERNEL) && defined(HAVE_SPL)
1159 EXPORT_SYMBOL(dsl_prop_register
);
1160 EXPORT_SYMBOL(dsl_prop_unregister
);
1161 EXPORT_SYMBOL(dsl_prop_get
);
1162 EXPORT_SYMBOL(dsl_prop_get_integer
);
1163 EXPORT_SYMBOL(dsl_prop_get_all
);
1164 EXPORT_SYMBOL(dsl_prop_get_received
);
1165 EXPORT_SYMBOL(dsl_prop_get_ds
);
1166 EXPORT_SYMBOL(dsl_prop_get_int_ds
);
1167 EXPORT_SYMBOL(dsl_prop_get_dd
);
1168 EXPORT_SYMBOL(dsl_props_set
);
1169 EXPORT_SYMBOL(dsl_prop_set_int
);
1170 EXPORT_SYMBOL(dsl_prop_set_string
);
1171 EXPORT_SYMBOL(dsl_prop_inherit
);
1172 EXPORT_SYMBOL(dsl_prop_predict
);
1173 EXPORT_SYMBOL(dsl_prop_nvlist_add_uint64
);
1174 EXPORT_SYMBOL(dsl_prop_nvlist_add_string
);