xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_prop.c (revision 013023d4)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
24*013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
2792241e0bSTom Erickson #include <sys/zfs_context.h>
28fa9e4066Sahrens #include <sys/dmu.h>
297f7322feSeschrock #include <sys/dmu_objset.h>
30fa9e4066Sahrens #include <sys/dmu_tx.h>
31fa9e4066Sahrens #include <sys/dsl_dataset.h>
32fa9e4066Sahrens #include <sys/dsl_dir.h>
33fa9e4066Sahrens #include <sys/dsl_prop.h>
341d452cf5Sahrens #include <sys/dsl_synctask.h>
35fa9e4066Sahrens #include <sys/spa.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens 
39fa9e4066Sahrens #include "zfs_prop.h"
40fa9e4066Sahrens 
4192241e0bSTom Erickson #define	ZPROP_INHERIT_SUFFIX "$inherit"
4292241e0bSTom Erickson #define	ZPROP_RECVD_SUFFIX "$recvd"
4392241e0bSTom Erickson 
44fa9e4066Sahrens static int
4592241e0bSTom Erickson dodefault(const char *propname, int intsz, int numints, void *buf)
46fa9e4066Sahrens {
47fa9e4066Sahrens 	zfs_prop_t prop;
48fa9e4066Sahrens 
49da6c28aaSamw 	/*
50da6c28aaSamw 	 * The setonce properties are read-only, BUT they still
51da6c28aaSamw 	 * have a default value that can be used as the initial
52da6c28aaSamw 	 * value.
53da6c28aaSamw 	 */
54990b4856Slling 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL ||
55da6c28aaSamw 	    (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
56be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
57fa9e4066Sahrens 
5891ebeef5Sahrens 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
59fa9e4066Sahrens 		if (intsz != 1)
60be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
61990b4856Slling 		(void) strncpy(buf, zfs_prop_default_string(prop),
6292241e0bSTom Erickson 		    numints);
63fa9e4066Sahrens 	} else {
6492241e0bSTom Erickson 		if (intsz != 8 || numints < 1)
65be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
66fa9e4066Sahrens 
67fa9e4066Sahrens 		*(uint64_t *)buf = zfs_prop_default_numeric(prop);
68fa9e4066Sahrens 	}
69fa9e4066Sahrens 
70fa9e4066Sahrens 	return (0);
71fa9e4066Sahrens }
72fa9e4066Sahrens 
73bb0ade09Sahrens int
74bb0ade09Sahrens dsl_prop_get_dd(dsl_dir_t *dd, const char *propname,
7592241e0bSTom Erickson     int intsz, int numints, void *buf, char *setpoint, boolean_t snapshot)
76fa9e4066Sahrens {
7799653d4eSeschrock 	int err = ENOENT;
7892241e0bSTom Erickson 	dsl_dir_t *target = dd;
79bb0ade09Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
80e9dbad6fSeschrock 	zfs_prop_t prop;
8192241e0bSTom Erickson 	boolean_t inheritable;
8292241e0bSTom Erickson 	boolean_t inheriting = B_FALSE;
8392241e0bSTom Erickson 	char *inheritstr;
8492241e0bSTom Erickson 	char *recvdstr;
85fa9e4066Sahrens 
863b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dd->dd_pool));
87bb0ade09Sahrens 
88fa9e4066Sahrens 	if (setpoint)
89fa9e4066Sahrens 		setpoint[0] = '\0';
90fa9e4066Sahrens 
91e9dbad6fSeschrock 	prop = zfs_name_to_prop(propname);
9292241e0bSTom Erickson 	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
9392241e0bSTom Erickson 	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
9492241e0bSTom Erickson 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
95e9dbad6fSeschrock 
9699653d4eSeschrock 	/*
9792241e0bSTom Erickson 	 * Note: dd may become NULL, therefore we shouldn't dereference it
9892241e0bSTom Erickson 	 * after this loop.
9999653d4eSeschrock 	 */
10099653d4eSeschrock 	for (; dd != NULL; dd = dd->dd_parent) {
10192241e0bSTom Erickson 		if (dd != target || snapshot) {
10292241e0bSTom Erickson 			if (!inheritable)
10392241e0bSTom Erickson 				break;
10492241e0bSTom Erickson 			inheriting = B_TRUE;
10592241e0bSTom Erickson 		}
10692241e0bSTom Erickson 
10792241e0bSTom Erickson 		/* Check for a local value. */
10892241e0bSTom Erickson 		err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname,
10992241e0bSTom Erickson 		    intsz, numints, buf);
110fa9e4066Sahrens 		if (err != ENOENT) {
11192241e0bSTom Erickson 			if (setpoint != NULL && err == 0)
112fa9e4066Sahrens 				dsl_dir_name(dd, setpoint);
113fa9e4066Sahrens 			break;
114fa9e4066Sahrens 		}
115e9dbad6fSeschrock 
116e9dbad6fSeschrock 		/*
11792241e0bSTom Erickson 		 * Skip the check for a received value if there is an explicit
11892241e0bSTom Erickson 		 * inheritance entry.
119e9dbad6fSeschrock 		 */
12092241e0bSTom Erickson 		err = zap_contains(mos, dd->dd_phys->dd_props_zapobj,
12192241e0bSTom Erickson 		    inheritstr);
12292241e0bSTom Erickson 		if (err != 0 && err != ENOENT)
123e9dbad6fSeschrock 			break;
12492241e0bSTom Erickson 
12592241e0bSTom Erickson 		if (err == ENOENT) {
12692241e0bSTom Erickson 			/* Check for a received value. */
12792241e0bSTom Erickson 			err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj,
12892241e0bSTom Erickson 			    recvdstr, intsz, numints, buf);
12992241e0bSTom Erickson 			if (err != ENOENT) {
13092241e0bSTom Erickson 				if (setpoint != NULL && err == 0) {
13192241e0bSTom Erickson 					if (inheriting) {
13292241e0bSTom Erickson 						dsl_dir_name(dd, setpoint);
13392241e0bSTom Erickson 					} else {
13492241e0bSTom Erickson 						(void) strcpy(setpoint,
13592241e0bSTom Erickson 						    ZPROP_SOURCE_VAL_RECVD);
13692241e0bSTom Erickson 					}
13792241e0bSTom Erickson 				}
13892241e0bSTom Erickson 				break;
13992241e0bSTom Erickson 			}
14092241e0bSTom Erickson 		}
14192241e0bSTom Erickson 
14292241e0bSTom Erickson 		/*
14392241e0bSTom Erickson 		 * If we found an explicit inheritance entry, err is zero even
14492241e0bSTom Erickson 		 * though we haven't yet found the value, so reinitializing err
14592241e0bSTom Erickson 		 * at the end of the loop (instead of at the beginning) ensures
14692241e0bSTom Erickson 		 * that err has a valid post-loop value.
14792241e0bSTom Erickson 		 */
148be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
149fa9e4066Sahrens 	}
15092241e0bSTom Erickson 
151fa9e4066Sahrens 	if (err == ENOENT)
15292241e0bSTom Erickson 		err = dodefault(propname, intsz, numints, buf);
15392241e0bSTom Erickson 
15492241e0bSTom Erickson 	strfree(inheritstr);
15592241e0bSTom Erickson 	strfree(recvdstr);
156fa9e4066Sahrens 
157fa9e4066Sahrens 	return (err);
158fa9e4066Sahrens }
159fa9e4066Sahrens 
160bb0ade09Sahrens int
161bb0ade09Sahrens dsl_prop_get_ds(dsl_dataset_t *ds, const char *propname,
16292241e0bSTom Erickson     int intsz, int numints, void *buf, char *setpoint)
163bb0ade09Sahrens {
16492241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
16592241e0bSTom Erickson 	boolean_t inheritable;
16692241e0bSTom Erickson 	boolean_t snapshot;
16792241e0bSTom Erickson 	uint64_t zapobj;
16892241e0bSTom Erickson 
1693b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
17092241e0bSTom Erickson 	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
17192241e0bSTom Erickson 	snapshot = (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds));
17292241e0bSTom Erickson 	zapobj = (ds->ds_phys == NULL ? 0 : ds->ds_phys->ds_props_obj);
17392241e0bSTom Erickson 
17492241e0bSTom Erickson 	if (zapobj != 0) {
17592241e0bSTom Erickson 		objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
17692241e0bSTom Erickson 		int err;
177bb0ade09Sahrens 
17892241e0bSTom Erickson 		ASSERT(snapshot);
17992241e0bSTom Erickson 
18092241e0bSTom Erickson 		/* Check for a local value. */
18192241e0bSTom Erickson 		err = zap_lookup(mos, zapobj, propname, intsz, numints, buf);
182bb0ade09Sahrens 		if (err != ENOENT) {
18392241e0bSTom Erickson 			if (setpoint != NULL && err == 0)
184bb0ade09Sahrens 				dsl_dataset_name(ds, setpoint);
185bb0ade09Sahrens 			return (err);
186bb0ade09Sahrens 		}
18792241e0bSTom Erickson 
18892241e0bSTom Erickson 		/*
18992241e0bSTom Erickson 		 * Skip the check for a received value if there is an explicit
19092241e0bSTom Erickson 		 * inheritance entry.
19192241e0bSTom Erickson 		 */
19292241e0bSTom Erickson 		if (inheritable) {
19392241e0bSTom Erickson 			char *inheritstr = kmem_asprintf("%s%s", propname,
19492241e0bSTom Erickson 			    ZPROP_INHERIT_SUFFIX);
19592241e0bSTom Erickson 			err = zap_contains(mos, zapobj, inheritstr);
19692241e0bSTom Erickson 			strfree(inheritstr);
19792241e0bSTom Erickson 			if (err != 0 && err != ENOENT)
19892241e0bSTom Erickson 				return (err);
19992241e0bSTom Erickson 		}
20092241e0bSTom Erickson 
20192241e0bSTom Erickson 		if (err == ENOENT) {
20292241e0bSTom Erickson 			/* Check for a received value. */
20392241e0bSTom Erickson 			char *recvdstr = kmem_asprintf("%s%s", propname,
20492241e0bSTom Erickson 			    ZPROP_RECVD_SUFFIX);
20592241e0bSTom Erickson 			err = zap_lookup(mos, zapobj, recvdstr,
20692241e0bSTom Erickson 			    intsz, numints, buf);
20792241e0bSTom Erickson 			strfree(recvdstr);
20892241e0bSTom Erickson 			if (err != ENOENT) {
20992241e0bSTom Erickson 				if (setpoint != NULL && err == 0)
21092241e0bSTom Erickson 					(void) strcpy(setpoint,
21192241e0bSTom Erickson 					    ZPROP_SOURCE_VAL_RECVD);
21292241e0bSTom Erickson 				return (err);
21392241e0bSTom Erickson 			}
21492241e0bSTom Erickson 		}
215bb0ade09Sahrens 	}
216bb0ade09Sahrens 
217bb0ade09Sahrens 	return (dsl_prop_get_dd(ds->ds_dir, propname,
21892241e0bSTom Erickson 	    intsz, numints, buf, setpoint, snapshot));
219bb0ade09Sahrens }
220bb0ade09Sahrens 
221fa9e4066Sahrens /*
222fa9e4066Sahrens  * Register interest in the named property.  We'll call the callback
223fa9e4066Sahrens  * once to notify it of the current property value, and again each time
224fa9e4066Sahrens  * the property changes, until this callback is unregistered.
225fa9e4066Sahrens  *
226fa9e4066Sahrens  * Return 0 on success, errno if the prop is not an integer value.
227fa9e4066Sahrens  */
228fa9e4066Sahrens int
229fa9e4066Sahrens dsl_prop_register(dsl_dataset_t *ds, const char *propname,
230fa9e4066Sahrens     dsl_prop_changed_cb_t *callback, void *cbarg)
231fa9e4066Sahrens {
23299653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
233bb0ade09Sahrens 	dsl_pool_t *dp = dd->dd_pool;
234fa9e4066Sahrens 	uint64_t value;
235fa9e4066Sahrens 	dsl_prop_cb_record_t *cbr;
236fa9e4066Sahrens 	int err;
237fa9e4066Sahrens 
2383b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
239fa9e4066Sahrens 
2403b2aab18SMatthew Ahrens 	err = dsl_prop_get_int_ds(ds, propname, &value);
2413b2aab18SMatthew Ahrens 	if (err != 0)
242fa9e4066Sahrens 		return (err);
243fa9e4066Sahrens 
244fa9e4066Sahrens 	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
24599653d4eSeschrock 	cbr->cbr_ds = ds;
246fa9e4066Sahrens 	cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
247fa9e4066Sahrens 	(void) strcpy((char *)cbr->cbr_propname, propname);
248fa9e4066Sahrens 	cbr->cbr_func = callback;
249fa9e4066Sahrens 	cbr->cbr_arg = cbarg;
250fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
251fa9e4066Sahrens 	list_insert_head(&dd->dd_prop_cbs, cbr);
252fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
253fa9e4066Sahrens 
254fa9e4066Sahrens 	cbr->cbr_func(cbr->cbr_arg, value);
255fa9e4066Sahrens 	return (0);
256fa9e4066Sahrens }
257fa9e4066Sahrens 
258fa9e4066Sahrens int
259bb0ade09Sahrens dsl_prop_get(const char *dsname, const char *propname,
260fa9e4066Sahrens     int intsz, int numints, void *buf, char *setpoint)
261fa9e4066Sahrens {
2623b2aab18SMatthew Ahrens 	objset_t *os;
2633b2aab18SMatthew Ahrens 	int error;
264fa9e4066Sahrens 
2653b2aab18SMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
2663b2aab18SMatthew Ahrens 	if (error != 0)
2673b2aab18SMatthew Ahrens 		return (error);
268fa9e4066Sahrens 
2693b2aab18SMatthew Ahrens 	error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
2703b2aab18SMatthew Ahrens 	    intsz, numints, buf, setpoint);
271fa9e4066Sahrens 
2723b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2733b2aab18SMatthew Ahrens 	return (error);
274fa9e4066Sahrens }
275fa9e4066Sahrens 
276fa9e4066Sahrens /*
277fa9e4066Sahrens  * Get the current property value.  It may have changed by the time this
278fa9e4066Sahrens  * function returns, so it is NOT safe to follow up with
279fa9e4066Sahrens  * dsl_prop_register() and assume that the value has not changed in
280fa9e4066Sahrens  * between.
281fa9e4066Sahrens  *
282fa9e4066Sahrens  * Return 0 on success, ENOENT if ddname is invalid.
283fa9e4066Sahrens  */
284fa9e4066Sahrens int
285fa9e4066Sahrens dsl_prop_get_integer(const char *ddname, const char *propname,
286fa9e4066Sahrens     uint64_t *valuep, char *setpoint)
287fa9e4066Sahrens {
288fa9e4066Sahrens 	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
289fa9e4066Sahrens }
290fa9e4066Sahrens 
2913b2aab18SMatthew Ahrens int
2923b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname,
2933b2aab18SMatthew Ahrens     uint64_t *valuep)
29492241e0bSTom Erickson {
2953b2aab18SMatthew Ahrens 	return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL));
29692241e0bSTom Erickson }
29792241e0bSTom Erickson 
29892241e0bSTom Erickson /*
29992241e0bSTom Erickson  * Predict the effective value of the given special property if it were set with
30092241e0bSTom Erickson  * the given value and source. This is not a general purpose function. It exists
30192241e0bSTom Erickson  * only to handle the special requirements of the quota and reservation
30292241e0bSTom Erickson  * properties. The fact that these properties are non-inheritable greatly
30392241e0bSTom Erickson  * simplifies the prediction logic.
30492241e0bSTom Erickson  *
30592241e0bSTom Erickson  * Returns 0 on success, a positive error code on failure, or -1 if called with
30692241e0bSTom Erickson  * a property not handled by this function.
30792241e0bSTom Erickson  */
30892241e0bSTom Erickson int
3093b2aab18SMatthew Ahrens dsl_prop_predict(dsl_dir_t *dd, const char *propname,
3103b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, uint64_t *newvalp)
31192241e0bSTom Erickson {
31292241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
31392241e0bSTom Erickson 	objset_t *mos;
31492241e0bSTom Erickson 	uint64_t zapobj;
31592241e0bSTom Erickson 	uint64_t version;
31692241e0bSTom Erickson 	char *recvdstr;
31792241e0bSTom Erickson 	int err = 0;
31892241e0bSTom Erickson 
31992241e0bSTom Erickson 	switch (prop) {
32092241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
32192241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
32292241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
32392241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
32492241e0bSTom Erickson 		break;
32592241e0bSTom Erickson 	default:
32692241e0bSTom Erickson 		return (-1);
32792241e0bSTom Erickson 	}
32892241e0bSTom Erickson 
32992241e0bSTom Erickson 	mos = dd->dd_pool->dp_meta_objset;
33092241e0bSTom Erickson 	zapobj = dd->dd_phys->dd_props_zapobj;
33192241e0bSTom Erickson 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
33292241e0bSTom Erickson 
33392241e0bSTom Erickson 	version = spa_version(dd->dd_pool->dp_spa);
33492241e0bSTom Erickson 	if (version < SPA_VERSION_RECVD_PROPS) {
33592241e0bSTom Erickson 		if (source & ZPROP_SRC_NONE)
33692241e0bSTom Erickson 			source = ZPROP_SRC_NONE;
33792241e0bSTom Erickson 		else if (source & ZPROP_SRC_RECEIVED)
33892241e0bSTom Erickson 			source = ZPROP_SRC_LOCAL;
33992241e0bSTom Erickson 	}
34092241e0bSTom Erickson 
34192241e0bSTom Erickson 	switch (source) {
34292241e0bSTom Erickson 	case ZPROP_SRC_NONE:
34392241e0bSTom Erickson 		/* Revert to the received value, if any. */
3443b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp);
34592241e0bSTom Erickson 		if (err == ENOENT)
3463b2aab18SMatthew Ahrens 			*newvalp = 0;
34792241e0bSTom Erickson 		break;
34892241e0bSTom Erickson 	case ZPROP_SRC_LOCAL:
3493b2aab18SMatthew Ahrens 		*newvalp = value;
35092241e0bSTom Erickson 		break;
35192241e0bSTom Erickson 	case ZPROP_SRC_RECEIVED:
35292241e0bSTom Erickson 		/*
35392241e0bSTom Erickson 		 * If there's no local setting, then the new received value will
35492241e0bSTom Erickson 		 * be the effective value.
35592241e0bSTom Erickson 		 */
3563b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
35792241e0bSTom Erickson 		if (err == ENOENT)
3583b2aab18SMatthew Ahrens 			*newvalp = value;
35992241e0bSTom Erickson 		break;
36092241e0bSTom Erickson 	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
36192241e0bSTom Erickson 		/*
36292241e0bSTom Erickson 		 * We're clearing the received value, so the local setting (if
36392241e0bSTom Erickson 		 * it exists) remains the effective value.
36492241e0bSTom Erickson 		 */
3653b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
36692241e0bSTom Erickson 		if (err == ENOENT)
3673b2aab18SMatthew Ahrens 			*newvalp = 0;
36892241e0bSTom Erickson 		break;
36992241e0bSTom Erickson 	default:
3703b2aab18SMatthew Ahrens 		panic("unexpected property source: %d", source);
37192241e0bSTom Erickson 	}
37292241e0bSTom Erickson 
37392241e0bSTom Erickson 	strfree(recvdstr);
37492241e0bSTom Erickson 
37592241e0bSTom Erickson 	if (err == ENOENT)
37692241e0bSTom Erickson 		return (0);
37792241e0bSTom Erickson 
37892241e0bSTom Erickson 	return (err);
37992241e0bSTom Erickson }
38092241e0bSTom Erickson 
381fa9e4066Sahrens /*
382fa9e4066Sahrens  * Unregister this callback.  Return 0 on success, ENOENT if ddname is
383fa9e4066Sahrens  * invalid, ENOMSG if no matching callback registered.
384fa9e4066Sahrens  */
385fa9e4066Sahrens int
386fa9e4066Sahrens dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
387fa9e4066Sahrens     dsl_prop_changed_cb_t *callback, void *cbarg)
388fa9e4066Sahrens {
38999653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
390fa9e4066Sahrens 	dsl_prop_cb_record_t *cbr;
391fa9e4066Sahrens 
392fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
393fa9e4066Sahrens 	for (cbr = list_head(&dd->dd_prop_cbs);
394fa9e4066Sahrens 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
39599653d4eSeschrock 		if (cbr->cbr_ds == ds &&
396fa9e4066Sahrens 		    cbr->cbr_func == callback &&
39799653d4eSeschrock 		    cbr->cbr_arg == cbarg &&
39899653d4eSeschrock 		    strcmp(cbr->cbr_propname, propname) == 0)
399fa9e4066Sahrens 			break;
400fa9e4066Sahrens 	}
401fa9e4066Sahrens 
402fa9e4066Sahrens 	if (cbr == NULL) {
403fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
404be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMSG));
405fa9e4066Sahrens 	}
406fa9e4066Sahrens 
407fa9e4066Sahrens 	list_remove(&dd->dd_prop_cbs, cbr);
408fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
409fa9e4066Sahrens 	kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
410fa9e4066Sahrens 	kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
411fa9e4066Sahrens 
412fa9e4066Sahrens 	return (0);
413fa9e4066Sahrens }
414fa9e4066Sahrens 
4153b2aab18SMatthew Ahrens boolean_t
4163b2aab18SMatthew Ahrens dsl_prop_hascb(dsl_dataset_t *ds)
41799653d4eSeschrock {
41899653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
4193b2aab18SMatthew Ahrens 	boolean_t rv = B_FALSE;
42099653d4eSeschrock 	dsl_prop_cb_record_t *cbr;
42199653d4eSeschrock 
42299653d4eSeschrock 	mutex_enter(&dd->dd_lock);
4233b2aab18SMatthew Ahrens 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
4243b2aab18SMatthew Ahrens 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
4253b2aab18SMatthew Ahrens 		if (cbr->cbr_ds == ds) {
4263b2aab18SMatthew Ahrens 			rv = B_TRUE;
4273b2aab18SMatthew Ahrens 			break;
4283b2aab18SMatthew Ahrens 		}
42999653d4eSeschrock 	}
43099653d4eSeschrock 	mutex_exit(&dd->dd_lock);
4313b2aab18SMatthew Ahrens 	return (rv);
4323b2aab18SMatthew Ahrens }
43399653d4eSeschrock 
4343b2aab18SMatthew Ahrens /* ARGSUSED */
4353b2aab18SMatthew Ahrens static int
4363b2aab18SMatthew Ahrens dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
4373b2aab18SMatthew Ahrens {
4383b2aab18SMatthew Ahrens 	dsl_dir_t *dd = ds->ds_dir;
4393b2aab18SMatthew Ahrens 	dsl_prop_cb_record_t *cbr;
4403b2aab18SMatthew Ahrens 
4413b2aab18SMatthew Ahrens 	mutex_enter(&dd->dd_lock);
4423b2aab18SMatthew Ahrens 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
4433b2aab18SMatthew Ahrens 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
4443b2aab18SMatthew Ahrens 		uint64_t value;
4453b2aab18SMatthew Ahrens 
4463b2aab18SMatthew Ahrens 		if (dsl_prop_get_ds(cbr->cbr_ds, cbr->cbr_propname,
4473b2aab18SMatthew Ahrens 		    sizeof (value), 1, &value, NULL) == 0)
4483b2aab18SMatthew Ahrens 			cbr->cbr_func(cbr->cbr_arg, value);
4493b2aab18SMatthew Ahrens 	}
4503b2aab18SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
4513b2aab18SMatthew Ahrens 
4523b2aab18SMatthew Ahrens 	return (0);
4533b2aab18SMatthew Ahrens }
4543b2aab18SMatthew Ahrens 
4553b2aab18SMatthew Ahrens /*
4563b2aab18SMatthew Ahrens  * Update all property values for ddobj & its descendants.  This is used
4573b2aab18SMatthew Ahrens  * when renaming the dir.
4583b2aab18SMatthew Ahrens  */
4593b2aab18SMatthew Ahrens void
4603b2aab18SMatthew Ahrens dsl_prop_notify_all(dsl_dir_t *dd)
4613b2aab18SMatthew Ahrens {
4623b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dd->dd_pool;
4633b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
4643b2aab18SMatthew Ahrens 	(void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb,
4653b2aab18SMatthew Ahrens 	    NULL, DS_FIND_CHILDREN);
46699653d4eSeschrock }
46799653d4eSeschrock 
468fa9e4066Sahrens static void
469fa9e4066Sahrens dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
470fa9e4066Sahrens     const char *propname, uint64_t value, int first)
471fa9e4066Sahrens {
472fa9e4066Sahrens 	dsl_dir_t *dd;
473fa9e4066Sahrens 	dsl_prop_cb_record_t *cbr;
474fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
4751d452cf5Sahrens 	zap_cursor_t zc;
476d8d77200Sahrens 	zap_attribute_t *za;
477fa9e4066Sahrens 	int err;
478fa9e4066Sahrens 
4793b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
4803b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
481ea8dc4b6Seschrock 	if (err)
482ea8dc4b6Seschrock 		return;
483fa9e4066Sahrens 
484fa9e4066Sahrens 	if (!first) {
485fa9e4066Sahrens 		/*
486fa9e4066Sahrens 		 * If the prop is set here, then this change is not
487fa9e4066Sahrens 		 * being inherited here or below; stop the recursion.
488fa9e4066Sahrens 		 */
48992241e0bSTom Erickson 		err = zap_contains(mos, dd->dd_phys->dd_props_zapobj, propname);
490fa9e4066Sahrens 		if (err == 0) {
4913b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
492fa9e4066Sahrens 			return;
493fa9e4066Sahrens 		}
494fa9e4066Sahrens 		ASSERT3U(err, ==, ENOENT);
495fa9e4066Sahrens 	}
496fa9e4066Sahrens 
497fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
498bb0ade09Sahrens 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
499bb0ade09Sahrens 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
500bb0ade09Sahrens 		uint64_t propobj = cbr->cbr_ds->ds_phys->ds_props_obj;
501bb0ade09Sahrens 
502bb0ade09Sahrens 		if (strcmp(cbr->cbr_propname, propname) != 0)
503bb0ade09Sahrens 			continue;
504bb0ade09Sahrens 
505bb0ade09Sahrens 		/*
506bb0ade09Sahrens 		 * If the property is set on this ds, then it is not
507bb0ade09Sahrens 		 * inherited here; don't call the callback.
508bb0ade09Sahrens 		 */
50992241e0bSTom Erickson 		if (propobj && 0 == zap_contains(mos, propobj, propname))
510bb0ade09Sahrens 			continue;
511bb0ade09Sahrens 
512bb0ade09Sahrens 		cbr->cbr_func(cbr->cbr_arg, value);
513fa9e4066Sahrens 	}
514fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
515fa9e4066Sahrens 
516d8d77200Sahrens 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
5171d452cf5Sahrens 	for (zap_cursor_init(&zc, mos,
5181d452cf5Sahrens 	    dd->dd_phys->dd_child_dir_zapobj);
519d8d77200Sahrens 	    zap_cursor_retrieve(&zc, za) == 0;
5201d452cf5Sahrens 	    zap_cursor_advance(&zc)) {
521d8d77200Sahrens 		dsl_prop_changed_notify(dp, za->za_first_integer,
5221d452cf5Sahrens 		    propname, value, FALSE);
523fa9e4066Sahrens 	}
524d8d77200Sahrens 	kmem_free(za, sizeof (zap_attribute_t));
5251d452cf5Sahrens 	zap_cursor_fini(&zc);
5263b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
527fa9e4066Sahrens }
528fa9e4066Sahrens 
52992241e0bSTom Erickson void
5303b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname,
5313b2aab18SMatthew Ahrens     zprop_source_t source, int intsz, int numints, const void *value,
5323b2aab18SMatthew Ahrens     dmu_tx_t *tx)
533fa9e4066Sahrens {
534bb0ade09Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
53592241e0bSTom Erickson 	uint64_t zapobj, intval, dummy;
5361d452cf5Sahrens 	int isint;
537ecd6cf80Smarks 	char valbuf[32];
5383b2aab18SMatthew Ahrens 	const char *valstr = NULL;
53992241e0bSTom Erickson 	char *inheritstr;
54092241e0bSTom Erickson 	char *recvdstr;
54192241e0bSTom Erickson 	char *tbuf = NULL;
54292241e0bSTom Erickson 	int err;
54392241e0bSTom Erickson 	uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa);
544fa9e4066Sahrens 
54592241e0bSTom Erickson 	isint = (dodefault(propname, 8, 1, &intval) == 0);
546fa9e4066Sahrens 
54792241e0bSTom Erickson 	if (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds)) {
54892241e0bSTom Erickson 		ASSERT(version >= SPA_VERSION_SNAP_PROPS);
549bb0ade09Sahrens 		if (ds->ds_phys->ds_props_obj == 0) {
550bb0ade09Sahrens 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
551bb0ade09Sahrens 			ds->ds_phys->ds_props_obj =
552bb0ade09Sahrens 			    zap_create(mos,
553bb0ade09Sahrens 			    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
554bb0ade09Sahrens 		}
555bb0ade09Sahrens 		zapobj = ds->ds_phys->ds_props_obj;
556bb0ade09Sahrens 	} else {
557bb0ade09Sahrens 		zapobj = ds->ds_dir->dd_phys->dd_props_zapobj;
558bb0ade09Sahrens 	}
559bb0ade09Sahrens 
56092241e0bSTom Erickson 	if (version < SPA_VERSION_RECVD_PROPS) {
56192241e0bSTom Erickson 		if (source & ZPROP_SRC_NONE)
56292241e0bSTom Erickson 			source = ZPROP_SRC_NONE;
56392241e0bSTom Erickson 		else if (source & ZPROP_SRC_RECEIVED)
56492241e0bSTom Erickson 			source = ZPROP_SRC_LOCAL;
56592241e0bSTom Erickson 	}
56692241e0bSTom Erickson 
56792241e0bSTom Erickson 	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
56892241e0bSTom Erickson 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
56992241e0bSTom Erickson 
57092241e0bSTom Erickson 	switch (source) {
57192241e0bSTom Erickson 	case ZPROP_SRC_NONE:
57292241e0bSTom Erickson 		/*
57392241e0bSTom Erickson 		 * revert to received value, if any (inherit -S)
57492241e0bSTom Erickson 		 * - remove propname
57592241e0bSTom Erickson 		 * - remove propname$inherit
57692241e0bSTom Erickson 		 */
57792241e0bSTom Erickson 		err = zap_remove(mos, zapobj, propname, tx);
57892241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
57992241e0bSTom Erickson 		err = zap_remove(mos, zapobj, inheritstr, tx);
58092241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
58192241e0bSTom Erickson 		break;
58292241e0bSTom Erickson 	case ZPROP_SRC_LOCAL:
58392241e0bSTom Erickson 		/*
58492241e0bSTom Erickson 		 * remove propname$inherit
58592241e0bSTom Erickson 		 * set propname -> value
58692241e0bSTom Erickson 		 */
58792241e0bSTom Erickson 		err = zap_remove(mos, zapobj, inheritstr, tx);
58892241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
5893b2aab18SMatthew Ahrens 		VERIFY0(zap_update(mos, zapobj, propname,
5903b2aab18SMatthew Ahrens 		    intsz, numints, value, tx));
59192241e0bSTom Erickson 		break;
59292241e0bSTom Erickson 	case ZPROP_SRC_INHERITED:
59392241e0bSTom Erickson 		/*
59492241e0bSTom Erickson 		 * explicitly inherit
59592241e0bSTom Erickson 		 * - remove propname
59692241e0bSTom Erickson 		 * - set propname$inherit
59792241e0bSTom Erickson 		 */
59892241e0bSTom Erickson 		err = zap_remove(mos, zapobj, propname, tx);
5991d452cf5Sahrens 		ASSERT(err == 0 || err == ENOENT);
60092241e0bSTom Erickson 		if (version >= SPA_VERSION_RECVD_PROPS &&
6013b2aab18SMatthew Ahrens 		    dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) {
60292241e0bSTom Erickson 			dummy = 0;
6033b2aab18SMatthew Ahrens 			VERIFY0(zap_update(mos, zapobj, inheritstr,
6043b2aab18SMatthew Ahrens 			    8, 1, &dummy, tx));
605fa9e4066Sahrens 		}
60692241e0bSTom Erickson 		break;
60792241e0bSTom Erickson 	case ZPROP_SRC_RECEIVED:
60892241e0bSTom Erickson 		/*
60992241e0bSTom Erickson 		 * set propname$recvd -> value
61092241e0bSTom Erickson 		 */
61192241e0bSTom Erickson 		err = zap_update(mos, zapobj, recvdstr,
6123b2aab18SMatthew Ahrens 		    intsz, numints, value, tx);
61392241e0bSTom Erickson 		ASSERT(err == 0);
61492241e0bSTom Erickson 		break;
61592241e0bSTom Erickson 	case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED):
61692241e0bSTom Erickson 		/*
61792241e0bSTom Erickson 		 * clear local and received settings
61892241e0bSTom Erickson 		 * - remove propname
61992241e0bSTom Erickson 		 * - remove propname$inherit
62092241e0bSTom Erickson 		 * - remove propname$recvd
62192241e0bSTom Erickson 		 */
62292241e0bSTom Erickson 		err = zap_remove(mos, zapobj, propname, tx);
62392241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
62492241e0bSTom Erickson 		err = zap_remove(mos, zapobj, inheritstr, tx);
62592241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
62692241e0bSTom Erickson 		/* FALLTHRU */
62792241e0bSTom Erickson 	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
62892241e0bSTom Erickson 		/*
62992241e0bSTom Erickson 		 * remove propname$recvd
63092241e0bSTom Erickson 		 */
63192241e0bSTom Erickson 		err = zap_remove(mos, zapobj, recvdstr, tx);
63292241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
63392241e0bSTom Erickson 		break;
63492241e0bSTom Erickson 	default:
63592241e0bSTom Erickson 		cmn_err(CE_PANIC, "unexpected property source: %d", source);
636fa9e4066Sahrens 	}
637fa9e4066Sahrens 
63892241e0bSTom Erickson 	strfree(inheritstr);
63992241e0bSTom Erickson 	strfree(recvdstr);
64092241e0bSTom Erickson 
6411d452cf5Sahrens 	if (isint) {
6423b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval));
64392241e0bSTom Erickson 
64492241e0bSTom Erickson 		if (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds)) {
645bb0ade09Sahrens 			dsl_prop_cb_record_t *cbr;
646bb0ade09Sahrens 			/*
647bb0ade09Sahrens 			 * It's a snapshot; nothing can inherit this
648bb0ade09Sahrens 			 * property, so just look for callbacks on this
649bb0ade09Sahrens 			 * ds here.
650bb0ade09Sahrens 			 */
651bb0ade09Sahrens 			mutex_enter(&ds->ds_dir->dd_lock);
652bb0ade09Sahrens 			for (cbr = list_head(&ds->ds_dir->dd_prop_cbs); cbr;
653bb0ade09Sahrens 			    cbr = list_next(&ds->ds_dir->dd_prop_cbs, cbr)) {
654bb0ade09Sahrens 				if (cbr->cbr_ds == ds &&
65592241e0bSTom Erickson 				    strcmp(cbr->cbr_propname, propname) == 0)
656bb0ade09Sahrens 					cbr->cbr_func(cbr->cbr_arg, intval);
657bb0ade09Sahrens 			}
658bb0ade09Sahrens 			mutex_exit(&ds->ds_dir->dd_lock);
659bb0ade09Sahrens 		} else {
660bb0ade09Sahrens 			dsl_prop_changed_notify(ds->ds_dir->dd_pool,
66192241e0bSTom Erickson 			    ds->ds_dir->dd_object, propname, intval, TRUE);
662bb0ade09Sahrens 		}
66392241e0bSTom Erickson 
664ecd6cf80Smarks 		(void) snprintf(valbuf, sizeof (valbuf),
665ecd6cf80Smarks 		    "%lld", (longlong_t)intval);
666ecd6cf80Smarks 		valstr = valbuf;
667ecd6cf80Smarks 	} else {
66892241e0bSTom Erickson 		if (source == ZPROP_SRC_LOCAL) {
6693b2aab18SMatthew Ahrens 			valstr = value;
67092241e0bSTom Erickson 		} else {
67192241e0bSTom Erickson 			tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
67292241e0bSTom Erickson 			if (dsl_prop_get_ds(ds, propname, 1,
67392241e0bSTom Erickson 			    ZAP_MAXVALUELEN, tbuf, NULL) == 0)
67492241e0bSTom Erickson 				valstr = tbuf;
67592241e0bSTom Erickson 		}
676ecd6cf80Smarks 	}
67792241e0bSTom Erickson 
6784445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE ||
6794445fffbSMatthew Ahrens 	    source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx,
6804445fffbSMatthew Ahrens 	    "%s=%s", propname, (valstr == NULL ? "" : valstr));
68192241e0bSTom Erickson 
68292241e0bSTom Erickson 	if (tbuf != NULL)
68392241e0bSTom Erickson 		kmem_free(tbuf, ZAP_MAXVALUELEN);
684fa9e4066Sahrens }
685fa9e4066Sahrens 
6863b2aab18SMatthew Ahrens int
6873b2aab18SMatthew Ahrens dsl_prop_set_int(const char *dsname, const char *propname,
6883b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value)
6895c0b6a79SRich Morris {
6903b2aab18SMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
6913b2aab18SMatthew Ahrens 	int error;
69292241e0bSTom Erickson 
6933b2aab18SMatthew Ahrens 	fnvlist_add_uint64(nvl, propname, value);
6943b2aab18SMatthew Ahrens 	error = dsl_props_set(dsname, source, nvl);
6953b2aab18SMatthew Ahrens 	fnvlist_free(nvl);
6963b2aab18SMatthew Ahrens 	return (error);
6975c0b6a79SRich Morris }
6985c0b6a79SRich Morris 
699a2eea2e1Sahrens int
7003b2aab18SMatthew Ahrens dsl_prop_set_string(const char *dsname, const char *propname,
7013b2aab18SMatthew Ahrens     zprop_source_t source, const char *value)
702fa9e4066Sahrens {
7033b2aab18SMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
7043b2aab18SMatthew Ahrens 	int error;
705bb0ade09Sahrens 
7063b2aab18SMatthew Ahrens 	fnvlist_add_string(nvl, propname, value);
7073b2aab18SMatthew Ahrens 	error = dsl_props_set(dsname, source, nvl);
7083b2aab18SMatthew Ahrens 	fnvlist_free(nvl);
7093b2aab18SMatthew Ahrens 	return (error);
7103b2aab18SMatthew Ahrens }
71192241e0bSTom Erickson 
7123b2aab18SMatthew Ahrens int
7133b2aab18SMatthew Ahrens dsl_prop_inherit(const char *dsname, const char *propname,
7143b2aab18SMatthew Ahrens     zprop_source_t source)
7153b2aab18SMatthew Ahrens {
7163b2aab18SMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
7173b2aab18SMatthew Ahrens 	int error;
718bb0ade09Sahrens 
7193b2aab18SMatthew Ahrens 	fnvlist_add_boolean(nvl, propname);
7203b2aab18SMatthew Ahrens 	error = dsl_props_set(dsname, source, nvl);
7213b2aab18SMatthew Ahrens 	fnvlist_free(nvl);
7223b2aab18SMatthew Ahrens 	return (error);
723fa9e4066Sahrens }
7247f7322feSeschrock 
7253b2aab18SMatthew Ahrens typedef struct dsl_props_set_arg {
7263b2aab18SMatthew Ahrens 	const char *dpsa_dsname;
7273b2aab18SMatthew Ahrens 	zprop_source_t dpsa_source;
7283b2aab18SMatthew Ahrens 	nvlist_t *dpsa_props;
7293b2aab18SMatthew Ahrens } dsl_props_set_arg_t;
7303b2aab18SMatthew Ahrens 
7313b2aab18SMatthew Ahrens static int
7323b2aab18SMatthew Ahrens dsl_props_set_check(void *arg, dmu_tx_t *tx)
7335c0b6a79SRich Morris {
7343b2aab18SMatthew Ahrens 	dsl_props_set_arg_t *dpsa = arg;
7353b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
7365c0b6a79SRich Morris 	dsl_dataset_t *ds;
737478ed9adSEric Taylor 	uint64_t version;
7385c0b6a79SRich Morris 	nvpair_t *elem = NULL;
7395c0b6a79SRich Morris 	int err;
7405c0b6a79SRich Morris 
7413b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds);
7423b2aab18SMatthew Ahrens 	if (err != 0)
743478ed9adSEric Taylor 		return (err);
7443b2aab18SMatthew Ahrens 
745478ed9adSEric Taylor 	version = spa_version(ds->ds_dir->dd_pool->dp_spa);
7463b2aab18SMatthew Ahrens 	while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) {
747478ed9adSEric Taylor 		if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
748478ed9adSEric Taylor 			dsl_dataset_rele(ds, FTAG);
749be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
750478ed9adSEric Taylor 		}
7515c0b6a79SRich Morris 		if (nvpair_type(elem) == DATA_TYPE_STRING) {
7523b2aab18SMatthew Ahrens 			char *valstr = fnvpair_value_string(elem);
753478ed9adSEric Taylor 			if (strlen(valstr) >= (version <
754478ed9adSEric Taylor 			    SPA_VERSION_STMF_PROP ?
755478ed9adSEric Taylor 			    ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) {
756478ed9adSEric Taylor 				dsl_dataset_rele(ds, FTAG);
757ccba0801SRich Morris 				return (E2BIG);
758478ed9adSEric Taylor 			}
7595c0b6a79SRich Morris 		}
7605c0b6a79SRich Morris 	}
7615c0b6a79SRich Morris 
7623b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds) && version < SPA_VERSION_SNAP_PROPS) {
7635c0b6a79SRich Morris 		dsl_dataset_rele(ds, FTAG);
764be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
7655c0b6a79SRich Morris 	}
7663b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
7673b2aab18SMatthew Ahrens 	return (0);
7683b2aab18SMatthew Ahrens }
7695c0b6a79SRich Morris 
7703b2aab18SMatthew Ahrens void
7713b2aab18SMatthew Ahrens dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source,
7723b2aab18SMatthew Ahrens     nvlist_t *props, dmu_tx_t *tx)
7733b2aab18SMatthew Ahrens {
7743b2aab18SMatthew Ahrens 	nvpair_t *elem = NULL;
77592241e0bSTom Erickson 
7763b2aab18SMatthew Ahrens 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
7773b2aab18SMatthew Ahrens 		nvpair_t *pair = elem;
7785c0b6a79SRich Morris 
7793b2aab18SMatthew Ahrens 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
7803b2aab18SMatthew Ahrens 			/*
7813b2aab18SMatthew Ahrens 			 * dsl_prop_get_all_impl() returns properties in this
7823b2aab18SMatthew Ahrens 			 * format.
7833b2aab18SMatthew Ahrens 			 */
7843b2aab18SMatthew Ahrens 			nvlist_t *attrs = fnvpair_value_nvlist(pair);
7853b2aab18SMatthew Ahrens 			pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
7863b2aab18SMatthew Ahrens 		}
7873b2aab18SMatthew Ahrens 
7883b2aab18SMatthew Ahrens 		if (nvpair_type(pair) == DATA_TYPE_STRING) {
7893b2aab18SMatthew Ahrens 			const char *value = fnvpair_value_string(pair);
7903b2aab18SMatthew Ahrens 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
7913b2aab18SMatthew Ahrens 			    source, 1, strlen(value) + 1, value, tx);
7923b2aab18SMatthew Ahrens 		} else if (nvpair_type(pair) == DATA_TYPE_UINT64) {
7933b2aab18SMatthew Ahrens 			uint64_t intval = fnvpair_value_uint64(pair);
7943b2aab18SMatthew Ahrens 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
7953b2aab18SMatthew Ahrens 			    source, sizeof (intval), 1, &intval, tx);
7963b2aab18SMatthew Ahrens 		} else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) {
7973b2aab18SMatthew Ahrens 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
7983b2aab18SMatthew Ahrens 			    source, 0, 0, NULL, tx);
7993b2aab18SMatthew Ahrens 		} else {
8003b2aab18SMatthew Ahrens 			panic("invalid nvpair type");
8013b2aab18SMatthew Ahrens 		}
8023b2aab18SMatthew Ahrens 	}
8033b2aab18SMatthew Ahrens }
8043b2aab18SMatthew Ahrens 
8053b2aab18SMatthew Ahrens static void
8063b2aab18SMatthew Ahrens dsl_props_set_sync(void *arg, dmu_tx_t *tx)
8073b2aab18SMatthew Ahrens {
8083b2aab18SMatthew Ahrens 	dsl_props_set_arg_t *dpsa = arg;
8093b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8103b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
8113b2aab18SMatthew Ahrens 
8123b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds));
8133b2aab18SMatthew Ahrens 	dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx);
8145c0b6a79SRich Morris 	dsl_dataset_rele(ds, FTAG);
8153b2aab18SMatthew Ahrens }
8163b2aab18SMatthew Ahrens 
8173b2aab18SMatthew Ahrens /*
8183b2aab18SMatthew Ahrens  * All-or-nothing; if any prop can't be set, nothing will be modified.
8193b2aab18SMatthew Ahrens  */
8203b2aab18SMatthew Ahrens int
8213b2aab18SMatthew Ahrens dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props)
8223b2aab18SMatthew Ahrens {
8233b2aab18SMatthew Ahrens 	dsl_props_set_arg_t dpsa;
8243b2aab18SMatthew Ahrens 	int nblks = 0;
8253b2aab18SMatthew Ahrens 
8263b2aab18SMatthew Ahrens 	dpsa.dpsa_dsname = dsname;
8273b2aab18SMatthew Ahrens 	dpsa.dpsa_source = source;
8283b2aab18SMatthew Ahrens 	dpsa.dpsa_props = props;
8293b2aab18SMatthew Ahrens 
8303b2aab18SMatthew Ahrens 	/*
8313b2aab18SMatthew Ahrens 	 * If the source includes NONE, then we will only be removing entries
8323b2aab18SMatthew Ahrens 	 * from the ZAP object.  In that case don't check for ENOSPC.
8333b2aab18SMatthew Ahrens 	 */
8343b2aab18SMatthew Ahrens 	if ((source & ZPROP_SRC_NONE) == 0)
8353b2aab18SMatthew Ahrens 		nblks = 2 * fnvlist_num_pairs(props);
8363b2aab18SMatthew Ahrens 
8373b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync,
8383b2aab18SMatthew Ahrens 	    &dpsa, nblks));
8395c0b6a79SRich Morris }
8405c0b6a79SRich Morris 
84192241e0bSTom Erickson typedef enum dsl_prop_getflags {
84292241e0bSTom Erickson 	DSL_PROP_GET_INHERITING = 0x1,	/* searching parent of target ds */
84392241e0bSTom Erickson 	DSL_PROP_GET_SNAPSHOT = 0x2,	/* snapshot dataset */
84492241e0bSTom Erickson 	DSL_PROP_GET_LOCAL = 0x4,	/* local properties */
84592241e0bSTom Erickson 	DSL_PROP_GET_RECEIVED = 0x8	/* received properties */
84692241e0bSTom Erickson } dsl_prop_getflags_t;
84792241e0bSTom Erickson 
84892241e0bSTom Erickson static int
84992241e0bSTom Erickson dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj,
85092241e0bSTom Erickson     const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv)
85192241e0bSTom Erickson {
85292241e0bSTom Erickson 	zap_cursor_t zc;
85392241e0bSTom Erickson 	zap_attribute_t za;
85492241e0bSTom Erickson 	int err = 0;
85592241e0bSTom Erickson 
85692241e0bSTom Erickson 	for (zap_cursor_init(&zc, mos, propobj);
85792241e0bSTom Erickson 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
85892241e0bSTom Erickson 	    zap_cursor_advance(&zc)) {
85992241e0bSTom Erickson 		nvlist_t *propval;
86092241e0bSTom Erickson 		zfs_prop_t prop;
86192241e0bSTom Erickson 		char buf[ZAP_MAXNAMELEN];
86292241e0bSTom Erickson 		char *valstr;
86392241e0bSTom Erickson 		const char *suffix;
86492241e0bSTom Erickson 		const char *propname;
86592241e0bSTom Erickson 		const char *source;
86692241e0bSTom Erickson 
86792241e0bSTom Erickson 		suffix = strchr(za.za_name, '$');
86892241e0bSTom Erickson 
86992241e0bSTom Erickson 		if (suffix == NULL) {
87092241e0bSTom Erickson 			/*
87192241e0bSTom Erickson 			 * Skip local properties if we only want received
87292241e0bSTom Erickson 			 * properties.
87392241e0bSTom Erickson 			 */
87492241e0bSTom Erickson 			if (flags & DSL_PROP_GET_RECEIVED)
87592241e0bSTom Erickson 				continue;
87692241e0bSTom Erickson 
87792241e0bSTom Erickson 			propname = za.za_name;
87892241e0bSTom Erickson 			source = setpoint;
87992241e0bSTom Erickson 		} else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) {
88092241e0bSTom Erickson 			/* Skip explicitly inherited entries. */
88192241e0bSTom Erickson 			continue;
88292241e0bSTom Erickson 		} else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) {
88392241e0bSTom Erickson 			if (flags & DSL_PROP_GET_LOCAL)
88492241e0bSTom Erickson 				continue;
88592241e0bSTom Erickson 
88692241e0bSTom Erickson 			(void) strncpy(buf, za.za_name, (suffix - za.za_name));
88792241e0bSTom Erickson 			buf[suffix - za.za_name] = '\0';
88892241e0bSTom Erickson 			propname = buf;
88992241e0bSTom Erickson 
89092241e0bSTom Erickson 			if (!(flags & DSL_PROP_GET_RECEIVED)) {
89192241e0bSTom Erickson 				/* Skip if locally overridden. */
89292241e0bSTom Erickson 				err = zap_contains(mos, propobj, propname);
89392241e0bSTom Erickson 				if (err == 0)
89492241e0bSTom Erickson 					continue;
89592241e0bSTom Erickson 				if (err != ENOENT)
89692241e0bSTom Erickson 					break;
89792241e0bSTom Erickson 
89892241e0bSTom Erickson 				/* Skip if explicitly inherited. */
89992241e0bSTom Erickson 				valstr = kmem_asprintf("%s%s", propname,
90092241e0bSTom Erickson 				    ZPROP_INHERIT_SUFFIX);
90192241e0bSTom Erickson 				err = zap_contains(mos, propobj, valstr);
90292241e0bSTom Erickson 				strfree(valstr);
90392241e0bSTom Erickson 				if (err == 0)
90492241e0bSTom Erickson 					continue;
90592241e0bSTom Erickson 				if (err != ENOENT)
90692241e0bSTom Erickson 					break;
90792241e0bSTom Erickson 			}
90892241e0bSTom Erickson 
90992241e0bSTom Erickson 			source = ((flags & DSL_PROP_GET_INHERITING) ?
91092241e0bSTom Erickson 			    setpoint : ZPROP_SOURCE_VAL_RECVD);
91192241e0bSTom Erickson 		} else {
91292241e0bSTom Erickson 			/*
91392241e0bSTom Erickson 			 * For backward compatibility, skip suffixes we don't
91492241e0bSTom Erickson 			 * recognize.
91592241e0bSTom Erickson 			 */
91692241e0bSTom Erickson 			continue;
91792241e0bSTom Erickson 		}
91892241e0bSTom Erickson 
91992241e0bSTom Erickson 		prop = zfs_name_to_prop(propname);
92092241e0bSTom Erickson 
92192241e0bSTom Erickson 		/* Skip non-inheritable properties. */
92292241e0bSTom Erickson 		if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL &&
92392241e0bSTom Erickson 		    !zfs_prop_inheritable(prop))
92492241e0bSTom Erickson 			continue;
92592241e0bSTom Erickson 
92692241e0bSTom Erickson 		/* Skip properties not valid for this type. */
92792241e0bSTom Erickson 		if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL &&
92892241e0bSTom Erickson 		    !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
92992241e0bSTom Erickson 			continue;
93092241e0bSTom Erickson 
93192241e0bSTom Erickson 		/* Skip properties already defined. */
93292241e0bSTom Erickson 		if (nvlist_exists(nv, propname))
93392241e0bSTom Erickson 			continue;
93492241e0bSTom Erickson 
93592241e0bSTom Erickson 		VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
93692241e0bSTom Erickson 		if (za.za_integer_length == 1) {
93792241e0bSTom Erickson 			/*
93892241e0bSTom Erickson 			 * String property
93992241e0bSTom Erickson 			 */
94092241e0bSTom Erickson 			char *tmp = kmem_alloc(za.za_num_integers,
94192241e0bSTom Erickson 			    KM_SLEEP);
94292241e0bSTom Erickson 			err = zap_lookup(mos, propobj,
94392241e0bSTom Erickson 			    za.za_name, 1, za.za_num_integers, tmp);
94492241e0bSTom Erickson 			if (err != 0) {
94592241e0bSTom Erickson 				kmem_free(tmp, za.za_num_integers);
94692241e0bSTom Erickson 				break;
94792241e0bSTom Erickson 			}
94892241e0bSTom Erickson 			VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
94992241e0bSTom Erickson 			    tmp) == 0);
95092241e0bSTom Erickson 			kmem_free(tmp, za.za_num_integers);
95192241e0bSTom Erickson 		} else {
95292241e0bSTom Erickson 			/*
95392241e0bSTom Erickson 			 * Integer property
95492241e0bSTom Erickson 			 */
95592241e0bSTom Erickson 			ASSERT(za.za_integer_length == 8);
95692241e0bSTom Erickson 			(void) nvlist_add_uint64(propval, ZPROP_VALUE,
95792241e0bSTom Erickson 			    za.za_first_integer);
95892241e0bSTom Erickson 		}
95992241e0bSTom Erickson 
96092241e0bSTom Erickson 		VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0);
96192241e0bSTom Erickson 		VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
96292241e0bSTom Erickson 		nvlist_free(propval);
96392241e0bSTom Erickson 	}
96492241e0bSTom Erickson 	zap_cursor_fini(&zc);
96592241e0bSTom Erickson 	if (err == ENOENT)
96692241e0bSTom Erickson 		err = 0;
96792241e0bSTom Erickson 	return (err);
96892241e0bSTom Erickson }
96992241e0bSTom Erickson 
9707f7322feSeschrock /*
9717f7322feSeschrock  * Iterate over all properties for this dataset and return them in an nvlist.
9727f7322feSeschrock  */
97392241e0bSTom Erickson static int
97492241e0bSTom Erickson dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp,
97592241e0bSTom Erickson     dsl_prop_getflags_t flags)
9767f7322feSeschrock {
97799653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
978bb0ade09Sahrens 	dsl_pool_t *dp = dd->dd_pool;
979bb0ade09Sahrens 	objset_t *mos = dp->dp_meta_objset;
98092241e0bSTom Erickson 	int err = 0;
98192241e0bSTom Erickson 	char setpoint[MAXNAMELEN];
9827f7322feSeschrock 
9837f7322feSeschrock 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9847f7322feSeschrock 
98592241e0bSTom Erickson 	if (dsl_dataset_is_snapshot(ds))
98692241e0bSTom Erickson 		flags |= DSL_PROP_GET_SNAPSHOT;
9877f7322feSeschrock 
9883b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
98992241e0bSTom Erickson 
99092241e0bSTom Erickson 	if (ds->ds_phys->ds_props_obj != 0) {
99192241e0bSTom Erickson 		ASSERT(flags & DSL_PROP_GET_SNAPSHOT);
99292241e0bSTom Erickson 		dsl_dataset_name(ds, setpoint);
99392241e0bSTom Erickson 		err = dsl_prop_get_all_impl(mos, ds->ds_phys->ds_props_obj,
99492241e0bSTom Erickson 		    setpoint, flags, *nvp);
99592241e0bSTom Erickson 		if (err)
99692241e0bSTom Erickson 			goto out;
99792241e0bSTom Erickson 	}
99892241e0bSTom Erickson 
99992241e0bSTom Erickson 	for (; dd != NULL; dd = dd->dd_parent) {
100092241e0bSTom Erickson 		if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) {
100192241e0bSTom Erickson 			if (flags & (DSL_PROP_GET_LOCAL |
100292241e0bSTom Erickson 			    DSL_PROP_GET_RECEIVED))
100392241e0bSTom Erickson 				break;
100492241e0bSTom Erickson 			flags |= DSL_PROP_GET_INHERITING;
1005bb0ade09Sahrens 		}
100692241e0bSTom Erickson 		dsl_dir_name(dd, setpoint);
100792241e0bSTom Erickson 		err = dsl_prop_get_all_impl(mos, dd->dd_phys->dd_props_zapobj,
100892241e0bSTom Erickson 		    setpoint, flags, *nvp);
100992241e0bSTom Erickson 		if (err)
101092241e0bSTom Erickson 			break;
101192241e0bSTom Erickson 	}
101292241e0bSTom Erickson out:
101392241e0bSTom Erickson 	return (err);
101492241e0bSTom Erickson }
1015a2eea2e1Sahrens 
101692241e0bSTom Erickson boolean_t
10173b2aab18SMatthew Ahrens dsl_prop_get_hasrecvd(const char *dsname)
101892241e0bSTom Erickson {
101992241e0bSTom Erickson 	uint64_t dummy;
1020bb0ade09Sahrens 
10213b2aab18SMatthew Ahrens 	return (0 ==
10223b2aab18SMatthew Ahrens 	    dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL));
102392241e0bSTom Erickson }
1024e9dbad6fSeschrock 
10253b2aab18SMatthew Ahrens static int
10263b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source)
102792241e0bSTom Erickson {
10283b2aab18SMatthew Ahrens 	uint64_t version;
10293b2aab18SMatthew Ahrens 	spa_t *spa;
10303b2aab18SMatthew Ahrens 	int error = 0;
10317f7322feSeschrock 
10323b2aab18SMatthew Ahrens 	VERIFY0(spa_open(dsname, &spa, FTAG));
10333b2aab18SMatthew Ahrens 	version = spa_version(spa);
10343b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
10357f7322feSeschrock 
10363b2aab18SMatthew Ahrens 	if (version >= SPA_VERSION_RECVD_PROPS)
10373b2aab18SMatthew Ahrens 		error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0);
10383b2aab18SMatthew Ahrens 	return (error);
103992241e0bSTom Erickson }
10407f7322feSeschrock 
104192241e0bSTom Erickson /*
104292241e0bSTom Erickson  * Call after successfully receiving properties to ensure that only the first
104392241e0bSTom Erickson  * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
104492241e0bSTom Erickson  */
10453b2aab18SMatthew Ahrens int
10463b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd(const char *dsname)
104792241e0bSTom Erickson {
10483b2aab18SMatthew Ahrens 	int error = 0;
10493b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(dsname))
10503b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL);
10513b2aab18SMatthew Ahrens 	return (error);
105292241e0bSTom Erickson }
10537f7322feSeschrock 
105492241e0bSTom Erickson void
10553b2aab18SMatthew Ahrens dsl_prop_unset_hasrecvd(const char *dsname)
105692241e0bSTom Erickson {
10573b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE));
105892241e0bSTom Erickson }
105992241e0bSTom Erickson 
106092241e0bSTom Erickson int
106192241e0bSTom Erickson dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
106292241e0bSTom Erickson {
106392241e0bSTom Erickson 	return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0));
106492241e0bSTom Erickson }
106592241e0bSTom Erickson 
106692241e0bSTom Erickson int
10673b2aab18SMatthew Ahrens dsl_prop_get_received(const char *dsname, nvlist_t **nvp)
106892241e0bSTom Erickson {
10693b2aab18SMatthew Ahrens 	objset_t *os;
10703b2aab18SMatthew Ahrens 	int error;
10713b2aab18SMatthew Ahrens 
107292241e0bSTom Erickson 	/*
107392241e0bSTom Erickson 	 * Received properties are not distinguishable from local properties
107492241e0bSTom Erickson 	 * until the dataset has received properties on or after
107592241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
107692241e0bSTom Erickson 	 */
10773b2aab18SMatthew Ahrens 	dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ?
107892241e0bSTom Erickson 	    DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL);
10793b2aab18SMatthew Ahrens 
10803b2aab18SMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
10813b2aab18SMatthew Ahrens 	if (error != 0)
10823b2aab18SMatthew Ahrens 		return (error);
10833b2aab18SMatthew Ahrens 	error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags);
10843b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
10853b2aab18SMatthew Ahrens 	return (error);
10867f7322feSeschrock }
1087a2eea2e1Sahrens 
1088a2eea2e1Sahrens void
1089a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
1090a2eea2e1Sahrens {
1091a2eea2e1Sahrens 	nvlist_t *propval;
109292241e0bSTom Erickson 	const char *propname = zfs_prop_to_name(prop);
109392241e0bSTom Erickson 	uint64_t default_value;
109492241e0bSTom Erickson 
109592241e0bSTom Erickson 	if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
109692241e0bSTom Erickson 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
109792241e0bSTom Erickson 		return;
109892241e0bSTom Erickson 	}
1099a2eea2e1Sahrens 
1100a2eea2e1Sahrens 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1101990b4856Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
110292241e0bSTom Erickson 	/* Indicate the default source if we can. */
110392241e0bSTom Erickson 	if (dodefault(propname, 8, 1, &default_value) == 0 &&
110492241e0bSTom Erickson 	    value == default_value) {
110592241e0bSTom Erickson 		VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0);
110692241e0bSTom Erickson 	}
110792241e0bSTom Erickson 	VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1108a2eea2e1Sahrens 	nvlist_free(propval);
1109a2eea2e1Sahrens }
1110a2eea2e1Sahrens 
1111a2eea2e1Sahrens void
1112a2eea2e1Sahrens dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
1113a2eea2e1Sahrens {
1114a2eea2e1Sahrens 	nvlist_t *propval;
111592241e0bSTom Erickson 	const char *propname = zfs_prop_to_name(prop);
111692241e0bSTom Erickson 
111792241e0bSTom Erickson 	if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
111892241e0bSTom Erickson 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
111992241e0bSTom Erickson 		return;
112092241e0bSTom Erickson 	}
1121a2eea2e1Sahrens 
1122a2eea2e1Sahrens 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1123990b4856Slling 	VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
112492241e0bSTom Erickson 	VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1125a2eea2e1Sahrens 	nvlist_free(propval);
1126a2eea2e1Sahrens }
1127