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 https://opensource.org/licenses/CDDL-1.0.
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, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Martin Matuska. All rights reserved.
25 * Copyright 2019 Joyent, Inc.
26 * Copyright (c) 2022 Hewlett Packard Enterprise Development LP.
29 #include <sys/zfs_context.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_prop.h>
36 #include <sys/dsl_synctask.h>
39 #include <sys/fs/zfs.h>
43 #define ZPROP_INHERIT_SUFFIX "$inherit"
44 #define ZPROP_RECVD_SUFFIX "$recvd"
45 #define ZPROP_IUV_SUFFIX "$iuv"
48 dodefault(zfs_prop_t prop
, int intsz
, int numints
, void *buf
)
51 * The setonce properties are read-only, BUT they still
52 * have a default value that can be used as the initial
55 if (prop
== ZPROP_INVAL
||
56 (zfs_prop_readonly(prop
) && !zfs_prop_setonce(prop
)))
57 return (SET_ERROR(ENOENT
));
59 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
61 return (SET_ERROR(EOVERFLOW
));
62 (void) strlcpy(buf
, zfs_prop_default_string(prop
),
65 if (intsz
!= 8 || numints
< 1)
66 return (SET_ERROR(EOVERFLOW
));
68 *(uint64_t *)buf
= zfs_prop_default_numeric(prop
);
75 dsl_prop_known_index(zfs_prop_t prop
, uint64_t value
)
77 const char *str
= NULL
;
78 if (prop
!= ZPROP_CONT
&& prop
!= ZPROP_INVAL
&&
79 zfs_prop_get_type(prop
) == PROP_TYPE_INDEX
)
80 return (!zfs_prop_index_to_string(prop
, value
, &str
));
86 dsl_prop_get_dd(dsl_dir_t
*dd
, const char *propname
,
87 int intsz
, int numints
, void *buf
, char *setpoint
, boolean_t snapshot
)
90 dsl_dir_t
*target
= dd
;
91 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
93 boolean_t inheritable
;
94 boolean_t inheriting
= B_FALSE
;
99 ASSERT(dsl_pool_config_held(dd
->dd_pool
));
104 prop
= zfs_name_to_prop(propname
);
105 inheritable
= (prop
== ZPROP_USERPROP
|| zfs_prop_inheritable(prop
));
106 inheritstr
= kmem_asprintf("%s%s", propname
, ZPROP_INHERIT_SUFFIX
);
107 recvdstr
= kmem_asprintf("%s%s", propname
, ZPROP_RECVD_SUFFIX
);
108 iuvstr
= kmem_asprintf("%s%s", propname
, ZPROP_IUV_SUFFIX
);
111 * Note: dd may become NULL, therefore we shouldn't dereference it
114 for (; dd
!= NULL
; dd
= dd
->dd_parent
) {
115 if (dd
!= target
|| snapshot
) {
117 err
= SET_ERROR(ENOENT
);
123 /* Check for a iuv value. */
124 err
= zap_lookup(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
,
125 iuvstr
, intsz
, numints
, buf
);
126 if (err
== 0 && dsl_prop_known_index(prop
,
127 *(uint64_t *)buf
) != 1)
130 if (setpoint
!= NULL
&& err
== 0)
131 dsl_dir_name(dd
, setpoint
);
135 /* Check for a local value. */
136 err
= zap_lookup(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
,
137 propname
, intsz
, numints
, buf
);
139 if (setpoint
!= NULL
&& err
== 0)
140 dsl_dir_name(dd
, setpoint
);
145 * Skip the check for a received value if there is an explicit
148 err
= zap_contains(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
,
150 if (err
!= 0 && err
!= ENOENT
)
154 /* Check for a received value. */
155 err
= zap_lookup(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
,
156 recvdstr
, intsz
, numints
, buf
);
158 if (setpoint
!= NULL
&& err
== 0) {
160 dsl_dir_name(dd
, setpoint
);
162 (void) strlcpy(setpoint
,
163 ZPROP_SOURCE_VAL_RECVD
,
172 * If we found an explicit inheritance entry, err is zero even
173 * though we haven't yet found the value, so reinitializing err
174 * at the end of the loop (instead of at the beginning) ensures
175 * that err has a valid post-loop value.
177 err
= SET_ERROR(ENOENT
);
181 err
= dodefault(prop
, intsz
, numints
, buf
);
183 kmem_strfree(inheritstr
);
184 kmem_strfree(recvdstr
);
185 kmem_strfree(iuvstr
);
191 dsl_prop_get_ds(dsl_dataset_t
*ds
, const char *propname
,
192 int intsz
, int numints
, void *buf
, char *setpoint
)
194 zfs_prop_t prop
= zfs_name_to_prop(propname
);
195 boolean_t inheritable
;
198 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
199 inheritable
= (prop
== ZPROP_USERPROP
|| zfs_prop_inheritable(prop
));
200 zapobj
= dsl_dataset_phys(ds
)->ds_props_obj
;
203 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
206 ASSERT(ds
->ds_is_snapshot
);
208 /* Check for a local value. */
209 err
= zap_lookup(mos
, zapobj
, propname
, intsz
, numints
, buf
);
211 if (setpoint
!= NULL
&& err
== 0)
212 dsl_dataset_name(ds
, setpoint
);
217 * Skip the check for a received value if there is an explicit
221 char *inheritstr
= kmem_asprintf("%s%s", propname
,
222 ZPROP_INHERIT_SUFFIX
);
223 err
= zap_contains(mos
, zapobj
, inheritstr
);
224 kmem_strfree(inheritstr
);
225 if (err
!= 0 && err
!= ENOENT
)
230 /* Check for a received value. */
231 char *recvdstr
= kmem_asprintf("%s%s", propname
,
233 err
= zap_lookup(mos
, zapobj
, recvdstr
,
234 intsz
, numints
, buf
);
235 kmem_strfree(recvdstr
);
237 if (setpoint
!= NULL
&& err
== 0)
238 (void) strlcpy(setpoint
,
239 ZPROP_SOURCE_VAL_RECVD
,
246 return (dsl_prop_get_dd(ds
->ds_dir
, propname
,
247 intsz
, numints
, buf
, setpoint
, ds
->ds_is_snapshot
));
250 static dsl_prop_record_t
*
251 dsl_prop_record_find(dsl_dir_t
*dd
, const char *propname
)
253 dsl_prop_record_t
*pr
= NULL
;
255 ASSERT(MUTEX_HELD(&dd
->dd_lock
));
257 for (pr
= list_head(&dd
->dd_props
);
258 pr
!= NULL
; pr
= list_next(&dd
->dd_props
, pr
)) {
259 if (strcmp(pr
->pr_propname
, propname
) == 0)
266 static dsl_prop_record_t
*
267 dsl_prop_record_create(dsl_dir_t
*dd
, const char *propname
)
269 dsl_prop_record_t
*pr
;
271 ASSERT(MUTEX_HELD(&dd
->dd_lock
));
273 pr
= kmem_alloc(sizeof (dsl_prop_record_t
), KM_SLEEP
);
274 pr
->pr_propname
= spa_strdup(propname
);
275 list_create(&pr
->pr_cbs
, sizeof (dsl_prop_cb_record_t
),
276 offsetof(dsl_prop_cb_record_t
, cbr_pr_node
));
277 list_insert_head(&dd
->dd_props
, pr
);
283 dsl_prop_init(dsl_dir_t
*dd
)
285 list_create(&dd
->dd_props
, sizeof (dsl_prop_record_t
),
286 offsetof(dsl_prop_record_t
, pr_node
));
290 dsl_prop_fini(dsl_dir_t
*dd
)
292 dsl_prop_record_t
*pr
;
294 while ((pr
= list_remove_head(&dd
->dd_props
)) != NULL
) {
295 list_destroy(&pr
->pr_cbs
);
296 spa_strfree((char *)pr
->pr_propname
);
297 kmem_free(pr
, sizeof (dsl_prop_record_t
));
299 list_destroy(&dd
->dd_props
);
303 * Register interest in the named property. We'll call the callback
304 * once to notify it of the current property value, and again each time
305 * the property changes, until this callback is unregistered.
307 * Return 0 on success, errno if the prop is not an integer value.
310 dsl_prop_register(dsl_dataset_t
*ds
, const char *propname
,
311 dsl_prop_changed_cb_t
*callback
, void *cbarg
)
313 dsl_dir_t
*dd
= ds
->ds_dir
;
315 dsl_prop_record_t
*pr
;
316 dsl_prop_cb_record_t
*cbr
;
318 dsl_pool_t
*dp __maybe_unused
= dd
->dd_pool
;
320 ASSERT(dsl_pool_config_held(dp
));
322 err
= dsl_prop_get_int_ds(ds
, propname
, &value
);
326 cbr
= kmem_alloc(sizeof (dsl_prop_cb_record_t
), KM_SLEEP
);
328 cbr
->cbr_func
= callback
;
329 cbr
->cbr_arg
= cbarg
;
331 mutex_enter(&dd
->dd_lock
);
332 pr
= dsl_prop_record_find(dd
, propname
);
334 pr
= dsl_prop_record_create(dd
, propname
);
336 list_insert_head(&pr
->pr_cbs
, cbr
);
337 list_insert_head(&ds
->ds_prop_cbs
, cbr
);
338 mutex_exit(&dd
->dd_lock
);
340 cbr
->cbr_func(cbr
->cbr_arg
, value
);
345 dsl_prop_get(const char *dsname
, const char *propname
,
346 int intsz
, int numints
, void *buf
, char *setpoint
)
351 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
355 error
= dsl_prop_get_ds(dmu_objset_ds(os
), propname
,
356 intsz
, numints
, buf
, setpoint
);
358 dmu_objset_rele(os
, FTAG
);
363 * Get the current property value. It may have changed by the time this
364 * function returns, so it is NOT safe to follow up with
365 * dsl_prop_register() and assume that the value has not changed in
368 * Return 0 on success, ENOENT if ddname is invalid.
371 dsl_prop_get_integer(const char *ddname
, const char *propname
,
372 uint64_t *valuep
, char *setpoint
)
374 return (dsl_prop_get(ddname
, propname
, 8, 1, valuep
, setpoint
));
378 dsl_prop_get_int_ds(dsl_dataset_t
*ds
, const char *propname
,
381 return (dsl_prop_get_ds(ds
, propname
, 8, 1, valuep
, NULL
));
385 * Predict the effective value of the given special property if it were set with
386 * the given value and source. This is not a general purpose function. It exists
387 * only to handle the special requirements of the quota and reservation
388 * properties. The fact that these properties are non-inheritable greatly
389 * simplifies the prediction logic.
391 * Returns 0 on success, a positive error code on failure, or -1 if called with
392 * a property not handled by this function.
395 dsl_prop_predict(dsl_dir_t
*dd
, const char *propname
,
396 zprop_source_t source
, uint64_t value
, uint64_t *newvalp
)
398 zfs_prop_t prop
= zfs_name_to_prop(propname
);
407 case ZFS_PROP_RESERVATION
:
408 case ZFS_PROP_REFQUOTA
:
409 case ZFS_PROP_REFRESERVATION
:
415 mos
= dd
->dd_pool
->dp_meta_objset
;
416 zapobj
= dsl_dir_phys(dd
)->dd_props_zapobj
;
417 recvdstr
= kmem_asprintf("%s%s", propname
, ZPROP_RECVD_SUFFIX
);
419 version
= spa_version(dd
->dd_pool
->dp_spa
);
420 if (version
< SPA_VERSION_RECVD_PROPS
) {
421 if (source
& ZPROP_SRC_NONE
)
422 source
= ZPROP_SRC_NONE
;
423 else if (source
& ZPROP_SRC_RECEIVED
)
424 source
= ZPROP_SRC_LOCAL
;
427 switch ((int)source
) {
429 /* Revert to the received value, if any. */
430 err
= zap_lookup(mos
, zapobj
, recvdstr
, 8, 1, newvalp
);
434 case ZPROP_SRC_LOCAL
:
437 case ZPROP_SRC_RECEIVED
:
439 * If there's no local setting, then the new received value will
440 * be the effective value.
442 err
= zap_lookup(mos
, zapobj
, propname
, 8, 1, newvalp
);
446 case (ZPROP_SRC_NONE
| ZPROP_SRC_RECEIVED
):
448 * We're clearing the received value, so the local setting (if
449 * it exists) remains the effective value.
451 err
= zap_lookup(mos
, zapobj
, propname
, 8, 1, newvalp
);
456 panic("unexpected property source: %d", source
);
459 kmem_strfree(recvdstr
);
468 * Unregister this callback. Return 0 on success, ENOENT if ddname is
469 * invalid, or ENOMSG if no matching callback registered.
471 * NOTE: This function is no longer used internally but has been preserved
472 * to prevent breaking external consumers (Lustre, etc).
475 dsl_prop_unregister(dsl_dataset_t
*ds
, const char *propname
,
476 dsl_prop_changed_cb_t
*callback
, void *cbarg
)
478 dsl_dir_t
*dd
= ds
->ds_dir
;
479 dsl_prop_cb_record_t
*cbr
;
481 mutex_enter(&dd
->dd_lock
);
482 for (cbr
= list_head(&ds
->ds_prop_cbs
);
483 cbr
; cbr
= list_next(&ds
->ds_prop_cbs
, cbr
)) {
484 if (cbr
->cbr_ds
== ds
&&
485 cbr
->cbr_func
== callback
&&
486 cbr
->cbr_arg
== cbarg
&&
487 strcmp(cbr
->cbr_pr
->pr_propname
, propname
) == 0)
492 mutex_exit(&dd
->dd_lock
);
493 return (SET_ERROR(ENOMSG
));
496 list_remove(&ds
->ds_prop_cbs
, cbr
);
497 list_remove(&cbr
->cbr_pr
->pr_cbs
, cbr
);
498 mutex_exit(&dd
->dd_lock
);
499 kmem_free(cbr
, sizeof (dsl_prop_cb_record_t
));
505 * Unregister all callbacks that are registered with the
506 * given callback argument.
509 dsl_prop_unregister_all(dsl_dataset_t
*ds
, void *cbarg
)
511 dsl_prop_cb_record_t
*cbr
, *next_cbr
;
513 dsl_dir_t
*dd
= ds
->ds_dir
;
515 mutex_enter(&dd
->dd_lock
);
516 next_cbr
= list_head(&ds
->ds_prop_cbs
);
517 while (next_cbr
!= NULL
) {
519 next_cbr
= list_next(&ds
->ds_prop_cbs
, cbr
);
520 if (cbr
->cbr_arg
== cbarg
) {
521 list_remove(&ds
->ds_prop_cbs
, cbr
);
522 list_remove(&cbr
->cbr_pr
->pr_cbs
, cbr
);
523 kmem_free(cbr
, sizeof (dsl_prop_cb_record_t
));
526 mutex_exit(&dd
->dd_lock
);
530 dsl_prop_hascb(dsl_dataset_t
*ds
)
532 return (!list_is_empty(&ds
->ds_prop_cbs
));
536 dsl_prop_notify_all_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
539 dsl_dir_t
*dd
= ds
->ds_dir
;
540 dsl_prop_record_t
*pr
;
541 dsl_prop_cb_record_t
*cbr
;
543 mutex_enter(&dd
->dd_lock
);
544 for (pr
= list_head(&dd
->dd_props
);
545 pr
; pr
= list_next(&dd
->dd_props
, pr
)) {
546 for (cbr
= list_head(&pr
->pr_cbs
); cbr
;
547 cbr
= list_next(&pr
->pr_cbs
, cbr
)) {
551 * Callback entries do not have holds on their
552 * datasets so that datasets with registered
553 * callbacks are still eligible for eviction.
554 * Unlike operations to update properties on a
555 * single dataset, we are performing a recursive
556 * descent of related head datasets. The caller
557 * of this function only has a dataset hold on
558 * the passed in head dataset, not the snapshots
559 * associated with this dataset. Without a hold,
560 * the dataset pointer within callback records
561 * for snapshots can be invalidated by eviction
564 * Use dsl_dataset_try_add_ref() to verify
565 * that the dataset for a snapshot has not
566 * begun eviction processing and to prevent
567 * eviction from occurring for the duration of
568 * the callback. If the hold attempt fails,
569 * this object is already being evicted and the
570 * callback can be safely ignored.
572 if (ds
!= cbr
->cbr_ds
&&
573 !dsl_dataset_try_add_ref(dp
, cbr
->cbr_ds
, FTAG
))
576 if (dsl_prop_get_ds(cbr
->cbr_ds
,
577 cbr
->cbr_pr
->pr_propname
, sizeof (value
), 1,
579 cbr
->cbr_func(cbr
->cbr_arg
, value
);
581 if (ds
!= cbr
->cbr_ds
)
582 dsl_dataset_rele(cbr
->cbr_ds
, FTAG
);
585 mutex_exit(&dd
->dd_lock
);
591 * Update all property values for ddobj & its descendants. This is used
592 * when renaming the dir.
595 dsl_prop_notify_all(dsl_dir_t
*dd
)
597 dsl_pool_t
*dp
= dd
->dd_pool
;
598 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
599 (void) dmu_objset_find_dp(dp
, dd
->dd_object
, dsl_prop_notify_all_cb
,
600 NULL
, DS_FIND_CHILDREN
);
604 dsl_prop_changed_notify(dsl_pool_t
*dp
, uint64_t ddobj
,
605 const char *propname
, uint64_t value
, int first
)
608 dsl_prop_record_t
*pr
;
609 dsl_prop_cb_record_t
*cbr
;
610 objset_t
*mos
= dp
->dp_meta_objset
;
615 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
616 err
= dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
);
622 * If the prop is set here, then this change is not
623 * being inherited here or below; stop the recursion.
625 err
= zap_contains(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
,
628 dsl_dir_rele(dd
, FTAG
);
631 ASSERT3U(err
, ==, ENOENT
);
634 mutex_enter(&dd
->dd_lock
);
635 pr
= dsl_prop_record_find(dd
, propname
);
637 for (cbr
= list_head(&pr
->pr_cbs
); cbr
;
638 cbr
= list_next(&pr
->pr_cbs
, cbr
)) {
642 * cbr->cbr_ds may be invalidated due to eviction,
643 * requiring the use of dsl_dataset_try_add_ref().
644 * See comment block in dsl_prop_notify_all_cb()
647 if (!dsl_dataset_try_add_ref(dp
, cbr
->cbr_ds
, FTAG
))
650 propobj
= dsl_dataset_phys(cbr
->cbr_ds
)->ds_props_obj
;
653 * If the property is not set on this ds, then it is
654 * inherited here; call the callback.
657 zap_contains(mos
, propobj
, propname
) != 0)
658 cbr
->cbr_func(cbr
->cbr_arg
, value
);
660 dsl_dataset_rele(cbr
->cbr_ds
, FTAG
);
663 mutex_exit(&dd
->dd_lock
);
665 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
666 for (zap_cursor_init(&zc
, mos
,
667 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
668 zap_cursor_retrieve(&zc
, za
) == 0;
669 zap_cursor_advance(&zc
)) {
670 dsl_prop_changed_notify(dp
, za
->za_first_integer
,
671 propname
, value
, FALSE
);
673 kmem_free(za
, sizeof (zap_attribute_t
));
674 zap_cursor_fini(&zc
);
675 dsl_dir_rele(dd
, FTAG
);
680 * For newer values in zfs index type properties, we add a new key
681 * propname$iuv (iuv = Ignore Unknown Values) to the properties zap object
682 * to store the new property value and store the default value in the
683 * existing prop key. So that the propname$iuv key is ignored by the older zfs
684 * versions and the default property value from the existing prop key is
688 dsl_prop_set_iuv(objset_t
*mos
, uint64_t zapobj
, const char *propname
,
689 int intsz
, int numints
, const void *value
, dmu_tx_t
*tx
)
691 char *iuvstr
= kmem_asprintf("%s%s", propname
, ZPROP_IUV_SUFFIX
);
692 boolean_t iuv
= B_FALSE
;
693 zfs_prop_t prop
= zfs_name_to_prop(propname
);
696 case ZFS_PROP_REDUNDANT_METADATA
:
697 if (*(uint64_t *)value
== ZFS_REDUNDANT_METADATA_SOME
||
698 *(uint64_t *)value
== ZFS_REDUNDANT_METADATA_NONE
)
706 VERIFY0(zap_update(mos
, zapobj
, iuvstr
, intsz
, numints
,
708 uint64_t val
= zfs_prop_default_numeric(prop
);
709 VERIFY0(zap_update(mos
, zapobj
, propname
, intsz
, numints
,
712 zap_remove(mos
, zapobj
, iuvstr
, tx
);
714 kmem_strfree(iuvstr
);
718 dsl_prop_set_sync_impl(dsl_dataset_t
*ds
, const char *propname
,
719 zprop_source_t source
, int intsz
, int numints
, const void *value
,
722 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
723 uint64_t zapobj
, intval
, dummy
, count
;
726 const char *valstr
= NULL
;
732 uint64_t version
= spa_version(ds
->ds_dir
->dd_pool
->dp_spa
);
734 isint
= (dodefault(zfs_name_to_prop(propname
), 8, 1, &intval
) == 0);
736 if (ds
->ds_is_snapshot
) {
737 ASSERT(version
>= SPA_VERSION_SNAP_PROPS
);
738 if (dsl_dataset_phys(ds
)->ds_props_obj
== 0 &&
739 (source
& ZPROP_SRC_NONE
) == 0) {
740 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
741 dsl_dataset_phys(ds
)->ds_props_obj
=
743 DMU_OT_DSL_PROPS
, DMU_OT_NONE
, 0, tx
);
745 zapobj
= dsl_dataset_phys(ds
)->ds_props_obj
;
747 zapobj
= dsl_dir_phys(ds
->ds_dir
)->dd_props_zapobj
;
750 /* If we are removing objects from a non-existent ZAP just return */
754 if (version
< SPA_VERSION_RECVD_PROPS
) {
755 if (source
& ZPROP_SRC_NONE
)
756 source
= ZPROP_SRC_NONE
;
757 else if (source
& ZPROP_SRC_RECEIVED
)
758 source
= ZPROP_SRC_LOCAL
;
761 inheritstr
= kmem_asprintf("%s%s", propname
, ZPROP_INHERIT_SUFFIX
);
762 recvdstr
= kmem_asprintf("%s%s", propname
, ZPROP_RECVD_SUFFIX
);
763 iuvstr
= kmem_asprintf("%s%s", propname
, ZPROP_IUV_SUFFIX
);
765 switch ((int)source
) {
768 * revert to received value, if any (inherit -S)
770 * - remove propname$inherit
772 err
= zap_remove(mos
, zapobj
, propname
, tx
);
773 ASSERT(err
== 0 || err
== ENOENT
);
774 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
775 ASSERT(err
== 0 || err
== ENOENT
);
777 case ZPROP_SRC_LOCAL
:
779 * remove propname$inherit
780 * set propname -> value
781 * set propname$iuv -> new property value
783 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
784 ASSERT(err
== 0 || err
== ENOENT
);
785 VERIFY0(zap_update(mos
, zapobj
, propname
,
786 intsz
, numints
, value
, tx
));
787 (void) dsl_prop_set_iuv(mos
, zapobj
, propname
, intsz
,
790 case ZPROP_SRC_INHERITED
:
794 * - set propname$inherit
796 err
= zap_remove(mos
, zapobj
, propname
, tx
);
797 ASSERT(err
== 0 || err
== ENOENT
);
798 err
= zap_remove(mos
, zapobj
, iuvstr
, tx
);
799 ASSERT(err
== 0 || err
== ENOENT
);
800 if (version
>= SPA_VERSION_RECVD_PROPS
&&
801 dsl_prop_get_int_ds(ds
, ZPROP_HAS_RECVD
, &dummy
) == 0) {
803 VERIFY0(zap_update(mos
, zapobj
, inheritstr
,
807 case ZPROP_SRC_RECEIVED
:
809 * set propname$recvd -> value
811 err
= zap_update(mos
, zapobj
, recvdstr
,
812 intsz
, numints
, value
, tx
);
815 case (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
):
817 * clear local and received settings
819 * - remove propname$inherit
820 * - remove propname$recvd
822 err
= zap_remove(mos
, zapobj
, propname
, tx
);
823 ASSERT(err
== 0 || err
== ENOENT
);
824 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
825 ASSERT(err
== 0 || err
== ENOENT
);
827 case (ZPROP_SRC_NONE
| ZPROP_SRC_RECEIVED
):
829 * remove propname$recvd
831 err
= zap_remove(mos
, zapobj
, recvdstr
, tx
);
832 ASSERT(err
== 0 || err
== ENOENT
);
835 cmn_err(CE_PANIC
, "unexpected property source: %d", source
);
838 kmem_strfree(inheritstr
);
839 kmem_strfree(recvdstr
);
840 kmem_strfree(iuvstr
);
843 * If we are left with an empty snap zap we can destroy it.
844 * This will prevent unnecessary calls to zap_lookup() in
845 * the "zfs list" and "zfs get" code paths.
847 if (ds
->ds_is_snapshot
&&
848 zap_count(mos
, zapobj
, &count
) == 0 && count
== 0) {
849 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
850 dsl_dataset_phys(ds
)->ds_props_obj
= 0;
851 zap_destroy(mos
, zapobj
, tx
);
855 VERIFY0(dsl_prop_get_int_ds(ds
, propname
, &intval
));
857 if (ds
->ds_is_snapshot
) {
858 dsl_prop_cb_record_t
*cbr
;
860 * It's a snapshot; nothing can inherit this
861 * property, so just look for callbacks on this
864 mutex_enter(&ds
->ds_dir
->dd_lock
);
865 for (cbr
= list_head(&ds
->ds_prop_cbs
); cbr
;
866 cbr
= list_next(&ds
->ds_prop_cbs
, cbr
)) {
867 if (strcmp(cbr
->cbr_pr
->pr_propname
,
869 cbr
->cbr_func(cbr
->cbr_arg
, intval
);
871 mutex_exit(&ds
->ds_dir
->dd_lock
);
873 dsl_prop_changed_notify(ds
->ds_dir
->dd_pool
,
874 ds
->ds_dir
->dd_object
, propname
, intval
, TRUE
);
877 (void) snprintf(valbuf
, sizeof (valbuf
),
878 "%lld", (longlong_t
)intval
);
881 if (source
== ZPROP_SRC_LOCAL
) {
884 tbuf
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
885 if (dsl_prop_get_ds(ds
, propname
, 1,
886 ZAP_MAXVALUELEN
, tbuf
, NULL
) == 0)
891 spa_history_log_internal_ds(ds
, (source
== ZPROP_SRC_NONE
||
892 source
== ZPROP_SRC_INHERITED
) ? "inherit" : "set", tx
,
893 "%s=%s", propname
, (valstr
== NULL
? "" : valstr
));
896 kmem_free(tbuf
, ZAP_MAXVALUELEN
);
900 dsl_prop_set_int(const char *dsname
, const char *propname
,
901 zprop_source_t source
, uint64_t value
)
903 nvlist_t
*nvl
= fnvlist_alloc();
906 fnvlist_add_uint64(nvl
, propname
, value
);
907 error
= dsl_props_set(dsname
, source
, nvl
);
913 dsl_prop_set_string(const char *dsname
, const char *propname
,
914 zprop_source_t source
, const char *value
)
916 nvlist_t
*nvl
= fnvlist_alloc();
919 fnvlist_add_string(nvl
, propname
, value
);
920 error
= dsl_props_set(dsname
, source
, nvl
);
926 dsl_prop_inherit(const char *dsname
, const char *propname
,
927 zprop_source_t source
)
929 nvlist_t
*nvl
= fnvlist_alloc();
932 fnvlist_add_boolean(nvl
, propname
);
933 error
= dsl_props_set(dsname
, source
, nvl
);
939 dsl_props_set_check(void *arg
, dmu_tx_t
*tx
)
941 dsl_props_set_arg_t
*dpsa
= arg
;
942 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
945 nvpair_t
*elem
= NULL
;
948 err
= dsl_dataset_hold(dp
, dpsa
->dpsa_dsname
, FTAG
, &ds
);
952 version
= spa_version(ds
->ds_dir
->dd_pool
->dp_spa
);
953 while ((elem
= nvlist_next_nvpair(dpsa
->dpsa_props
, elem
)) != NULL
) {
954 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
955 dsl_dataset_rele(ds
, FTAG
);
956 return (SET_ERROR(ENAMETOOLONG
));
958 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
959 const char *valstr
= fnvpair_value_string(elem
);
960 if (strlen(valstr
) >= (version
<
961 SPA_VERSION_STMF_PROP
?
962 ZAP_OLDMAXVALUELEN
: ZAP_MAXVALUELEN
)) {
963 dsl_dataset_rele(ds
, FTAG
);
964 return (SET_ERROR(E2BIG
));
969 if (ds
->ds_is_snapshot
&& version
< SPA_VERSION_SNAP_PROPS
) {
970 dsl_dataset_rele(ds
, FTAG
);
971 return (SET_ERROR(ENOTSUP
));
973 dsl_dataset_rele(ds
, FTAG
);
978 dsl_props_set_sync_impl(dsl_dataset_t
*ds
, zprop_source_t source
,
979 nvlist_t
*props
, dmu_tx_t
*tx
)
981 nvpair_t
*elem
= NULL
;
983 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
984 nvpair_t
*pair
= elem
;
985 const char *name
= nvpair_name(pair
);
987 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
989 * This usually happens when we reuse the nvlist_t data
990 * returned by the counterpart dsl_prop_get_all_impl().
991 * For instance we do this to restore the original
992 * received properties when an error occurs in the
993 * zfs_ioc_recv() codepath.
995 nvlist_t
*attrs
= fnvpair_value_nvlist(pair
);
996 pair
= fnvlist_lookup_nvpair(attrs
, ZPROP_VALUE
);
999 if (nvpair_type(pair
) == DATA_TYPE_STRING
) {
1000 const char *value
= fnvpair_value_string(pair
);
1001 dsl_prop_set_sync_impl(ds
, name
,
1002 source
, 1, strlen(value
) + 1, value
, tx
);
1003 } else if (nvpair_type(pair
) == DATA_TYPE_UINT64
) {
1004 uint64_t intval
= fnvpair_value_uint64(pair
);
1005 dsl_prop_set_sync_impl(ds
, name
,
1006 source
, sizeof (intval
), 1, &intval
, tx
);
1007 } else if (nvpair_type(pair
) == DATA_TYPE_BOOLEAN
) {
1008 dsl_prop_set_sync_impl(ds
, name
,
1009 source
, 0, 0, NULL
, tx
);
1011 panic("invalid nvpair type");
1017 dsl_props_set_sync(void *arg
, dmu_tx_t
*tx
)
1019 dsl_props_set_arg_t
*dpsa
= arg
;
1020 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1023 VERIFY0(dsl_dataset_hold(dp
, dpsa
->dpsa_dsname
, FTAG
, &ds
));
1024 dsl_props_set_sync_impl(ds
, dpsa
->dpsa_source
, dpsa
->dpsa_props
, tx
);
1025 dsl_dataset_rele(ds
, FTAG
);
1029 * All-or-nothing; if any prop can't be set, nothing will be modified.
1032 dsl_props_set(const char *dsname
, zprop_source_t source
, nvlist_t
*props
)
1034 dsl_props_set_arg_t dpsa
;
1037 dpsa
.dpsa_dsname
= dsname
;
1038 dpsa
.dpsa_source
= source
;
1039 dpsa
.dpsa_props
= props
;
1042 * If the source includes NONE, then we will only be removing entries
1043 * from the ZAP object. In that case don't check for ENOSPC.
1045 if ((source
& ZPROP_SRC_NONE
) == 0)
1046 nblks
= 2 * fnvlist_num_pairs(props
);
1048 return (dsl_sync_task(dsname
, dsl_props_set_check
, dsl_props_set_sync
,
1049 &dpsa
, nblks
, ZFS_SPACE_CHECK_RESERVED
));
1052 typedef enum dsl_prop_getflags
{
1053 DSL_PROP_GET_INHERITING
= 0x1, /* searching parent of target ds */
1054 DSL_PROP_GET_SNAPSHOT
= 0x2, /* snapshot dataset */
1055 DSL_PROP_GET_LOCAL
= 0x4, /* local properties */
1056 DSL_PROP_GET_RECEIVED
= 0x8, /* received properties */
1057 } dsl_prop_getflags_t
;
1060 dsl_prop_get_all_impl(objset_t
*mos
, uint64_t propobj
,
1061 const char *setpoint
, dsl_prop_getflags_t flags
, nvlist_t
*nv
)
1067 for (zap_cursor_init(&zc
, mos
, propobj
);
1068 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
1069 zap_cursor_advance(&zc
)) {
1072 char buf
[ZAP_MAXNAMELEN
];
1075 const char *propname
;
1078 suffix
= strchr(za
.za_name
, '$');
1080 if (suffix
== NULL
) {
1082 * Skip local properties if we only want received
1085 if (flags
& DSL_PROP_GET_RECEIVED
)
1088 propname
= za
.za_name
;
1091 /* Skip if iuv entries are preset. */
1092 valstr
= kmem_asprintf("%s%s", propname
,
1094 err
= zap_contains(mos
, propobj
, valstr
);
1095 kmem_strfree(valstr
);
1098 } else if (strcmp(suffix
, ZPROP_INHERIT_SUFFIX
) == 0) {
1099 /* Skip explicitly inherited entries. */
1101 } else if (strcmp(suffix
, ZPROP_RECVD_SUFFIX
) == 0) {
1102 if (flags
& DSL_PROP_GET_LOCAL
)
1105 (void) strlcpy(buf
, za
.za_name
,
1106 MIN(sizeof (buf
), suffix
- za
.za_name
+ 1));
1109 if (!(flags
& DSL_PROP_GET_RECEIVED
)) {
1110 /* Skip if locally overridden. */
1111 err
= zap_contains(mos
, propobj
, propname
);
1117 /* Skip if explicitly inherited. */
1118 valstr
= kmem_asprintf("%s%s", propname
,
1119 ZPROP_INHERIT_SUFFIX
);
1120 err
= zap_contains(mos
, propobj
, valstr
);
1121 kmem_strfree(valstr
);
1128 source
= ((flags
& DSL_PROP_GET_INHERITING
) ?
1129 setpoint
: ZPROP_SOURCE_VAL_RECVD
);
1130 } else if (strcmp(suffix
, ZPROP_IUV_SUFFIX
) == 0) {
1131 (void) strlcpy(buf
, za
.za_name
,
1132 MIN(sizeof (buf
), suffix
- za
.za_name
+ 1));
1135 prop
= zfs_name_to_prop(propname
);
1137 if (dsl_prop_known_index(prop
,
1138 za
.za_first_integer
) != 1)
1142 * For backward compatibility, skip suffixes we don't
1148 prop
= zfs_name_to_prop(propname
);
1150 /* Skip non-inheritable properties. */
1151 if ((flags
& DSL_PROP_GET_INHERITING
) &&
1152 prop
!= ZPROP_USERPROP
&& !zfs_prop_inheritable(prop
))
1155 /* Skip properties not valid for this type. */
1156 if ((flags
& DSL_PROP_GET_SNAPSHOT
) && prop
!= ZPROP_USERPROP
&&
1157 !zfs_prop_valid_for_type(prop
, ZFS_TYPE_SNAPSHOT
, B_FALSE
))
1160 /* Skip properties already defined. */
1161 if (nvlist_exists(nv
, propname
))
1164 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1165 if (za
.za_integer_length
== 1) {
1169 char *tmp
= kmem_alloc(za
.za_num_integers
,
1171 err
= zap_lookup(mos
, propobj
,
1172 za
.za_name
, 1, za
.za_num_integers
, tmp
);
1174 kmem_free(tmp
, za
.za_num_integers
);
1177 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
,
1179 kmem_free(tmp
, za
.za_num_integers
);
1184 ASSERT(za
.za_integer_length
== 8);
1185 (void) nvlist_add_uint64(propval
, ZPROP_VALUE
,
1186 za
.za_first_integer
);
1189 VERIFY(nvlist_add_string(propval
, ZPROP_SOURCE
, source
) == 0);
1190 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
1191 nvlist_free(propval
);
1193 zap_cursor_fini(&zc
);
1200 * Iterate over all properties for this dataset and return them in an nvlist.
1203 dsl_prop_get_all_ds(dsl_dataset_t
*ds
, nvlist_t
**nvp
,
1204 dsl_prop_getflags_t flags
)
1206 dsl_dir_t
*dd
= ds
->ds_dir
;
1207 dsl_pool_t
*dp
= dd
->dd_pool
;
1208 objset_t
*mos
= dp
->dp_meta_objset
;
1210 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
1212 VERIFY(nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1214 if (ds
->ds_is_snapshot
)
1215 flags
|= DSL_PROP_GET_SNAPSHOT
;
1217 ASSERT(dsl_pool_config_held(dp
));
1219 if (dsl_dataset_phys(ds
)->ds_props_obj
!= 0) {
1220 ASSERT(flags
& DSL_PROP_GET_SNAPSHOT
);
1221 dsl_dataset_name(ds
, setpoint
);
1222 err
= dsl_prop_get_all_impl(mos
,
1223 dsl_dataset_phys(ds
)->ds_props_obj
, setpoint
, flags
, *nvp
);
1228 for (; dd
!= NULL
; dd
= dd
->dd_parent
) {
1229 if (dd
!= ds
->ds_dir
|| (flags
& DSL_PROP_GET_SNAPSHOT
)) {
1230 if (flags
& (DSL_PROP_GET_LOCAL
|
1231 DSL_PROP_GET_RECEIVED
))
1233 flags
|= DSL_PROP_GET_INHERITING
;
1235 dsl_dir_name(dd
, setpoint
);
1236 err
= dsl_prop_get_all_impl(mos
,
1237 dsl_dir_phys(dd
)->dd_props_zapobj
, setpoint
, flags
, *nvp
);
1251 dsl_prop_get_hasrecvd(const char *dsname
)
1256 dsl_prop_get_integer(dsname
, ZPROP_HAS_RECVD
, &dummy
, NULL
));
1260 dsl_prop_set_hasrecvd_impl(const char *dsname
, zprop_source_t source
)
1266 VERIFY0(spa_open(dsname
, &spa
, FTAG
));
1267 version
= spa_version(spa
);
1268 spa_close(spa
, FTAG
);
1270 if (version
>= SPA_VERSION_RECVD_PROPS
)
1271 error
= dsl_prop_set_int(dsname
, ZPROP_HAS_RECVD
, source
, 0);
1276 * Call after successfully receiving properties to ensure that only the first
1277 * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
1280 dsl_prop_set_hasrecvd(const char *dsname
)
1283 if (!dsl_prop_get_hasrecvd(dsname
))
1284 error
= dsl_prop_set_hasrecvd_impl(dsname
, ZPROP_SRC_LOCAL
);
1289 dsl_prop_unset_hasrecvd(const char *dsname
)
1291 VERIFY0(dsl_prop_set_hasrecvd_impl(dsname
, ZPROP_SRC_NONE
));
1295 dsl_prop_get_all(objset_t
*os
, nvlist_t
**nvp
)
1297 return (dsl_prop_get_all_ds(os
->os_dsl_dataset
, nvp
, 0));
1301 dsl_prop_get_received(const char *dsname
, nvlist_t
**nvp
)
1307 * Received properties are not distinguishable from local properties
1308 * until the dataset has received properties on or after
1309 * SPA_VERSION_RECVD_PROPS.
1311 dsl_prop_getflags_t flags
= (dsl_prop_get_hasrecvd(dsname
) ?
1312 DSL_PROP_GET_RECEIVED
: DSL_PROP_GET_LOCAL
);
1314 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1317 error
= dsl_prop_get_all_ds(os
->os_dsl_dataset
, nvp
, flags
);
1318 dmu_objset_rele(os
, FTAG
);
1323 dsl_prop_nvlist_add_uint64(nvlist_t
*nv
, zfs_prop_t prop
, uint64_t value
)
1326 const char *propname
= zfs_prop_to_name(prop
);
1327 uint64_t default_value
;
1329 if (nvlist_lookup_nvlist(nv
, propname
, &propval
) == 0) {
1330 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, value
) == 0);
1334 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1335 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, value
) == 0);
1336 /* Indicate the default source if we can. */
1337 if (dodefault(prop
, 8, 1, &default_value
) == 0 &&
1338 value
== default_value
) {
1339 VERIFY(nvlist_add_string(propval
, ZPROP_SOURCE
, "") == 0);
1341 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
1342 nvlist_free(propval
);
1346 dsl_prop_nvlist_add_string(nvlist_t
*nv
, zfs_prop_t prop
, const char *value
)
1349 const char *propname
= zfs_prop_to_name(prop
);
1351 if (nvlist_lookup_nvlist(nv
, propname
, &propval
) == 0) {
1352 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, value
) == 0);
1356 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1357 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, value
) == 0);
1358 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
1359 nvlist_free(propval
);
1362 #if defined(_KERNEL)
1363 EXPORT_SYMBOL(dsl_prop_register
);
1364 EXPORT_SYMBOL(dsl_prop_unregister
);
1365 EXPORT_SYMBOL(dsl_prop_unregister_all
);
1366 EXPORT_SYMBOL(dsl_prop_get
);
1367 EXPORT_SYMBOL(dsl_prop_get_integer
);
1368 EXPORT_SYMBOL(dsl_prop_get_all
);
1369 EXPORT_SYMBOL(dsl_prop_get_received
);
1370 EXPORT_SYMBOL(dsl_prop_get_ds
);
1371 EXPORT_SYMBOL(dsl_prop_get_int_ds
);
1372 EXPORT_SYMBOL(dsl_prop_get_dd
);
1373 EXPORT_SYMBOL(dsl_props_set
);
1374 EXPORT_SYMBOL(dsl_prop_set_int
);
1375 EXPORT_SYMBOL(dsl_prop_set_string
);
1376 EXPORT_SYMBOL(dsl_prop_inherit
);
1377 EXPORT_SYMBOL(dsl_prop_predict
);
1378 EXPORT_SYMBOL(dsl_prop_nvlist_add_uint64
);
1379 EXPORT_SYMBOL(dsl_prop_nvlist_add_string
);