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
= zap_attribute_alloc();
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 zap_attribute_free(za
);
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
)
701 case ZFS_PROP_SNAPDIR
:
702 if (*(uint64_t *)value
== ZFS_SNAPDIR_DISABLED
)
710 VERIFY0(zap_update(mos
, zapobj
, iuvstr
, intsz
, numints
,
712 uint64_t val
= zfs_prop_default_numeric(prop
);
713 VERIFY0(zap_update(mos
, zapobj
, propname
, intsz
, numints
,
716 zap_remove(mos
, zapobj
, iuvstr
, tx
);
718 kmem_strfree(iuvstr
);
722 dsl_prop_set_sync_impl(dsl_dataset_t
*ds
, const char *propname
,
723 zprop_source_t source
, int intsz
, int numints
, const void *value
,
726 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
727 uint64_t zapobj
, intval
, dummy
, count
;
730 const char *valstr
= NULL
;
736 uint64_t version
= spa_version(ds
->ds_dir
->dd_pool
->dp_spa
);
738 isint
= (dodefault(zfs_name_to_prop(propname
), 8, 1, &intval
) == 0);
740 if (ds
->ds_is_snapshot
) {
741 ASSERT(version
>= SPA_VERSION_SNAP_PROPS
);
742 if (dsl_dataset_phys(ds
)->ds_props_obj
== 0 &&
743 (source
& ZPROP_SRC_NONE
) == 0) {
744 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
745 dsl_dataset_phys(ds
)->ds_props_obj
=
747 DMU_OT_DSL_PROPS
, DMU_OT_NONE
, 0, tx
);
749 zapobj
= dsl_dataset_phys(ds
)->ds_props_obj
;
751 zapobj
= dsl_dir_phys(ds
->ds_dir
)->dd_props_zapobj
;
754 /* If we are removing objects from a non-existent ZAP just return */
758 if (version
< SPA_VERSION_RECVD_PROPS
) {
759 if (source
& ZPROP_SRC_NONE
)
760 source
= ZPROP_SRC_NONE
;
761 else if (source
& ZPROP_SRC_RECEIVED
)
762 source
= ZPROP_SRC_LOCAL
;
765 inheritstr
= kmem_asprintf("%s%s", propname
, ZPROP_INHERIT_SUFFIX
);
766 recvdstr
= kmem_asprintf("%s%s", propname
, ZPROP_RECVD_SUFFIX
);
767 iuvstr
= kmem_asprintf("%s%s", propname
, ZPROP_IUV_SUFFIX
);
769 switch ((int)source
) {
772 * revert to received value, if any (inherit -S)
774 * - remove propname$inherit
776 err
= zap_remove(mos
, zapobj
, propname
, tx
);
777 ASSERT(err
== 0 || err
== ENOENT
);
778 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
779 ASSERT(err
== 0 || err
== ENOENT
);
781 case ZPROP_SRC_LOCAL
:
783 * remove propname$inherit
784 * set propname -> value
785 * set propname$iuv -> new property value
787 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
788 ASSERT(err
== 0 || err
== ENOENT
);
789 VERIFY0(zap_update(mos
, zapobj
, propname
,
790 intsz
, numints
, value
, tx
));
791 (void) dsl_prop_set_iuv(mos
, zapobj
, propname
, intsz
,
794 case ZPROP_SRC_INHERITED
:
798 * - set propname$inherit
800 err
= zap_remove(mos
, zapobj
, propname
, tx
);
801 ASSERT(err
== 0 || err
== ENOENT
);
802 err
= zap_remove(mos
, zapobj
, iuvstr
, tx
);
803 ASSERT(err
== 0 || err
== ENOENT
);
804 if (version
>= SPA_VERSION_RECVD_PROPS
&&
805 dsl_prop_get_int_ds(ds
, ZPROP_HAS_RECVD
, &dummy
) == 0) {
807 VERIFY0(zap_update(mos
, zapobj
, inheritstr
,
811 case ZPROP_SRC_RECEIVED
:
813 * set propname$recvd -> value
815 err
= zap_update(mos
, zapobj
, recvdstr
,
816 intsz
, numints
, value
, tx
);
819 case (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
):
821 * clear local and received settings
823 * - remove propname$inherit
824 * - remove propname$recvd
826 err
= zap_remove(mos
, zapobj
, propname
, tx
);
827 ASSERT(err
== 0 || err
== ENOENT
);
828 err
= zap_remove(mos
, zapobj
, inheritstr
, tx
);
829 ASSERT(err
== 0 || err
== ENOENT
);
831 case (ZPROP_SRC_NONE
| ZPROP_SRC_RECEIVED
):
833 * remove propname$recvd
835 err
= zap_remove(mos
, zapobj
, recvdstr
, tx
);
836 ASSERT(err
== 0 || err
== ENOENT
);
839 cmn_err(CE_PANIC
, "unexpected property source: %d", source
);
842 kmem_strfree(inheritstr
);
843 kmem_strfree(recvdstr
);
844 kmem_strfree(iuvstr
);
847 * If we are left with an empty snap zap we can destroy it.
848 * This will prevent unnecessary calls to zap_lookup() in
849 * the "zfs list" and "zfs get" code paths.
851 if (ds
->ds_is_snapshot
&&
852 zap_count(mos
, zapobj
, &count
) == 0 && count
== 0) {
853 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
854 dsl_dataset_phys(ds
)->ds_props_obj
= 0;
855 zap_destroy(mos
, zapobj
, tx
);
859 VERIFY0(dsl_prop_get_int_ds(ds
, propname
, &intval
));
861 if (ds
->ds_is_snapshot
) {
862 dsl_prop_cb_record_t
*cbr
;
864 * It's a snapshot; nothing can inherit this
865 * property, so just look for callbacks on this
868 mutex_enter(&ds
->ds_dir
->dd_lock
);
869 for (cbr
= list_head(&ds
->ds_prop_cbs
); cbr
;
870 cbr
= list_next(&ds
->ds_prop_cbs
, cbr
)) {
871 if (strcmp(cbr
->cbr_pr
->pr_propname
,
873 cbr
->cbr_func(cbr
->cbr_arg
, intval
);
875 mutex_exit(&ds
->ds_dir
->dd_lock
);
877 dsl_prop_changed_notify(ds
->ds_dir
->dd_pool
,
878 ds
->ds_dir
->dd_object
, propname
, intval
, TRUE
);
881 (void) snprintf(valbuf
, sizeof (valbuf
),
882 "%lld", (longlong_t
)intval
);
885 if (source
== ZPROP_SRC_LOCAL
) {
888 tbuf
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
889 if (dsl_prop_get_ds(ds
, propname
, 1,
890 ZAP_MAXVALUELEN
, tbuf
, NULL
) == 0)
895 spa_history_log_internal_ds(ds
, (source
== ZPROP_SRC_NONE
||
896 source
== ZPROP_SRC_INHERITED
) ? "inherit" : "set", tx
,
897 "%s=%s", propname
, (valstr
== NULL
? "" : valstr
));
900 kmem_free(tbuf
, ZAP_MAXVALUELEN
);
904 dsl_prop_set_int(const char *dsname
, const char *propname
,
905 zprop_source_t source
, uint64_t value
)
907 nvlist_t
*nvl
= fnvlist_alloc();
910 fnvlist_add_uint64(nvl
, propname
, value
);
911 error
= dsl_props_set(dsname
, source
, nvl
);
917 dsl_prop_set_string(const char *dsname
, const char *propname
,
918 zprop_source_t source
, const char *value
)
920 nvlist_t
*nvl
= fnvlist_alloc();
923 fnvlist_add_string(nvl
, propname
, value
);
924 error
= dsl_props_set(dsname
, source
, nvl
);
930 dsl_prop_inherit(const char *dsname
, const char *propname
,
931 zprop_source_t source
)
933 nvlist_t
*nvl
= fnvlist_alloc();
936 fnvlist_add_boolean(nvl
, propname
);
937 error
= dsl_props_set(dsname
, source
, nvl
);
943 dsl_props_set_check(void *arg
, dmu_tx_t
*tx
)
945 dsl_props_set_arg_t
*dpsa
= arg
;
946 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
949 nvpair_t
*elem
= NULL
;
952 err
= dsl_dataset_hold(dp
, dpsa
->dpsa_dsname
, FTAG
, &ds
);
956 version
= spa_version(ds
->ds_dir
->dd_pool
->dp_spa
);
957 while ((elem
= nvlist_next_nvpair(dpsa
->dpsa_props
, elem
)) != NULL
) {
958 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
959 dsl_dataset_rele(ds
, FTAG
);
960 return (SET_ERROR(ENAMETOOLONG
));
962 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
963 const char *valstr
= fnvpair_value_string(elem
);
964 if (strlen(valstr
) >= (version
<
965 SPA_VERSION_STMF_PROP
?
966 ZAP_OLDMAXVALUELEN
: ZAP_MAXVALUELEN
)) {
967 dsl_dataset_rele(ds
, FTAG
);
968 return (SET_ERROR(E2BIG
));
973 if (ds
->ds_is_snapshot
&& version
< SPA_VERSION_SNAP_PROPS
) {
974 dsl_dataset_rele(ds
, FTAG
);
975 return (SET_ERROR(ENOTSUP
));
977 dsl_dataset_rele(ds
, FTAG
);
982 dsl_props_set_sync_impl(dsl_dataset_t
*ds
, zprop_source_t source
,
983 nvlist_t
*props
, dmu_tx_t
*tx
)
985 nvpair_t
*elem
= NULL
;
987 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
988 nvpair_t
*pair
= elem
;
989 const char *name
= nvpair_name(pair
);
991 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
993 * This usually happens when we reuse the nvlist_t data
994 * returned by the counterpart dsl_prop_get_all_impl().
995 * For instance we do this to restore the original
996 * received properties when an error occurs in the
997 * zfs_ioc_recv() codepath.
999 nvlist_t
*attrs
= fnvpair_value_nvlist(pair
);
1000 pair
= fnvlist_lookup_nvpair(attrs
, ZPROP_VALUE
);
1003 if (nvpair_type(pair
) == DATA_TYPE_STRING
) {
1004 const char *value
= fnvpair_value_string(pair
);
1005 dsl_prop_set_sync_impl(ds
, name
,
1006 source
, 1, strlen(value
) + 1, value
, tx
);
1007 } else if (nvpair_type(pair
) == DATA_TYPE_UINT64
) {
1008 uint64_t intval
= fnvpair_value_uint64(pair
);
1009 dsl_prop_set_sync_impl(ds
, name
,
1010 source
, sizeof (intval
), 1, &intval
, tx
);
1011 } else if (nvpair_type(pair
) == DATA_TYPE_BOOLEAN
) {
1012 dsl_prop_set_sync_impl(ds
, name
,
1013 source
, 0, 0, NULL
, tx
);
1015 panic("invalid nvpair type");
1021 dsl_props_set_sync(void *arg
, dmu_tx_t
*tx
)
1023 dsl_props_set_arg_t
*dpsa
= arg
;
1024 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1027 VERIFY0(dsl_dataset_hold(dp
, dpsa
->dpsa_dsname
, FTAG
, &ds
));
1028 dsl_props_set_sync_impl(ds
, dpsa
->dpsa_source
, dpsa
->dpsa_props
, tx
);
1029 dsl_dataset_rele(ds
, FTAG
);
1033 * All-or-nothing; if any prop can't be set, nothing will be modified.
1036 dsl_props_set(const char *dsname
, zprop_source_t source
, nvlist_t
*props
)
1038 dsl_props_set_arg_t dpsa
;
1041 dpsa
.dpsa_dsname
= dsname
;
1042 dpsa
.dpsa_source
= source
;
1043 dpsa
.dpsa_props
= props
;
1046 * If the source includes NONE, then we will only be removing entries
1047 * from the ZAP object. In that case don't check for ENOSPC.
1049 if ((source
& ZPROP_SRC_NONE
) == 0)
1050 nblks
= 2 * fnvlist_num_pairs(props
);
1052 return (dsl_sync_task(dsname
, dsl_props_set_check
, dsl_props_set_sync
,
1053 &dpsa
, nblks
, ZFS_SPACE_CHECK_RESERVED
));
1056 typedef enum dsl_prop_getflags
{
1057 DSL_PROP_GET_INHERITING
= 0x1, /* searching parent of target ds */
1058 DSL_PROP_GET_SNAPSHOT
= 0x2, /* snapshot dataset */
1059 DSL_PROP_GET_LOCAL
= 0x4, /* local properties */
1060 DSL_PROP_GET_RECEIVED
= 0x8, /* received properties */
1061 } dsl_prop_getflags_t
;
1064 dsl_prop_get_all_impl(objset_t
*mos
, uint64_t propobj
,
1065 const char *setpoint
, dsl_prop_getflags_t flags
, nvlist_t
*nv
)
1068 zap_attribute_t
*za
= zap_attribute_alloc();
1071 for (zap_cursor_init(&zc
, mos
, propobj
);
1072 (err
= zap_cursor_retrieve(&zc
, za
)) == 0;
1073 zap_cursor_advance(&zc
)) {
1076 char buf
[ZAP_MAXNAMELEN
];
1079 const char *propname
;
1082 suffix
= strchr(za
->za_name
, '$');
1084 if (suffix
== NULL
) {
1086 * Skip local properties if we only want received
1089 if (flags
& DSL_PROP_GET_RECEIVED
)
1092 propname
= za
->za_name
;
1095 /* Skip if iuv entries are preset. */
1096 valstr
= kmem_asprintf("%s%s", propname
,
1098 err
= zap_contains(mos
, propobj
, valstr
);
1099 kmem_strfree(valstr
);
1102 } else if (strcmp(suffix
, ZPROP_INHERIT_SUFFIX
) == 0) {
1103 /* Skip explicitly inherited entries. */
1105 } else if (strcmp(suffix
, ZPROP_RECVD_SUFFIX
) == 0) {
1106 if (flags
& DSL_PROP_GET_LOCAL
)
1109 (void) strlcpy(buf
, za
->za_name
,
1110 MIN(sizeof (buf
), suffix
- za
->za_name
+ 1));
1113 if (!(flags
& DSL_PROP_GET_RECEIVED
)) {
1114 /* Skip if locally overridden. */
1115 err
= zap_contains(mos
, propobj
, propname
);
1121 /* Skip if explicitly inherited. */
1122 valstr
= kmem_asprintf("%s%s", propname
,
1123 ZPROP_INHERIT_SUFFIX
);
1124 err
= zap_contains(mos
, propobj
, valstr
);
1125 kmem_strfree(valstr
);
1132 source
= ((flags
& DSL_PROP_GET_INHERITING
) ?
1133 setpoint
: ZPROP_SOURCE_VAL_RECVD
);
1134 } else if (strcmp(suffix
, ZPROP_IUV_SUFFIX
) == 0) {
1135 (void) strlcpy(buf
, za
->za_name
,
1136 MIN(sizeof (buf
), suffix
- za
->za_name
+ 1));
1139 prop
= zfs_name_to_prop(propname
);
1141 if (dsl_prop_known_index(prop
,
1142 za
->za_first_integer
) != 1)
1146 * For backward compatibility, skip suffixes we don't
1152 prop
= zfs_name_to_prop(propname
);
1154 /* Skip non-inheritable properties. */
1155 if ((flags
& DSL_PROP_GET_INHERITING
) &&
1156 prop
!= ZPROP_USERPROP
&& !zfs_prop_inheritable(prop
))
1159 /* Skip properties not valid for this type. */
1160 if ((flags
& DSL_PROP_GET_SNAPSHOT
) && prop
!= ZPROP_USERPROP
&&
1161 !zfs_prop_valid_for_type(prop
, ZFS_TYPE_SNAPSHOT
, B_FALSE
))
1164 /* Skip properties already defined. */
1165 if (nvlist_exists(nv
, propname
))
1168 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1169 if (za
->za_integer_length
== 1) {
1173 char *tmp
= kmem_alloc(za
->za_num_integers
,
1175 err
= zap_lookup(mos
, propobj
,
1176 za
->za_name
, 1, za
->za_num_integers
, tmp
);
1178 kmem_free(tmp
, za
->za_num_integers
);
1181 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
,
1183 kmem_free(tmp
, za
->za_num_integers
);
1188 ASSERT(za
->za_integer_length
== 8);
1189 (void) nvlist_add_uint64(propval
, ZPROP_VALUE
,
1190 za
->za_first_integer
);
1193 VERIFY(nvlist_add_string(propval
, ZPROP_SOURCE
, source
) == 0);
1194 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
1195 nvlist_free(propval
);
1197 zap_cursor_fini(&zc
);
1198 zap_attribute_free(za
);
1205 * Iterate over all properties for this dataset and return them in an nvlist.
1208 dsl_prop_get_all_ds(dsl_dataset_t
*ds
, nvlist_t
**nvp
,
1209 dsl_prop_getflags_t flags
)
1211 dsl_dir_t
*dd
= ds
->ds_dir
;
1212 dsl_pool_t
*dp
= dd
->dd_pool
;
1213 objset_t
*mos
= dp
->dp_meta_objset
;
1215 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
1217 VERIFY(nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1219 if (ds
->ds_is_snapshot
)
1220 flags
|= DSL_PROP_GET_SNAPSHOT
;
1222 ASSERT(dsl_pool_config_held(dp
));
1224 if (dsl_dataset_phys(ds
)->ds_props_obj
!= 0) {
1225 ASSERT(flags
& DSL_PROP_GET_SNAPSHOT
);
1226 dsl_dataset_name(ds
, setpoint
);
1227 err
= dsl_prop_get_all_impl(mos
,
1228 dsl_dataset_phys(ds
)->ds_props_obj
, setpoint
, flags
, *nvp
);
1233 for (; dd
!= NULL
; dd
= dd
->dd_parent
) {
1234 if (dd
!= ds
->ds_dir
|| (flags
& DSL_PROP_GET_SNAPSHOT
)) {
1235 if (flags
& (DSL_PROP_GET_LOCAL
|
1236 DSL_PROP_GET_RECEIVED
))
1238 flags
|= DSL_PROP_GET_INHERITING
;
1240 dsl_dir_name(dd
, setpoint
);
1241 err
= dsl_prop_get_all_impl(mos
,
1242 dsl_dir_phys(dd
)->dd_props_zapobj
, setpoint
, flags
, *nvp
);
1256 dsl_prop_get_hasrecvd(const char *dsname
)
1261 dsl_prop_get_integer(dsname
, ZPROP_HAS_RECVD
, &dummy
, NULL
));
1265 dsl_prop_set_hasrecvd_impl(const char *dsname
, zprop_source_t source
)
1271 VERIFY0(spa_open(dsname
, &spa
, FTAG
));
1272 version
= spa_version(spa
);
1273 spa_close(spa
, FTAG
);
1275 if (version
>= SPA_VERSION_RECVD_PROPS
)
1276 error
= dsl_prop_set_int(dsname
, ZPROP_HAS_RECVD
, source
, 0);
1281 * Call after successfully receiving properties to ensure that only the first
1282 * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
1285 dsl_prop_set_hasrecvd(const char *dsname
)
1288 if (!dsl_prop_get_hasrecvd(dsname
))
1289 error
= dsl_prop_set_hasrecvd_impl(dsname
, ZPROP_SRC_LOCAL
);
1294 dsl_prop_unset_hasrecvd(const char *dsname
)
1296 VERIFY0(dsl_prop_set_hasrecvd_impl(dsname
, ZPROP_SRC_NONE
));
1300 dsl_prop_get_all(objset_t
*os
, nvlist_t
**nvp
)
1302 return (dsl_prop_get_all_ds(os
->os_dsl_dataset
, nvp
, 0));
1306 dsl_prop_get_received(const char *dsname
, nvlist_t
**nvp
)
1312 * Received properties are not distinguishable from local properties
1313 * until the dataset has received properties on or after
1314 * SPA_VERSION_RECVD_PROPS.
1316 dsl_prop_getflags_t flags
= (dsl_prop_get_hasrecvd(dsname
) ?
1317 DSL_PROP_GET_RECEIVED
: DSL_PROP_GET_LOCAL
);
1319 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1322 error
= dsl_prop_get_all_ds(os
->os_dsl_dataset
, nvp
, flags
);
1323 dmu_objset_rele(os
, FTAG
);
1328 dsl_prop_nvlist_add_uint64(nvlist_t
*nv
, zfs_prop_t prop
, uint64_t value
)
1331 const char *propname
= zfs_prop_to_name(prop
);
1332 uint64_t default_value
;
1334 if (nvlist_lookup_nvlist(nv
, propname
, &propval
) == 0) {
1335 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, value
) == 0);
1339 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1340 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, value
) == 0);
1341 /* Indicate the default source if we can. */
1342 if (dodefault(prop
, 8, 1, &default_value
) == 0 &&
1343 value
== default_value
) {
1344 VERIFY(nvlist_add_string(propval
, ZPROP_SOURCE
, "") == 0);
1346 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
1347 nvlist_free(propval
);
1351 dsl_prop_nvlist_add_string(nvlist_t
*nv
, zfs_prop_t prop
, const char *value
)
1354 const char *propname
= zfs_prop_to_name(prop
);
1356 if (nvlist_lookup_nvlist(nv
, propname
, &propval
) == 0) {
1357 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, value
) == 0);
1361 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1362 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, value
) == 0);
1363 VERIFY(nvlist_add_nvlist(nv
, propname
, propval
) == 0);
1364 nvlist_free(propval
);
1367 #if defined(_KERNEL)
1368 EXPORT_SYMBOL(dsl_prop_register
);
1369 EXPORT_SYMBOL(dsl_prop_unregister
);
1370 EXPORT_SYMBOL(dsl_prop_unregister_all
);
1371 EXPORT_SYMBOL(dsl_prop_get
);
1372 EXPORT_SYMBOL(dsl_prop_get_integer
);
1373 EXPORT_SYMBOL(dsl_prop_get_all
);
1374 EXPORT_SYMBOL(dsl_prop_get_received
);
1375 EXPORT_SYMBOL(dsl_prop_get_ds
);
1376 EXPORT_SYMBOL(dsl_prop_get_int_ds
);
1377 EXPORT_SYMBOL(dsl_prop_get_dd
);
1378 EXPORT_SYMBOL(dsl_props_set
);
1379 EXPORT_SYMBOL(dsl_prop_set_int
);
1380 EXPORT_SYMBOL(dsl_prop_set_string
);
1381 EXPORT_SYMBOL(dsl_prop_inherit
);
1382 EXPORT_SYMBOL(dsl_prop_predict
);
1383 EXPORT_SYMBOL(dsl_prop_nvlist_add_uint64
);
1384 EXPORT_SYMBOL(dsl_prop_nvlist_add_string
);