xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_prop.c (revision e57a022b)
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.
237d46dc6cSMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24013023d4SMartin 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. */
108c1379625SJustin T. Gibbs 		err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
109c1379625SJustin T. Gibbs 		    propname, 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 		 */
120c1379625SJustin T. Gibbs 		err = zap_contains(mos, dsl_dir_phys(dd)->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. */
127c1379625SJustin T. Gibbs 			err = zap_lookup(mos, dsl_dir_phys(dd)->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 	uint64_t zapobj;
16792241e0bSTom Erickson 
1683b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
16992241e0bSTom Erickson 	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
170c1379625SJustin T. Gibbs 	zapobj = dsl_dataset_phys(ds)->ds_props_obj;
17192241e0bSTom Erickson 
17292241e0bSTom Erickson 	if (zapobj != 0) {
17392241e0bSTom Erickson 		objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
17492241e0bSTom Erickson 		int err;
175bb0ade09Sahrens 
176bc9014e6SJustin Gibbs 		ASSERT(ds->ds_is_snapshot);
17792241e0bSTom Erickson 
17892241e0bSTom Erickson 		/* Check for a local value. */
17992241e0bSTom Erickson 		err = zap_lookup(mos, zapobj, propname, intsz, numints, buf);
180bb0ade09Sahrens 		if (err != ENOENT) {
18192241e0bSTom Erickson 			if (setpoint != NULL && err == 0)
182bb0ade09Sahrens 				dsl_dataset_name(ds, setpoint);
183bb0ade09Sahrens 			return (err);
184bb0ade09Sahrens 		}
18592241e0bSTom Erickson 
18692241e0bSTom Erickson 		/*
18792241e0bSTom Erickson 		 * Skip the check for a received value if there is an explicit
18892241e0bSTom Erickson 		 * inheritance entry.
18992241e0bSTom Erickson 		 */
19092241e0bSTom Erickson 		if (inheritable) {
19192241e0bSTom Erickson 			char *inheritstr = kmem_asprintf("%s%s", propname,
19292241e0bSTom Erickson 			    ZPROP_INHERIT_SUFFIX);
19392241e0bSTom Erickson 			err = zap_contains(mos, zapobj, inheritstr);
19492241e0bSTom Erickson 			strfree(inheritstr);
19592241e0bSTom Erickson 			if (err != 0 && err != ENOENT)
19692241e0bSTom Erickson 				return (err);
19792241e0bSTom Erickson 		}
19892241e0bSTom Erickson 
19992241e0bSTom Erickson 		if (err == ENOENT) {
20092241e0bSTom Erickson 			/* Check for a received value. */
20192241e0bSTom Erickson 			char *recvdstr = kmem_asprintf("%s%s", propname,
20292241e0bSTom Erickson 			    ZPROP_RECVD_SUFFIX);
20392241e0bSTom Erickson 			err = zap_lookup(mos, zapobj, recvdstr,
20492241e0bSTom Erickson 			    intsz, numints, buf);
20592241e0bSTom Erickson 			strfree(recvdstr);
20692241e0bSTom Erickson 			if (err != ENOENT) {
20792241e0bSTom Erickson 				if (setpoint != NULL && err == 0)
20892241e0bSTom Erickson 					(void) strcpy(setpoint,
20992241e0bSTom Erickson 					    ZPROP_SOURCE_VAL_RECVD);
21092241e0bSTom Erickson 				return (err);
21192241e0bSTom Erickson 			}
21292241e0bSTom Erickson 		}
213bb0ade09Sahrens 	}
214bb0ade09Sahrens 
215bb0ade09Sahrens 	return (dsl_prop_get_dd(ds->ds_dir, propname,
216bc9014e6SJustin Gibbs 	    intsz, numints, buf, setpoint, ds->ds_is_snapshot));
217bb0ade09Sahrens }
218bb0ade09Sahrens 
219fa9e4066Sahrens /*
220fa9e4066Sahrens  * Register interest in the named property.  We'll call the callback
221fa9e4066Sahrens  * once to notify it of the current property value, and again each time
222fa9e4066Sahrens  * the property changes, until this callback is unregistered.
223fa9e4066Sahrens  *
224fa9e4066Sahrens  * Return 0 on success, errno if the prop is not an integer value.
225fa9e4066Sahrens  */
226fa9e4066Sahrens int
227fa9e4066Sahrens dsl_prop_register(dsl_dataset_t *ds, const char *propname,
228fa9e4066Sahrens     dsl_prop_changed_cb_t *callback, void *cbarg)
229fa9e4066Sahrens {
23099653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
231bb0ade09Sahrens 	dsl_pool_t *dp = dd->dd_pool;
232fa9e4066Sahrens 	uint64_t value;
233fa9e4066Sahrens 	dsl_prop_cb_record_t *cbr;
234fa9e4066Sahrens 	int err;
235fa9e4066Sahrens 
2363b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
237fa9e4066Sahrens 
2383b2aab18SMatthew Ahrens 	err = dsl_prop_get_int_ds(ds, propname, &value);
2393b2aab18SMatthew Ahrens 	if (err != 0)
240fa9e4066Sahrens 		return (err);
241fa9e4066Sahrens 
242fa9e4066Sahrens 	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
24399653d4eSeschrock 	cbr->cbr_ds = ds;
244fa9e4066Sahrens 	cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
245fa9e4066Sahrens 	(void) strcpy((char *)cbr->cbr_propname, propname);
246fa9e4066Sahrens 	cbr->cbr_func = callback;
247fa9e4066Sahrens 	cbr->cbr_arg = cbarg;
248fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
249fa9e4066Sahrens 	list_insert_head(&dd->dd_prop_cbs, cbr);
250fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
251fa9e4066Sahrens 
252fa9e4066Sahrens 	cbr->cbr_func(cbr->cbr_arg, value);
253fa9e4066Sahrens 	return (0);
254fa9e4066Sahrens }
255fa9e4066Sahrens 
256fa9e4066Sahrens int
257bb0ade09Sahrens dsl_prop_get(const char *dsname, const char *propname,
258fa9e4066Sahrens     int intsz, int numints, void *buf, char *setpoint)
259fa9e4066Sahrens {
2603b2aab18SMatthew Ahrens 	objset_t *os;
2613b2aab18SMatthew Ahrens 	int error;
262fa9e4066Sahrens 
2633b2aab18SMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
2643b2aab18SMatthew Ahrens 	if (error != 0)
2653b2aab18SMatthew Ahrens 		return (error);
266fa9e4066Sahrens 
2673b2aab18SMatthew Ahrens 	error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
2683b2aab18SMatthew Ahrens 	    intsz, numints, buf, setpoint);
269fa9e4066Sahrens 
2703b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2713b2aab18SMatthew Ahrens 	return (error);
272fa9e4066Sahrens }
273fa9e4066Sahrens 
274fa9e4066Sahrens /*
275fa9e4066Sahrens  * Get the current property value.  It may have changed by the time this
276fa9e4066Sahrens  * function returns, so it is NOT safe to follow up with
277fa9e4066Sahrens  * dsl_prop_register() and assume that the value has not changed in
278fa9e4066Sahrens  * between.
279fa9e4066Sahrens  *
280fa9e4066Sahrens  * Return 0 on success, ENOENT if ddname is invalid.
281fa9e4066Sahrens  */
282fa9e4066Sahrens int
283fa9e4066Sahrens dsl_prop_get_integer(const char *ddname, const char *propname,
284fa9e4066Sahrens     uint64_t *valuep, char *setpoint)
285fa9e4066Sahrens {
286fa9e4066Sahrens 	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
287fa9e4066Sahrens }
288fa9e4066Sahrens 
2893b2aab18SMatthew Ahrens int
2903b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname,
2913b2aab18SMatthew Ahrens     uint64_t *valuep)
29292241e0bSTom Erickson {
2933b2aab18SMatthew Ahrens 	return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL));
29492241e0bSTom Erickson }
29592241e0bSTom Erickson 
29692241e0bSTom Erickson /*
29792241e0bSTom Erickson  * Predict the effective value of the given special property if it were set with
29892241e0bSTom Erickson  * the given value and source. This is not a general purpose function. It exists
29992241e0bSTom Erickson  * only to handle the special requirements of the quota and reservation
30092241e0bSTom Erickson  * properties. The fact that these properties are non-inheritable greatly
30192241e0bSTom Erickson  * simplifies the prediction logic.
30292241e0bSTom Erickson  *
30392241e0bSTom Erickson  * Returns 0 on success, a positive error code on failure, or -1 if called with
30492241e0bSTom Erickson  * a property not handled by this function.
30592241e0bSTom Erickson  */
30692241e0bSTom Erickson int
3073b2aab18SMatthew Ahrens dsl_prop_predict(dsl_dir_t *dd, const char *propname,
3083b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, uint64_t *newvalp)
30992241e0bSTom Erickson {
31092241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
31192241e0bSTom Erickson 	objset_t *mos;
31292241e0bSTom Erickson 	uint64_t zapobj;
31392241e0bSTom Erickson 	uint64_t version;
31492241e0bSTom Erickson 	char *recvdstr;
31592241e0bSTom Erickson 	int err = 0;
31692241e0bSTom Erickson 
31792241e0bSTom Erickson 	switch (prop) {
31892241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
31992241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
32092241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
32192241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
32292241e0bSTom Erickson 		break;
32392241e0bSTom Erickson 	default:
32492241e0bSTom Erickson 		return (-1);
32592241e0bSTom Erickson 	}
32692241e0bSTom Erickson 
32792241e0bSTom Erickson 	mos = dd->dd_pool->dp_meta_objset;
328c1379625SJustin T. Gibbs 	zapobj = dsl_dir_phys(dd)->dd_props_zapobj;
32992241e0bSTom Erickson 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
33092241e0bSTom Erickson 
33192241e0bSTom Erickson 	version = spa_version(dd->dd_pool->dp_spa);
33292241e0bSTom Erickson 	if (version < SPA_VERSION_RECVD_PROPS) {
33392241e0bSTom Erickson 		if (source & ZPROP_SRC_NONE)
33492241e0bSTom Erickson 			source = ZPROP_SRC_NONE;
33592241e0bSTom Erickson 		else if (source & ZPROP_SRC_RECEIVED)
33692241e0bSTom Erickson 			source = ZPROP_SRC_LOCAL;
33792241e0bSTom Erickson 	}
33892241e0bSTom Erickson 
33992241e0bSTom Erickson 	switch (source) {
34092241e0bSTom Erickson 	case ZPROP_SRC_NONE:
34192241e0bSTom Erickson 		/* Revert to the received value, if any. */
3423b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp);
34392241e0bSTom Erickson 		if (err == ENOENT)
3443b2aab18SMatthew Ahrens 			*newvalp = 0;
34592241e0bSTom Erickson 		break;
34692241e0bSTom Erickson 	case ZPROP_SRC_LOCAL:
3473b2aab18SMatthew Ahrens 		*newvalp = value;
34892241e0bSTom Erickson 		break;
34992241e0bSTom Erickson 	case ZPROP_SRC_RECEIVED:
35092241e0bSTom Erickson 		/*
35192241e0bSTom Erickson 		 * If there's no local setting, then the new received value will
35292241e0bSTom Erickson 		 * be the effective value.
35392241e0bSTom Erickson 		 */
3543b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
35592241e0bSTom Erickson 		if (err == ENOENT)
3563b2aab18SMatthew Ahrens 			*newvalp = value;
35792241e0bSTom Erickson 		break;
35892241e0bSTom Erickson 	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
35992241e0bSTom Erickson 		/*
36092241e0bSTom Erickson 		 * We're clearing the received value, so the local setting (if
36192241e0bSTom Erickson 		 * it exists) remains the effective value.
36292241e0bSTom Erickson 		 */
3633b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
36492241e0bSTom Erickson 		if (err == ENOENT)
3653b2aab18SMatthew Ahrens 			*newvalp = 0;
36692241e0bSTom Erickson 		break;
36792241e0bSTom Erickson 	default:
3683b2aab18SMatthew Ahrens 		panic("unexpected property source: %d", source);
36992241e0bSTom Erickson 	}
37092241e0bSTom Erickson 
37192241e0bSTom Erickson 	strfree(recvdstr);
37292241e0bSTom Erickson 
37392241e0bSTom Erickson 	if (err == ENOENT)
37492241e0bSTom Erickson 		return (0);
37592241e0bSTom Erickson 
37692241e0bSTom Erickson 	return (err);
37792241e0bSTom Erickson }
37892241e0bSTom Erickson 
379fa9e4066Sahrens /*
380fa9e4066Sahrens  * Unregister this callback.  Return 0 on success, ENOENT if ddname is
381f7170741SWill Andrews  * invalid, or ENOMSG if no matching callback registered.
382fa9e4066Sahrens  */
383fa9e4066Sahrens int
384fa9e4066Sahrens dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
385fa9e4066Sahrens     dsl_prop_changed_cb_t *callback, void *cbarg)
386fa9e4066Sahrens {
38799653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
388fa9e4066Sahrens 	dsl_prop_cb_record_t *cbr;
389fa9e4066Sahrens 
390fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
391fa9e4066Sahrens 	for (cbr = list_head(&dd->dd_prop_cbs);
392fa9e4066Sahrens 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
39399653d4eSeschrock 		if (cbr->cbr_ds == ds &&
394fa9e4066Sahrens 		    cbr->cbr_func == callback &&
39599653d4eSeschrock 		    cbr->cbr_arg == cbarg &&
39699653d4eSeschrock 		    strcmp(cbr->cbr_propname, propname) == 0)
397fa9e4066Sahrens 			break;
398fa9e4066Sahrens 	}
399fa9e4066Sahrens 
400fa9e4066Sahrens 	if (cbr == NULL) {
401fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
402be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMSG));
403fa9e4066Sahrens 	}
404fa9e4066Sahrens 
405fa9e4066Sahrens 	list_remove(&dd->dd_prop_cbs, cbr);
406fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
407fa9e4066Sahrens 	kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
408fa9e4066Sahrens 	kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
409fa9e4066Sahrens 
410fa9e4066Sahrens 	return (0);
411fa9e4066Sahrens }
412fa9e4066Sahrens 
4133b2aab18SMatthew Ahrens boolean_t
4143b2aab18SMatthew Ahrens dsl_prop_hascb(dsl_dataset_t *ds)
41599653d4eSeschrock {
41699653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
4173b2aab18SMatthew Ahrens 	boolean_t rv = B_FALSE;
41899653d4eSeschrock 	dsl_prop_cb_record_t *cbr;
41999653d4eSeschrock 
42099653d4eSeschrock 	mutex_enter(&dd->dd_lock);
4213b2aab18SMatthew Ahrens 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
4223b2aab18SMatthew Ahrens 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
4233b2aab18SMatthew Ahrens 		if (cbr->cbr_ds == ds) {
4243b2aab18SMatthew Ahrens 			rv = B_TRUE;
4253b2aab18SMatthew Ahrens 			break;
4263b2aab18SMatthew Ahrens 		}
42799653d4eSeschrock 	}
42899653d4eSeschrock 	mutex_exit(&dd->dd_lock);
4293b2aab18SMatthew Ahrens 	return (rv);
4303b2aab18SMatthew Ahrens }
43199653d4eSeschrock 
4323b2aab18SMatthew Ahrens /* ARGSUSED */
4333b2aab18SMatthew Ahrens static int
4343b2aab18SMatthew Ahrens dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
4353b2aab18SMatthew Ahrens {
4363b2aab18SMatthew Ahrens 	dsl_dir_t *dd = ds->ds_dir;
4373b2aab18SMatthew Ahrens 	dsl_prop_cb_record_t *cbr;
4383b2aab18SMatthew Ahrens 
4393b2aab18SMatthew Ahrens 	mutex_enter(&dd->dd_lock);
4403b2aab18SMatthew Ahrens 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
4413b2aab18SMatthew Ahrens 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
4423b2aab18SMatthew Ahrens 		uint64_t value;
4433b2aab18SMatthew Ahrens 
444*e57a022bSJustin T. Gibbs 		/*
445*e57a022bSJustin T. Gibbs 		 * Callback entries do not have holds on their datasets
446*e57a022bSJustin T. Gibbs 		 * so that datasets with registered callbacks are still
447*e57a022bSJustin T. Gibbs 		 * eligible for eviction.  Unlike operations on callbacks
448*e57a022bSJustin T. Gibbs 		 * for a single dataset, we are performing a recursive
449*e57a022bSJustin T. Gibbs 		 * descent of related datasets and the calling context
450*e57a022bSJustin T. Gibbs 		 * for this iteration only has a dataset hold on the root.
451*e57a022bSJustin T. Gibbs 		 * Without a hold, the callback's pointer to the dataset
452*e57a022bSJustin T. Gibbs 		 * could be invalidated by eviction at any time.
453*e57a022bSJustin T. Gibbs 		 *
454*e57a022bSJustin T. Gibbs 		 * Use dsl_dataset_try_add_ref() to verify that the
455*e57a022bSJustin T. Gibbs 		 * dataset has not begun eviction processing and to
456*e57a022bSJustin T. Gibbs 		 * prevent eviction from occurring for the duration
457*e57a022bSJustin T. Gibbs 		 * of the callback.  If the hold attempt fails, this
458*e57a022bSJustin T. Gibbs 		 * object is already being evicted and the callback can
459*e57a022bSJustin T. Gibbs 		 * be safely ignored.
460*e57a022bSJustin T. Gibbs 		 */
461*e57a022bSJustin T. Gibbs 		if (!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
462*e57a022bSJustin T. Gibbs 			continue;
463*e57a022bSJustin T. Gibbs 
4643b2aab18SMatthew Ahrens 		if (dsl_prop_get_ds(cbr->cbr_ds, cbr->cbr_propname,
4653b2aab18SMatthew Ahrens 		    sizeof (value), 1, &value, NULL) == 0)
4663b2aab18SMatthew Ahrens 			cbr->cbr_func(cbr->cbr_arg, value);
467*e57a022bSJustin T. Gibbs 
468*e57a022bSJustin T. Gibbs 		dsl_dataset_rele(cbr->cbr_ds, FTAG);
4693b2aab18SMatthew Ahrens 	}
4703b2aab18SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
4713b2aab18SMatthew Ahrens 
4723b2aab18SMatthew Ahrens 	return (0);
4733b2aab18SMatthew Ahrens }
4743b2aab18SMatthew Ahrens 
4753b2aab18SMatthew Ahrens /*
4763b2aab18SMatthew Ahrens  * Update all property values for ddobj & its descendants.  This is used
4773b2aab18SMatthew Ahrens  * when renaming the dir.
4783b2aab18SMatthew Ahrens  */
4793b2aab18SMatthew Ahrens void
4803b2aab18SMatthew Ahrens dsl_prop_notify_all(dsl_dir_t *dd)
4813b2aab18SMatthew Ahrens {
4823b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dd->dd_pool;
4833b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
4843b2aab18SMatthew Ahrens 	(void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb,
4853b2aab18SMatthew Ahrens 	    NULL, DS_FIND_CHILDREN);
48699653d4eSeschrock }
48799653d4eSeschrock 
488fa9e4066Sahrens static void
489fa9e4066Sahrens dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
490fa9e4066Sahrens     const char *propname, uint64_t value, int first)
491fa9e4066Sahrens {
492fa9e4066Sahrens 	dsl_dir_t *dd;
493fa9e4066Sahrens 	dsl_prop_cb_record_t *cbr;
494fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
4951d452cf5Sahrens 	zap_cursor_t zc;
496d8d77200Sahrens 	zap_attribute_t *za;
497fa9e4066Sahrens 	int err;
498fa9e4066Sahrens 
4993b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
5003b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
501ea8dc4b6Seschrock 	if (err)
502ea8dc4b6Seschrock 		return;
503fa9e4066Sahrens 
504fa9e4066Sahrens 	if (!first) {
505fa9e4066Sahrens 		/*
506fa9e4066Sahrens 		 * If the prop is set here, then this change is not
507fa9e4066Sahrens 		 * being inherited here or below; stop the recursion.
508fa9e4066Sahrens 		 */
509c1379625SJustin T. Gibbs 		err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
510c1379625SJustin T. Gibbs 		    propname);
511fa9e4066Sahrens 		if (err == 0) {
5123b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
513fa9e4066Sahrens 			return;
514fa9e4066Sahrens 		}
515fa9e4066Sahrens 		ASSERT3U(err, ==, ENOENT);
516fa9e4066Sahrens 	}
517fa9e4066Sahrens 
518fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
519bb0ade09Sahrens 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
520bb0ade09Sahrens 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
521*e57a022bSJustin T. Gibbs 		uint64_t propobj;
522bb0ade09Sahrens 
523*e57a022bSJustin T. Gibbs 		/*
524*e57a022bSJustin T. Gibbs 		 * cbr->cbf_ds may be invalidated due to eviction,
525*e57a022bSJustin T. Gibbs 		 * requiring the use of dsl_dataset_try_add_ref().
526*e57a022bSJustin T. Gibbs 		 * See comment block in dsl_prop_notify_all_cb()
527*e57a022bSJustin T. Gibbs 		 * for details.
528*e57a022bSJustin T. Gibbs 		 */
529*e57a022bSJustin T. Gibbs 		if (strcmp(cbr->cbr_propname, propname) != 0 ||
530*e57a022bSJustin T. Gibbs 		    !dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
531bb0ade09Sahrens 			continue;
532bb0ade09Sahrens 
533*e57a022bSJustin T. Gibbs 		propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
534*e57a022bSJustin T. Gibbs 
535bb0ade09Sahrens 		/*
536*e57a022bSJustin T. Gibbs 		 * If the property is not set on this ds, then it is
537*e57a022bSJustin T. Gibbs 		 * inherited here; call the callback.
538bb0ade09Sahrens 		 */
539*e57a022bSJustin T. Gibbs 		if (propobj == 0 || zap_contains(mos, propobj, propname) != 0)
540*e57a022bSJustin T. Gibbs 			cbr->cbr_func(cbr->cbr_arg, value);
541bb0ade09Sahrens 
542*e57a022bSJustin T. Gibbs 		dsl_dataset_rele(cbr->cbr_ds, FTAG);
543fa9e4066Sahrens 	}
544fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
545fa9e4066Sahrens 
546d8d77200Sahrens 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
5471d452cf5Sahrens 	for (zap_cursor_init(&zc, mos,
548c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj);
549d8d77200Sahrens 	    zap_cursor_retrieve(&zc, za) == 0;
5501d452cf5Sahrens 	    zap_cursor_advance(&zc)) {
551d8d77200Sahrens 		dsl_prop_changed_notify(dp, za->za_first_integer,
5521d452cf5Sahrens 		    propname, value, FALSE);
553fa9e4066Sahrens 	}
554d8d77200Sahrens 	kmem_free(za, sizeof (zap_attribute_t));
5551d452cf5Sahrens 	zap_cursor_fini(&zc);
5563b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
557fa9e4066Sahrens }
558fa9e4066Sahrens 
55992241e0bSTom Erickson void
5603b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname,
5613b2aab18SMatthew Ahrens     zprop_source_t source, int intsz, int numints, const void *value,
5623b2aab18SMatthew Ahrens     dmu_tx_t *tx)
563fa9e4066Sahrens {
564bb0ade09Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
56592241e0bSTom Erickson 	uint64_t zapobj, intval, dummy;
5661d452cf5Sahrens 	int isint;
567ecd6cf80Smarks 	char valbuf[32];
5683b2aab18SMatthew Ahrens 	const char *valstr = NULL;
56992241e0bSTom Erickson 	char *inheritstr;
57092241e0bSTom Erickson 	char *recvdstr;
57192241e0bSTom Erickson 	char *tbuf = NULL;
57292241e0bSTom Erickson 	int err;
57392241e0bSTom Erickson 	uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa);
574fa9e4066Sahrens 
57592241e0bSTom Erickson 	isint = (dodefault(propname, 8, 1, &intval) == 0);
576fa9e4066Sahrens 
577bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
57892241e0bSTom Erickson 		ASSERT(version >= SPA_VERSION_SNAP_PROPS);
579c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_props_obj == 0) {
580bb0ade09Sahrens 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
581c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds)->ds_props_obj =
582bb0ade09Sahrens 			    zap_create(mos,
583bb0ade09Sahrens 			    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
584bb0ade09Sahrens 		}
585c1379625SJustin T. Gibbs 		zapobj = dsl_dataset_phys(ds)->ds_props_obj;
586bb0ade09Sahrens 	} else {
587c1379625SJustin T. Gibbs 		zapobj = dsl_dir_phys(ds->ds_dir)->dd_props_zapobj;
588bb0ade09Sahrens 	}
589bb0ade09Sahrens 
59092241e0bSTom Erickson 	if (version < SPA_VERSION_RECVD_PROPS) {
59192241e0bSTom Erickson 		if (source & ZPROP_SRC_NONE)
59292241e0bSTom Erickson 			source = ZPROP_SRC_NONE;
59392241e0bSTom Erickson 		else if (source & ZPROP_SRC_RECEIVED)
59492241e0bSTom Erickson 			source = ZPROP_SRC_LOCAL;
59592241e0bSTom Erickson 	}
59692241e0bSTom Erickson 
59792241e0bSTom Erickson 	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
59892241e0bSTom Erickson 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
59992241e0bSTom Erickson 
60092241e0bSTom Erickson 	switch (source) {
60192241e0bSTom Erickson 	case ZPROP_SRC_NONE:
60292241e0bSTom Erickson 		/*
60392241e0bSTom Erickson 		 * revert to received value, if any (inherit -S)
60492241e0bSTom Erickson 		 * - remove propname
60592241e0bSTom Erickson 		 * - remove propname$inherit
60692241e0bSTom Erickson 		 */
60792241e0bSTom Erickson 		err = zap_remove(mos, zapobj, propname, tx);
60892241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
60992241e0bSTom Erickson 		err = zap_remove(mos, zapobj, inheritstr, tx);
61092241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
61192241e0bSTom Erickson 		break;
61292241e0bSTom Erickson 	case ZPROP_SRC_LOCAL:
61392241e0bSTom Erickson 		/*
61492241e0bSTom Erickson 		 * remove propname$inherit
61592241e0bSTom Erickson 		 * set propname -> value
61692241e0bSTom Erickson 		 */
61792241e0bSTom Erickson 		err = zap_remove(mos, zapobj, inheritstr, tx);
61892241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
6193b2aab18SMatthew Ahrens 		VERIFY0(zap_update(mos, zapobj, propname,
6203b2aab18SMatthew Ahrens 		    intsz, numints, value, tx));
62192241e0bSTom Erickson 		break;
62292241e0bSTom Erickson 	case ZPROP_SRC_INHERITED:
62392241e0bSTom Erickson 		/*
62492241e0bSTom Erickson 		 * explicitly inherit
62592241e0bSTom Erickson 		 * - remove propname
62692241e0bSTom Erickson 		 * - set propname$inherit
62792241e0bSTom Erickson 		 */
62892241e0bSTom Erickson 		err = zap_remove(mos, zapobj, propname, tx);
6291d452cf5Sahrens 		ASSERT(err == 0 || err == ENOENT);
63092241e0bSTom Erickson 		if (version >= SPA_VERSION_RECVD_PROPS &&
6313b2aab18SMatthew Ahrens 		    dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) {
63292241e0bSTom Erickson 			dummy = 0;
6333b2aab18SMatthew Ahrens 			VERIFY0(zap_update(mos, zapobj, inheritstr,
6343b2aab18SMatthew Ahrens 			    8, 1, &dummy, tx));
635fa9e4066Sahrens 		}
63692241e0bSTom Erickson 		break;
63792241e0bSTom Erickson 	case ZPROP_SRC_RECEIVED:
63892241e0bSTom Erickson 		/*
63992241e0bSTom Erickson 		 * set propname$recvd -> value
64092241e0bSTom Erickson 		 */
64192241e0bSTom Erickson 		err = zap_update(mos, zapobj, recvdstr,
6423b2aab18SMatthew Ahrens 		    intsz, numints, value, tx);
64392241e0bSTom Erickson 		ASSERT(err == 0);
64492241e0bSTom Erickson 		break;
64592241e0bSTom Erickson 	case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED):
64692241e0bSTom Erickson 		/*
64792241e0bSTom Erickson 		 * clear local and received settings
64892241e0bSTom Erickson 		 * - remove propname
64992241e0bSTom Erickson 		 * - remove propname$inherit
65092241e0bSTom Erickson 		 * - remove propname$recvd
65192241e0bSTom Erickson 		 */
65292241e0bSTom Erickson 		err = zap_remove(mos, zapobj, propname, tx);
65392241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
65492241e0bSTom Erickson 		err = zap_remove(mos, zapobj, inheritstr, tx);
65592241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
65692241e0bSTom Erickson 		/* FALLTHRU */
65792241e0bSTom Erickson 	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
65892241e0bSTom Erickson 		/*
65992241e0bSTom Erickson 		 * remove propname$recvd
66092241e0bSTom Erickson 		 */
66192241e0bSTom Erickson 		err = zap_remove(mos, zapobj, recvdstr, tx);
66292241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
66392241e0bSTom Erickson 		break;
66492241e0bSTom Erickson 	default:
66592241e0bSTom Erickson 		cmn_err(CE_PANIC, "unexpected property source: %d", source);
666fa9e4066Sahrens 	}
667fa9e4066Sahrens 
66892241e0bSTom Erickson 	strfree(inheritstr);
66992241e0bSTom Erickson 	strfree(recvdstr);
67092241e0bSTom Erickson 
6711d452cf5Sahrens 	if (isint) {
6723b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval));
67392241e0bSTom Erickson 
674bc9014e6SJustin Gibbs 		if (ds->ds_is_snapshot) {
675bb0ade09Sahrens 			dsl_prop_cb_record_t *cbr;
676bb0ade09Sahrens 			/*
677bb0ade09Sahrens 			 * It's a snapshot; nothing can inherit this
678bb0ade09Sahrens 			 * property, so just look for callbacks on this
679bb0ade09Sahrens 			 * ds here.
680bb0ade09Sahrens 			 */
681bb0ade09Sahrens 			mutex_enter(&ds->ds_dir->dd_lock);
682bb0ade09Sahrens 			for (cbr = list_head(&ds->ds_dir->dd_prop_cbs); cbr;
683bb0ade09Sahrens 			    cbr = list_next(&ds->ds_dir->dd_prop_cbs, cbr)) {
684bb0ade09Sahrens 				if (cbr->cbr_ds == ds &&
68592241e0bSTom Erickson 				    strcmp(cbr->cbr_propname, propname) == 0)
686bb0ade09Sahrens 					cbr->cbr_func(cbr->cbr_arg, intval);
687bb0ade09Sahrens 			}
688bb0ade09Sahrens 			mutex_exit(&ds->ds_dir->dd_lock);
689bb0ade09Sahrens 		} else {
690bb0ade09Sahrens 			dsl_prop_changed_notify(ds->ds_dir->dd_pool,
69192241e0bSTom Erickson 			    ds->ds_dir->dd_object, propname, intval, TRUE);
692bb0ade09Sahrens 		}
69392241e0bSTom Erickson 
694ecd6cf80Smarks 		(void) snprintf(valbuf, sizeof (valbuf),
695ecd6cf80Smarks 		    "%lld", (longlong_t)intval);
696ecd6cf80Smarks 		valstr = valbuf;
697ecd6cf80Smarks 	} else {
69892241e0bSTom Erickson 		if (source == ZPROP_SRC_LOCAL) {
6993b2aab18SMatthew Ahrens 			valstr = value;
70092241e0bSTom Erickson 		} else {
70192241e0bSTom Erickson 			tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
70292241e0bSTom Erickson 			if (dsl_prop_get_ds(ds, propname, 1,
70392241e0bSTom Erickson 			    ZAP_MAXVALUELEN, tbuf, NULL) == 0)
70492241e0bSTom Erickson 				valstr = tbuf;
70592241e0bSTom Erickson 		}
706ecd6cf80Smarks 	}
70792241e0bSTom Erickson 
7084445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE ||
7094445fffbSMatthew Ahrens 	    source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx,
7104445fffbSMatthew Ahrens 	    "%s=%s", propname, (valstr == NULL ? "" : valstr));
71192241e0bSTom Erickson 
71292241e0bSTom Erickson 	if (tbuf != NULL)
71392241e0bSTom Erickson 		kmem_free(tbuf, ZAP_MAXVALUELEN);
714fa9e4066Sahrens }
715fa9e4066Sahrens 
7163b2aab18SMatthew Ahrens int
7173b2aab18SMatthew Ahrens dsl_prop_set_int(const char *dsname, const char *propname,
7183b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value)
7195c0b6a79SRich Morris {
7203b2aab18SMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
7213b2aab18SMatthew Ahrens 	int error;
72292241e0bSTom Erickson 
7233b2aab18SMatthew Ahrens 	fnvlist_add_uint64(nvl, propname, value);
7243b2aab18SMatthew Ahrens 	error = dsl_props_set(dsname, source, nvl);
7253b2aab18SMatthew Ahrens 	fnvlist_free(nvl);
7263b2aab18SMatthew Ahrens 	return (error);
7275c0b6a79SRich Morris }
7285c0b6a79SRich Morris 
729a2eea2e1Sahrens int
7303b2aab18SMatthew Ahrens dsl_prop_set_string(const char *dsname, const char *propname,
7313b2aab18SMatthew Ahrens     zprop_source_t source, const char *value)
732fa9e4066Sahrens {
7333b2aab18SMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
7343b2aab18SMatthew Ahrens 	int error;
735bb0ade09Sahrens 
7363b2aab18SMatthew Ahrens 	fnvlist_add_string(nvl, propname, value);
7373b2aab18SMatthew Ahrens 	error = dsl_props_set(dsname, source, nvl);
7383b2aab18SMatthew Ahrens 	fnvlist_free(nvl);
7393b2aab18SMatthew Ahrens 	return (error);
7403b2aab18SMatthew Ahrens }
74192241e0bSTom Erickson 
7423b2aab18SMatthew Ahrens int
7433b2aab18SMatthew Ahrens dsl_prop_inherit(const char *dsname, const char *propname,
7443b2aab18SMatthew Ahrens     zprop_source_t source)
7453b2aab18SMatthew Ahrens {
7463b2aab18SMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
7473b2aab18SMatthew Ahrens 	int error;
748bb0ade09Sahrens 
7493b2aab18SMatthew Ahrens 	fnvlist_add_boolean(nvl, propname);
7503b2aab18SMatthew Ahrens 	error = dsl_props_set(dsname, source, nvl);
7513b2aab18SMatthew Ahrens 	fnvlist_free(nvl);
7523b2aab18SMatthew Ahrens 	return (error);
753fa9e4066Sahrens }
7547f7322feSeschrock 
7553b2aab18SMatthew Ahrens typedef struct dsl_props_set_arg {
7563b2aab18SMatthew Ahrens 	const char *dpsa_dsname;
7573b2aab18SMatthew Ahrens 	zprop_source_t dpsa_source;
7583b2aab18SMatthew Ahrens 	nvlist_t *dpsa_props;
7593b2aab18SMatthew Ahrens } dsl_props_set_arg_t;
7603b2aab18SMatthew Ahrens 
7613b2aab18SMatthew Ahrens static int
7623b2aab18SMatthew Ahrens dsl_props_set_check(void *arg, dmu_tx_t *tx)
7635c0b6a79SRich Morris {
7643b2aab18SMatthew Ahrens 	dsl_props_set_arg_t *dpsa = arg;
7653b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
7665c0b6a79SRich Morris 	dsl_dataset_t *ds;
767478ed9adSEric Taylor 	uint64_t version;
7685c0b6a79SRich Morris 	nvpair_t *elem = NULL;
7695c0b6a79SRich Morris 	int err;
7705c0b6a79SRich Morris 
7713b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds);
7723b2aab18SMatthew Ahrens 	if (err != 0)
773478ed9adSEric Taylor 		return (err);
7743b2aab18SMatthew Ahrens 
775478ed9adSEric Taylor 	version = spa_version(ds->ds_dir->dd_pool->dp_spa);
7763b2aab18SMatthew Ahrens 	while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) {
777478ed9adSEric Taylor 		if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
778478ed9adSEric Taylor 			dsl_dataset_rele(ds, FTAG);
779be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
780478ed9adSEric Taylor 		}
7815c0b6a79SRich Morris 		if (nvpair_type(elem) == DATA_TYPE_STRING) {
7823b2aab18SMatthew Ahrens 			char *valstr = fnvpair_value_string(elem);
783478ed9adSEric Taylor 			if (strlen(valstr) >= (version <
784478ed9adSEric Taylor 			    SPA_VERSION_STMF_PROP ?
785478ed9adSEric Taylor 			    ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) {
786478ed9adSEric Taylor 				dsl_dataset_rele(ds, FTAG);
787ccba0801SRich Morris 				return (E2BIG);
788478ed9adSEric Taylor 			}
7895c0b6a79SRich Morris 		}
7905c0b6a79SRich Morris 	}
7915c0b6a79SRich Morris 
792bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot && version < SPA_VERSION_SNAP_PROPS) {
7935c0b6a79SRich Morris 		dsl_dataset_rele(ds, FTAG);
794be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
7955c0b6a79SRich Morris 	}
7963b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
7973b2aab18SMatthew Ahrens 	return (0);
7983b2aab18SMatthew Ahrens }
7995c0b6a79SRich Morris 
8003b2aab18SMatthew Ahrens void
8013b2aab18SMatthew Ahrens dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source,
8023b2aab18SMatthew Ahrens     nvlist_t *props, dmu_tx_t *tx)
8033b2aab18SMatthew Ahrens {
8043b2aab18SMatthew Ahrens 	nvpair_t *elem = NULL;
80592241e0bSTom Erickson 
8063b2aab18SMatthew Ahrens 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
8073b2aab18SMatthew Ahrens 		nvpair_t *pair = elem;
8085c0b6a79SRich Morris 
8093b2aab18SMatthew Ahrens 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
8103b2aab18SMatthew Ahrens 			/*
8113b2aab18SMatthew Ahrens 			 * dsl_prop_get_all_impl() returns properties in this
8123b2aab18SMatthew Ahrens 			 * format.
8133b2aab18SMatthew Ahrens 			 */
8143b2aab18SMatthew Ahrens 			nvlist_t *attrs = fnvpair_value_nvlist(pair);
8153b2aab18SMatthew Ahrens 			pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
8163b2aab18SMatthew Ahrens 		}
8173b2aab18SMatthew Ahrens 
8183b2aab18SMatthew Ahrens 		if (nvpair_type(pair) == DATA_TYPE_STRING) {
8193b2aab18SMatthew Ahrens 			const char *value = fnvpair_value_string(pair);
8203b2aab18SMatthew Ahrens 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
8213b2aab18SMatthew Ahrens 			    source, 1, strlen(value) + 1, value, tx);
8223b2aab18SMatthew Ahrens 		} else if (nvpair_type(pair) == DATA_TYPE_UINT64) {
8233b2aab18SMatthew Ahrens 			uint64_t intval = fnvpair_value_uint64(pair);
8243b2aab18SMatthew Ahrens 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
8253b2aab18SMatthew Ahrens 			    source, sizeof (intval), 1, &intval, tx);
8263b2aab18SMatthew Ahrens 		} else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) {
8273b2aab18SMatthew Ahrens 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
8283b2aab18SMatthew Ahrens 			    source, 0, 0, NULL, tx);
8293b2aab18SMatthew Ahrens 		} else {
8303b2aab18SMatthew Ahrens 			panic("invalid nvpair type");
8313b2aab18SMatthew Ahrens 		}
8323b2aab18SMatthew Ahrens 	}
8333b2aab18SMatthew Ahrens }
8343b2aab18SMatthew Ahrens 
8353b2aab18SMatthew Ahrens static void
8363b2aab18SMatthew Ahrens dsl_props_set_sync(void *arg, dmu_tx_t *tx)
8373b2aab18SMatthew Ahrens {
8383b2aab18SMatthew Ahrens 	dsl_props_set_arg_t *dpsa = arg;
8393b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8403b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
8413b2aab18SMatthew Ahrens 
8423b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds));
8433b2aab18SMatthew Ahrens 	dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx);
8445c0b6a79SRich Morris 	dsl_dataset_rele(ds, FTAG);
8453b2aab18SMatthew Ahrens }
8463b2aab18SMatthew Ahrens 
8473b2aab18SMatthew Ahrens /*
8483b2aab18SMatthew Ahrens  * All-or-nothing; if any prop can't be set, nothing will be modified.
8493b2aab18SMatthew Ahrens  */
8503b2aab18SMatthew Ahrens int
8513b2aab18SMatthew Ahrens dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props)
8523b2aab18SMatthew Ahrens {
8533b2aab18SMatthew Ahrens 	dsl_props_set_arg_t dpsa;
8543b2aab18SMatthew Ahrens 	int nblks = 0;
8553b2aab18SMatthew Ahrens 
8563b2aab18SMatthew Ahrens 	dpsa.dpsa_dsname = dsname;
8573b2aab18SMatthew Ahrens 	dpsa.dpsa_source = source;
8583b2aab18SMatthew Ahrens 	dpsa.dpsa_props = props;
8593b2aab18SMatthew Ahrens 
8603b2aab18SMatthew Ahrens 	/*
8613b2aab18SMatthew Ahrens 	 * If the source includes NONE, then we will only be removing entries
8623b2aab18SMatthew Ahrens 	 * from the ZAP object.  In that case don't check for ENOSPC.
8633b2aab18SMatthew Ahrens 	 */
8643b2aab18SMatthew Ahrens 	if ((source & ZPROP_SRC_NONE) == 0)
8653b2aab18SMatthew Ahrens 		nblks = 2 * fnvlist_num_pairs(props);
8663b2aab18SMatthew Ahrens 
8673b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync,
8687d46dc6cSMatthew Ahrens 	    &dpsa, nblks, ZFS_SPACE_CHECK_RESERVED));
8695c0b6a79SRich Morris }
8705c0b6a79SRich Morris 
87192241e0bSTom Erickson typedef enum dsl_prop_getflags {
87292241e0bSTom Erickson 	DSL_PROP_GET_INHERITING = 0x1,	/* searching parent of target ds */
87392241e0bSTom Erickson 	DSL_PROP_GET_SNAPSHOT = 0x2,	/* snapshot dataset */
87492241e0bSTom Erickson 	DSL_PROP_GET_LOCAL = 0x4,	/* local properties */
87592241e0bSTom Erickson 	DSL_PROP_GET_RECEIVED = 0x8	/* received properties */
87692241e0bSTom Erickson } dsl_prop_getflags_t;
87792241e0bSTom Erickson 
87892241e0bSTom Erickson static int
87992241e0bSTom Erickson dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj,
88092241e0bSTom Erickson     const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv)
88192241e0bSTom Erickson {
88292241e0bSTom Erickson 	zap_cursor_t zc;
88392241e0bSTom Erickson 	zap_attribute_t za;
88492241e0bSTom Erickson 	int err = 0;
88592241e0bSTom Erickson 
88692241e0bSTom Erickson 	for (zap_cursor_init(&zc, mos, propobj);
88792241e0bSTom Erickson 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
88892241e0bSTom Erickson 	    zap_cursor_advance(&zc)) {
88992241e0bSTom Erickson 		nvlist_t *propval;
89092241e0bSTom Erickson 		zfs_prop_t prop;
89192241e0bSTom Erickson 		char buf[ZAP_MAXNAMELEN];
89292241e0bSTom Erickson 		char *valstr;
89392241e0bSTom Erickson 		const char *suffix;
89492241e0bSTom Erickson 		const char *propname;
89592241e0bSTom Erickson 		const char *source;
89692241e0bSTom Erickson 
89792241e0bSTom Erickson 		suffix = strchr(za.za_name, '$');
89892241e0bSTom Erickson 
89992241e0bSTom Erickson 		if (suffix == NULL) {
90092241e0bSTom Erickson 			/*
90192241e0bSTom Erickson 			 * Skip local properties if we only want received
90292241e0bSTom Erickson 			 * properties.
90392241e0bSTom Erickson 			 */
90492241e0bSTom Erickson 			if (flags & DSL_PROP_GET_RECEIVED)
90592241e0bSTom Erickson 				continue;
90692241e0bSTom Erickson 
90792241e0bSTom Erickson 			propname = za.za_name;
90892241e0bSTom Erickson 			source = setpoint;
90992241e0bSTom Erickson 		} else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) {
91092241e0bSTom Erickson 			/* Skip explicitly inherited entries. */
91192241e0bSTom Erickson 			continue;
91292241e0bSTom Erickson 		} else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) {
91392241e0bSTom Erickson 			if (flags & DSL_PROP_GET_LOCAL)
91492241e0bSTom Erickson 				continue;
91592241e0bSTom Erickson 
91692241e0bSTom Erickson 			(void) strncpy(buf, za.za_name, (suffix - za.za_name));
91792241e0bSTom Erickson 			buf[suffix - za.za_name] = '\0';
91892241e0bSTom Erickson 			propname = buf;
91992241e0bSTom Erickson 
92092241e0bSTom Erickson 			if (!(flags & DSL_PROP_GET_RECEIVED)) {
92192241e0bSTom Erickson 				/* Skip if locally overridden. */
92292241e0bSTom Erickson 				err = zap_contains(mos, propobj, propname);
92392241e0bSTom Erickson 				if (err == 0)
92492241e0bSTom Erickson 					continue;
92592241e0bSTom Erickson 				if (err != ENOENT)
92692241e0bSTom Erickson 					break;
92792241e0bSTom Erickson 
92892241e0bSTom Erickson 				/* Skip if explicitly inherited. */
92992241e0bSTom Erickson 				valstr = kmem_asprintf("%s%s", propname,
93092241e0bSTom Erickson 				    ZPROP_INHERIT_SUFFIX);
93192241e0bSTom Erickson 				err = zap_contains(mos, propobj, valstr);
93292241e0bSTom Erickson 				strfree(valstr);
93392241e0bSTom Erickson 				if (err == 0)
93492241e0bSTom Erickson 					continue;
93592241e0bSTom Erickson 				if (err != ENOENT)
93692241e0bSTom Erickson 					break;
93792241e0bSTom Erickson 			}
93892241e0bSTom Erickson 
93992241e0bSTom Erickson 			source = ((flags & DSL_PROP_GET_INHERITING) ?
94092241e0bSTom Erickson 			    setpoint : ZPROP_SOURCE_VAL_RECVD);
94192241e0bSTom Erickson 		} else {
94292241e0bSTom Erickson 			/*
94392241e0bSTom Erickson 			 * For backward compatibility, skip suffixes we don't
94492241e0bSTom Erickson 			 * recognize.
94592241e0bSTom Erickson 			 */
94692241e0bSTom Erickson 			continue;
94792241e0bSTom Erickson 		}
94892241e0bSTom Erickson 
94992241e0bSTom Erickson 		prop = zfs_name_to_prop(propname);
95092241e0bSTom Erickson 
95192241e0bSTom Erickson 		/* Skip non-inheritable properties. */
95292241e0bSTom Erickson 		if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL &&
95392241e0bSTom Erickson 		    !zfs_prop_inheritable(prop))
95492241e0bSTom Erickson 			continue;
95592241e0bSTom Erickson 
95692241e0bSTom Erickson 		/* Skip properties not valid for this type. */
95792241e0bSTom Erickson 		if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL &&
95892241e0bSTom Erickson 		    !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
95992241e0bSTom Erickson 			continue;
96092241e0bSTom Erickson 
96192241e0bSTom Erickson 		/* Skip properties already defined. */
96292241e0bSTom Erickson 		if (nvlist_exists(nv, propname))
96392241e0bSTom Erickson 			continue;
96492241e0bSTom Erickson 
96592241e0bSTom Erickson 		VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
96692241e0bSTom Erickson 		if (za.za_integer_length == 1) {
96792241e0bSTom Erickson 			/*
96892241e0bSTom Erickson 			 * String property
96992241e0bSTom Erickson 			 */
97092241e0bSTom Erickson 			char *tmp = kmem_alloc(za.za_num_integers,
97192241e0bSTom Erickson 			    KM_SLEEP);
97292241e0bSTom Erickson 			err = zap_lookup(mos, propobj,
97392241e0bSTom Erickson 			    za.za_name, 1, za.za_num_integers, tmp);
97492241e0bSTom Erickson 			if (err != 0) {
97592241e0bSTom Erickson 				kmem_free(tmp, za.za_num_integers);
97692241e0bSTom Erickson 				break;
97792241e0bSTom Erickson 			}
97892241e0bSTom Erickson 			VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
97992241e0bSTom Erickson 			    tmp) == 0);
98092241e0bSTom Erickson 			kmem_free(tmp, za.za_num_integers);
98192241e0bSTom Erickson 		} else {
98292241e0bSTom Erickson 			/*
98392241e0bSTom Erickson 			 * Integer property
98492241e0bSTom Erickson 			 */
98592241e0bSTom Erickson 			ASSERT(za.za_integer_length == 8);
98692241e0bSTom Erickson 			(void) nvlist_add_uint64(propval, ZPROP_VALUE,
98792241e0bSTom Erickson 			    za.za_first_integer);
98892241e0bSTom Erickson 		}
98992241e0bSTom Erickson 
99092241e0bSTom Erickson 		VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0);
99192241e0bSTom Erickson 		VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
99292241e0bSTom Erickson 		nvlist_free(propval);
99392241e0bSTom Erickson 	}
99492241e0bSTom Erickson 	zap_cursor_fini(&zc);
99592241e0bSTom Erickson 	if (err == ENOENT)
99692241e0bSTom Erickson 		err = 0;
99792241e0bSTom Erickson 	return (err);
99892241e0bSTom Erickson }
99992241e0bSTom Erickson 
10007f7322feSeschrock /*
10017f7322feSeschrock  * Iterate over all properties for this dataset and return them in an nvlist.
10027f7322feSeschrock  */
100392241e0bSTom Erickson static int
100492241e0bSTom Erickson dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp,
100592241e0bSTom Erickson     dsl_prop_getflags_t flags)
10067f7322feSeschrock {
100799653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
1008bb0ade09Sahrens 	dsl_pool_t *dp = dd->dd_pool;
1009bb0ade09Sahrens 	objset_t *mos = dp->dp_meta_objset;
101092241e0bSTom Erickson 	int err = 0;
101192241e0bSTom Erickson 	char setpoint[MAXNAMELEN];
10127f7322feSeschrock 
10137f7322feSeschrock 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10147f7322feSeschrock 
1015bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot)
101692241e0bSTom Erickson 		flags |= DSL_PROP_GET_SNAPSHOT;
10177f7322feSeschrock 
10183b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
101992241e0bSTom Erickson 
1020c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_props_obj != 0) {
102192241e0bSTom Erickson 		ASSERT(flags & DSL_PROP_GET_SNAPSHOT);
102292241e0bSTom Erickson 		dsl_dataset_name(ds, setpoint);
1023c1379625SJustin T. Gibbs 		err = dsl_prop_get_all_impl(mos,
1024c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_props_obj, setpoint, flags, *nvp);
102592241e0bSTom Erickson 		if (err)
102692241e0bSTom Erickson 			goto out;
102792241e0bSTom Erickson 	}
102892241e0bSTom Erickson 
102992241e0bSTom Erickson 	for (; dd != NULL; dd = dd->dd_parent) {
103092241e0bSTom Erickson 		if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) {
103192241e0bSTom Erickson 			if (flags & (DSL_PROP_GET_LOCAL |
103292241e0bSTom Erickson 			    DSL_PROP_GET_RECEIVED))
103392241e0bSTom Erickson 				break;
103492241e0bSTom Erickson 			flags |= DSL_PROP_GET_INHERITING;
1035bb0ade09Sahrens 		}
103692241e0bSTom Erickson 		dsl_dir_name(dd, setpoint);
1037c1379625SJustin T. Gibbs 		err = dsl_prop_get_all_impl(mos,
1038c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_props_zapobj, setpoint, flags, *nvp);
103992241e0bSTom Erickson 		if (err)
104092241e0bSTom Erickson 			break;
104192241e0bSTom Erickson 	}
104292241e0bSTom Erickson out:
104392241e0bSTom Erickson 	return (err);
104492241e0bSTom Erickson }
1045a2eea2e1Sahrens 
104692241e0bSTom Erickson boolean_t
10473b2aab18SMatthew Ahrens dsl_prop_get_hasrecvd(const char *dsname)
104892241e0bSTom Erickson {
104992241e0bSTom Erickson 	uint64_t dummy;
1050bb0ade09Sahrens 
10513b2aab18SMatthew Ahrens 	return (0 ==
10523b2aab18SMatthew Ahrens 	    dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL));
105392241e0bSTom Erickson }
1054e9dbad6fSeschrock 
10553b2aab18SMatthew Ahrens static int
10563b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source)
105792241e0bSTom Erickson {
10583b2aab18SMatthew Ahrens 	uint64_t version;
10593b2aab18SMatthew Ahrens 	spa_t *spa;
10603b2aab18SMatthew Ahrens 	int error = 0;
10617f7322feSeschrock 
10623b2aab18SMatthew Ahrens 	VERIFY0(spa_open(dsname, &spa, FTAG));
10633b2aab18SMatthew Ahrens 	version = spa_version(spa);
10643b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
10657f7322feSeschrock 
10663b2aab18SMatthew Ahrens 	if (version >= SPA_VERSION_RECVD_PROPS)
10673b2aab18SMatthew Ahrens 		error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0);
10683b2aab18SMatthew Ahrens 	return (error);
106992241e0bSTom Erickson }
10707f7322feSeschrock 
107192241e0bSTom Erickson /*
107292241e0bSTom Erickson  * Call after successfully receiving properties to ensure that only the first
107392241e0bSTom Erickson  * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
107492241e0bSTom Erickson  */
10753b2aab18SMatthew Ahrens int
10763b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd(const char *dsname)
107792241e0bSTom Erickson {
10783b2aab18SMatthew Ahrens 	int error = 0;
10793b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(dsname))
10803b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL);
10813b2aab18SMatthew Ahrens 	return (error);
108292241e0bSTom Erickson }
10837f7322feSeschrock 
108492241e0bSTom Erickson void
10853b2aab18SMatthew Ahrens dsl_prop_unset_hasrecvd(const char *dsname)
108692241e0bSTom Erickson {
10873b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE));
108892241e0bSTom Erickson }
108992241e0bSTom Erickson 
109092241e0bSTom Erickson int
109192241e0bSTom Erickson dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
109292241e0bSTom Erickson {
109392241e0bSTom Erickson 	return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0));
109492241e0bSTom Erickson }
109592241e0bSTom Erickson 
109692241e0bSTom Erickson int
10973b2aab18SMatthew Ahrens dsl_prop_get_received(const char *dsname, nvlist_t **nvp)
109892241e0bSTom Erickson {
10993b2aab18SMatthew Ahrens 	objset_t *os;
11003b2aab18SMatthew Ahrens 	int error;
11013b2aab18SMatthew Ahrens 
110292241e0bSTom Erickson 	/*
110392241e0bSTom Erickson 	 * Received properties are not distinguishable from local properties
110492241e0bSTom Erickson 	 * until the dataset has received properties on or after
110592241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
110692241e0bSTom Erickson 	 */
11073b2aab18SMatthew Ahrens 	dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ?
110892241e0bSTom Erickson 	    DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL);
11093b2aab18SMatthew Ahrens 
11103b2aab18SMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
11113b2aab18SMatthew Ahrens 	if (error != 0)
11123b2aab18SMatthew Ahrens 		return (error);
11133b2aab18SMatthew Ahrens 	error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags);
11143b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
11153b2aab18SMatthew Ahrens 	return (error);
11167f7322feSeschrock }
1117a2eea2e1Sahrens 
1118a2eea2e1Sahrens void
1119a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
1120a2eea2e1Sahrens {
1121a2eea2e1Sahrens 	nvlist_t *propval;
112292241e0bSTom Erickson 	const char *propname = zfs_prop_to_name(prop);
112392241e0bSTom Erickson 	uint64_t default_value;
112492241e0bSTom Erickson 
112592241e0bSTom Erickson 	if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
112692241e0bSTom Erickson 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
112792241e0bSTom Erickson 		return;
112892241e0bSTom Erickson 	}
1129a2eea2e1Sahrens 
1130a2eea2e1Sahrens 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1131990b4856Slling 	VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
113292241e0bSTom Erickson 	/* Indicate the default source if we can. */
113392241e0bSTom Erickson 	if (dodefault(propname, 8, 1, &default_value) == 0 &&
113492241e0bSTom Erickson 	    value == default_value) {
113592241e0bSTom Erickson 		VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0);
113692241e0bSTom Erickson 	}
113792241e0bSTom Erickson 	VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1138a2eea2e1Sahrens 	nvlist_free(propval);
1139a2eea2e1Sahrens }
1140a2eea2e1Sahrens 
1141a2eea2e1Sahrens void
1142a2eea2e1Sahrens dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
1143a2eea2e1Sahrens {
1144a2eea2e1Sahrens 	nvlist_t *propval;
114592241e0bSTom Erickson 	const char *propname = zfs_prop_to_name(prop);
114692241e0bSTom Erickson 
114792241e0bSTom Erickson 	if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
114892241e0bSTom Erickson 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
114992241e0bSTom Erickson 		return;
115092241e0bSTom Erickson 	}
1151a2eea2e1Sahrens 
1152a2eea2e1Sahrens 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1153990b4856Slling 	VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
115492241e0bSTom Erickson 	VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1155a2eea2e1Sahrens 	nvlist_free(propval);
1156a2eea2e1Sahrens }
1157