xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_prop.c (revision 2d85dedb)
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.
239adfa60dSMatthew Ahrens  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
25*2d85dedbSJason King  * Copyright 2019 Joyent, Inc.
26fa9e4066Sahrens  */
2892241e0bSTom Erickson #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/dmu.h>
307f7322feSeschrock #include <sys/dmu_objset.h>
31fa9e4066Sahrens #include <sys/dmu_tx.h>
32fa9e4066Sahrens #include <sys/dsl_dataset.h>
33fa9e4066Sahrens #include <sys/dsl_dir.h>
34fa9e4066Sahrens #include <sys/dsl_prop.h>
351d452cf5Sahrens #include <sys/dsl_synctask.h>
36fa9e4066Sahrens #include <sys/spa.h>
37fa9e4066Sahrens #include <sys/zap.h>
38fa9e4066Sahrens #include <sys/fs/zfs.h>
40fa9e4066Sahrens #include "zfs_prop.h"
4292241e0bSTom Erickson #define	ZPROP_INHERIT_SUFFIX "$inherit"
4392241e0bSTom Erickson #define	ZPROP_RECVD_SUFFIX "$recvd"
4492241e0bSTom Erickson 
45fa9e4066Sahrens static int
dodefault(zfs_prop_t prop,int intsz,int numints,void * buf)46d09e4475SAlex Wilson dodefault(zfs_prop_t prop, int intsz, int numints, void *buf)
47fa9e4066Sahrens {
48da6c28aaSamw 	/*
49da6c28aaSamw 	 * The setonce properties are read-only, BUT they still
50da6c28aaSamw 	 * have a default value that can be used as the initial
51da6c28aaSamw 	 * value.
52da6c28aaSamw 	 */
53d09e4475SAlex Wilson 	if (prop == ZPROP_INVAL ||
54da6c28aaSamw 	    (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
55be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
5791ebeef5Sahrens 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
58fa9e4066Sahrens 		if (intsz != 1)
59be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
60990b4856Slling 		(void) strncpy(buf, zfs_prop_default_string(prop),
6192241e0bSTom Erickson 		    numints);
62fa9e4066Sahrens 	} else {
6392241e0bSTom Erickson 		if (intsz != 8 || numints < 1)
64be6fd75aSMatthew Ahrens 			return (SET_ERROR(EOVERFLOW));
66fa9e4066Sahrens 		*(uint64_t *)buf = zfs_prop_default_numeric(prop);
67fa9e4066Sahrens 	}
69fa9e4066Sahrens 	return (0);
70fa9e4066Sahrens }
72bb0ade09Sahrens int
dsl_prop_get_dd(dsl_dir_t * dd,const char * propname,int intsz,int numints,void * buf,char * setpoint,boolean_t snapshot)73bb0ade09Sahrens dsl_prop_get_dd(dsl_dir_t *dd, const char *propname,
7492241e0bSTom Erickson     int intsz, int numints, void *buf, char *setpoint, boolean_t snapshot)
75fa9e4066Sahrens {
7699653d4eSeschrock 	int err = ENOENT;
7792241e0bSTom Erickson 	dsl_dir_t *target = dd;
78bb0ade09Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
79e9dbad6fSeschrock 	zfs_prop_t prop;
8092241e0bSTom Erickson 	boolean_t inheritable;
8192241e0bSTom Erickson 	boolean_t inheriting = B_FALSE;
8292241e0bSTom Erickson 	char *inheritstr;
8392241e0bSTom Erickson 	char *recvdstr;
853b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dd->dd_pool));
87fa9e4066Sahrens 	if (setpoint)
88fa9e4066Sahrens 		setpoint[0] = '\0';
90e9dbad6fSeschrock 	prop = zfs_name_to_prop(propname);
9192241e0bSTom Erickson 	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
9292241e0bSTom Erickson 	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
9392241e0bSTom Erickson 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
9599653d4eSeschrock 	/*
9692241e0bSTom Erickson 	 * Note: dd may become NULL, therefore we shouldn't dereference it
9792241e0bSTom Erickson 	 * after this loop.
9899653d4eSeschrock 	 */
9999653d4eSeschrock 	for (; dd != NULL; dd = dd->dd_parent) {
10092241e0bSTom Erickson 		if (dd != target || snapshot) {
10192241e0bSTom Erickson 			if (!inheritable)
10292241e0bSTom Erickson 				break;
10392241e0bSTom Erickson 			inheriting = B_TRUE;
10492241e0bSTom Erickson 		}
10592241e0bSTom Erickson 
10692241e0bSTom Erickson 		/* Check for a local value. */
107c1379625SJustin T. Gibbs 		err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
108c1379625SJustin T. Gibbs 		    propname, intsz, numints, buf);
109fa9e4066Sahrens 		if (err != ENOENT) {
11092241e0bSTom Erickson 			if (setpoint != NULL && err == 0)
111fa9e4066Sahrens 				dsl_dir_name(dd, setpoint);
112fa9e4066Sahrens 			break;
113fa9e4066Sahrens 		}
115e9dbad6fSeschrock 		/*
11692241e0bSTom Erickson 		 * Skip the check for a received value if there is an explicit
11792241e0bSTom Erickson 		 * inheritance entry.
118e9dbad6fSeschrock 		 */
119c1379625SJustin T. Gibbs 		err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
12092241e0bSTom Erickson 		    inheritstr);
12192241e0bSTom Erickson 		if (err != 0 && err != ENOENT)
122e9dbad6fSeschrock 			break;
12392241e0bSTom Erickson 
12492241e0bSTom Erickson 		if (err == ENOENT) {
12592241e0bSTom Erickson 			/* Check for a received value. */
126c1379625SJustin T. Gibbs 			err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
12792241e0bSTom Erickson 			    recvdstr, intsz, numints, buf);
12892241e0bSTom Erickson 			if (err != ENOENT) {
12992241e0bSTom Erickson 				if (setpoint != NULL && err == 0) {
13092241e0bSTom Erickson 					if (inheriting) {
13192241e0bSTom Erickson 						dsl_dir_name(dd, setpoint);
13292241e0bSTom Erickson 					} else {
13392241e0bSTom Erickson 						(void) strcpy(setpoint,
13492241e0bSTom Erickson 						    ZPROP_SOURCE_VAL_RECVD);
13592241e0bSTom Erickson 					}
13692241e0bSTom Erickson 				}
13792241e0bSTom Erickson 				break;
13892241e0bSTom Erickson 			}
13992241e0bSTom Erickson 		}
14092241e0bSTom Erickson 
14192241e0bSTom Erickson 		/*
14292241e0bSTom Erickson 		 * If we found an explicit inheritance entry, err is zero even
14392241e0bSTom Erickson 		 * though we haven't yet found the value, so reinitializing err
14492241e0bSTom Erickson 		 * at the end of the loop (instead of at the beginning) ensures
14592241e0bSTom Erickson 		 * that err has a valid post-loop value.
14692241e0bSTom Erickson 		 */
147be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
148fa9e4066Sahrens 	}
14992241e0bSTom Erickson 
150fa9e4066Sahrens 	if (err == ENOENT)
151d09e4475SAlex Wilson 		err = dodefault(prop, intsz, numints, buf);
15292241e0bSTom Erickson 
15392241e0bSTom Erickson 	strfree(inheritstr);
15492241e0bSTom Erickson 	strfree(recvdstr);
156fa9e4066Sahrens 	return (err);
157fa9e4066Sahrens }
159bb0ade09Sahrens int
dsl_prop_get_ds(dsl_dataset_t * ds,const char * propname,int intsz,int numints,void * buf,char * setpoint)160bb0ade09Sahrens dsl_prop_get_ds(dsl_dataset_t *ds, const char *propname,
16192241e0bSTom Erickson     int intsz, int numints, void *buf, char *setpoint)
162bb0ade09Sahrens {
16392241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
16492241e0bSTom Erickson 	boolean_t inheritable;
16592241e0bSTom Erickson 	uint64_t zapobj;
16692241e0bSTom Erickson 
1673b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
16892241e0bSTom Erickson 	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
169c1379625SJustin T. Gibbs 	zapobj = dsl_dataset_phys(ds)->ds_props_obj;
17092241e0bSTom Erickson 
17192241e0bSTom Erickson 	if (zapobj != 0) {
17292241e0bSTom Erickson 		objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
17392241e0bSTom Erickson 		int err;
175bc9014e6SJustin Gibbs 		ASSERT(ds->ds_is_snapshot);
17692241e0bSTom Erickson 
17792241e0bSTom Erickson 		/* Check for a local value. */
17892241e0bSTom Erickson 		err = zap_lookup(mos, zapobj, propname, intsz, numints, buf);
179bb0ade09Sahrens 		if (err != ENOENT) {
18092241e0bSTom Erickson 			if (setpoint != NULL && err == 0)
181bb0ade09Sahrens 				dsl_dataset_name(ds, setpoint);
182bb0ade09Sahrens 			return (err);
183bb0ade09Sahrens 		}
18492241e0bSTom Erickson 
18592241e0bSTom Erickson 		/*
18692241e0bSTom Erickson 		 * Skip the check for a received value if there is an explicit
18792241e0bSTom Erickson 		 * inheritance entry.
18892241e0bSTom Erickson 		 */
18992241e0bSTom Erickson 		if (inheritable) {
19092241e0bSTom Erickson 			char *inheritstr = kmem_asprintf("%s%s", propname,
19192241e0bSTom Erickson 			    ZPROP_INHERIT_SUFFIX);
19292241e0bSTom Erickson 			err = zap_contains(mos, zapobj, inheritstr);
19392241e0bSTom Erickson 			strfree(inheritstr);
19492241e0bSTom Erickson 			if (err != 0 && err != ENOENT)
19592241e0bSTom Erickson 				return (err);
19692241e0bSTom Erickson 		}
19792241e0bSTom Erickson 
19892241e0bSTom Erickson 		if (err == ENOENT) {
19992241e0bSTom Erickson 			/* Check for a received value. */
20092241e0bSTom Erickson 			char *recvdstr = kmem_asprintf("%s%s", propname,
20192241e0bSTom Erickson 			    ZPROP_RECVD_SUFFIX);
20292241e0bSTom Erickson 			err = zap_lookup(mos, zapobj, recvdstr,
20392241e0bSTom Erickson 			    intsz, numints, buf);
20492241e0bSTom Erickson 			strfree(recvdstr);
20592241e0bSTom Erickson 			if (err != ENOENT) {
20692241e0bSTom Erickson 				if (setpoint != NULL && err == 0)
20792241e0bSTom Erickson 					(void) strcpy(setpoint,
20892241e0bSTom Erickson 					    ZPROP_SOURCE_VAL_RECVD);
20992241e0bSTom Erickson 				return (err);
21092241e0bSTom Erickson 			}
21192241e0bSTom Erickson 		}
212bb0ade09Sahrens 	}
214bb0ade09Sahrens 	return (dsl_prop_get_dd(ds->ds_dir, propname,
215bc9014e6SJustin Gibbs 	    intsz, numints, buf, setpoint, ds->ds_is_snapshot));
216bb0ade09Sahrens }
21803bad06fSJustin Gibbs static dsl_prop_record_t *
dsl_prop_record_find(dsl_dir_t * dd,const char * propname)21903bad06fSJustin Gibbs dsl_prop_record_find(dsl_dir_t *dd, const char *propname)
22003bad06fSJustin Gibbs {
22103bad06fSJustin Gibbs 	dsl_prop_record_t *pr = NULL;
22203bad06fSJustin Gibbs 
22303bad06fSJustin Gibbs 	ASSERT(MUTEX_HELD(&dd->dd_lock));
22403bad06fSJustin Gibbs 
22503bad06fSJustin Gibbs 	for (pr = list_head(&dd->dd_props);
22603bad06fSJustin Gibbs 	    pr != NULL; pr = list_next(&dd->dd_props, pr)) {
22703bad06fSJustin Gibbs 		if (strcmp(pr->pr_propname, propname) == 0)
22803bad06fSJustin Gibbs 			break;
22903bad06fSJustin Gibbs 	}
23003bad06fSJustin Gibbs 
23103bad06fSJustin Gibbs 	return (pr);
23203bad06fSJustin Gibbs }
23303bad06fSJustin Gibbs 
23403bad06fSJustin Gibbs static dsl_prop_record_t *
dsl_prop_record_create(dsl_dir_t * dd,const char * propname)23503bad06fSJustin Gibbs dsl_prop_record_create(dsl_dir_t *dd, const char *propname)
23603bad06fSJustin Gibbs {
23703bad06fSJustin Gibbs 	dsl_prop_record_t *pr;
23803bad06fSJustin Gibbs 
23903bad06fSJustin Gibbs 	ASSERT(MUTEX_HELD(&dd->dd_lock));
24003bad06fSJustin Gibbs 
24103bad06fSJustin Gibbs 	pr = kmem_alloc(sizeof (dsl_prop_record_t), KM_SLEEP);
24203bad06fSJustin Gibbs 	pr->pr_propname = spa_strdup(propname);
24303bad06fSJustin Gibbs 	list_create(&pr->pr_cbs, sizeof (dsl_prop_cb_record_t),
24403bad06fSJustin Gibbs 	    offsetof(dsl_prop_cb_record_t, cbr_pr_node));
24503bad06fSJustin Gibbs 	list_insert_head(&dd->dd_props, pr);
24603bad06fSJustin Gibbs 
24703bad06fSJustin Gibbs 	return (pr);
24803bad06fSJustin Gibbs }
24903bad06fSJustin Gibbs 
25003bad06fSJustin Gibbs void
dsl_prop_init(dsl_dir_t * dd)25103bad06fSJustin Gibbs dsl_prop_init(dsl_dir_t *dd)
25203bad06fSJustin Gibbs {
25303bad06fSJustin Gibbs 	list_create(&dd->dd_props, sizeof (dsl_prop_record_t),
25403bad06fSJustin Gibbs 	    offsetof(dsl_prop_record_t, pr_node));
25503bad06fSJustin Gibbs }
25603bad06fSJustin Gibbs 
25703bad06fSJustin Gibbs void
dsl_prop_fini(dsl_dir_t * dd)25803bad06fSJustin Gibbs dsl_prop_fini(dsl_dir_t *dd)
25903bad06fSJustin Gibbs {
26003bad06fSJustin Gibbs 	dsl_prop_record_t *pr;
26103bad06fSJustin Gibbs 
26203bad06fSJustin Gibbs 	while ((pr = list_remove_head(&dd->dd_props)) != NULL) {
26303bad06fSJustin Gibbs 		list_destroy(&pr->pr_cbs);
26403bad06fSJustin Gibbs 		strfree((char *)pr->pr_propname);
26503bad06fSJustin Gibbs 		kmem_free(pr, sizeof (dsl_prop_record_t));
26603bad06fSJustin Gibbs 	}
26703bad06fSJustin Gibbs 	list_destroy(&dd->dd_props);
26803bad06fSJustin Gibbs }
26903bad06fSJustin Gibbs 
270fa9e4066Sahrens /*
271fa9e4066Sahrens  * Register interest in the named property.  We'll call the callback
272fa9e4066Sahrens  * once to notify it of the current property value, and again each time
273fa9e4066Sahrens  * the property changes, until this callback is unregistered.
274fa9e4066Sahrens  *
275fa9e4066Sahrens  * Return 0 on success, errno if the prop is not an integer value.
276fa9e4066Sahrens  */
277fa9e4066Sahrens int
dsl_prop_register(dsl_dataset_t * ds,const char * propname,dsl_prop_changed_cb_t * callback,void * cbarg)278fa9e4066Sahrens dsl_prop_register(dsl_dataset_t *ds, const char *propname,
279fa9e4066Sahrens     dsl_prop_changed_cb_t *callback, void *cbarg)
280fa9e4066Sahrens {
28199653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
282bb0ade09Sahrens 	dsl_pool_t *dp = dd->dd_pool;
283fa9e4066Sahrens 	uint64_t value;
28403bad06fSJustin Gibbs 	dsl_prop_record_t *pr;
285fa9e4066Sahrens 	dsl_prop_cb_record_t *cbr;
286fa9e4066Sahrens 	int err;
2883b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
2903b2aab18SMatthew Ahrens 	err = dsl_prop_get_int_ds(ds, propname, &value);
2913b2aab18SMatthew Ahrens 	if (err != 0)
292fa9e4066Sahrens 		return (err);
294fa9e4066Sahrens 	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
29599653d4eSeschrock 	cbr->cbr_ds = ds;
296fa9e4066Sahrens 	cbr->cbr_func = callback;
297fa9e4066Sahrens 	cbr->cbr_arg = cbarg;
29803bad06fSJustin Gibbs 
299fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
30003bad06fSJustin Gibbs 	pr = dsl_prop_record_find(dd, propname);
30103bad06fSJustin Gibbs 	if (pr == NULL)
30203bad06fSJustin Gibbs 		pr = dsl_prop_record_create(dd, propname);
30303bad06fSJustin Gibbs 	cbr->cbr_pr = pr;
30403bad06fSJustin Gibbs 	list_insert_head(&pr->pr_cbs, cbr);
30503bad06fSJustin Gibbs 	list_insert_head(&ds->ds_prop_cbs, cbr);
306fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
308fa9e4066Sahrens 	cbr->cbr_func(cbr->cbr_arg, value);
309fa9e4066Sahrens 	return (0);
310fa9e4066Sahrens }
312fa9e4066Sahrens int
dsl_prop_get(const char * dsname,const char * propname,int intsz,int numints,void * buf,char * setpoint)313bb0ade09Sahrens dsl_prop_get(const char *dsname, const char *propname,
314fa9e4066Sahrens     int intsz, int numints, void *buf, char *setpoint)
315fa9e4066Sahrens {
3163b2aab18SMatthew Ahrens 	objset_t *os;
3173b2aab18SMatthew Ahrens 	int error;
3193b2aab18SMatthew Ahrens 	error = dmu_objset_hold(dsname, FTAG, &os);
3203b2aab18SMatthew Ahrens 	if (error != 0)
3213b2aab18SMatthew Ahrens 		return (error);
3233b2aab18SMatthew Ahrens 	error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
3243b2aab18SMatthew Ahrens 	    intsz, numints, buf, setpoint);
3263b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
3273b2aab18SMatthew Ahrens 	return (error);
328fa9e4066Sahrens }
330fa9e4066Sahrens /*
331fa9e4066Sahrens  * Get the current property value.  It may have changed by the time this
332fa9e4066Sahrens  * function returns, so it is NOT safe to follow up with
333fa9e4066Sahrens  * dsl_prop_register() and assume that the value has not changed in
334fa9e4066Sahrens  * between.
335fa9e4066Sahrens  *
336fa9e4066Sahrens  * Return 0 on success, ENOENT if ddname is invalid.
337fa9e4066Sahrens  */
338fa9e4066Sahrens int
dsl_prop_get_integer(const char * ddname,const char * propname,uint64_t * valuep,char * setpoint)339fa9e4066Sahrens dsl_prop_get_integer(const char *ddname, const char *propname,
340fa9e4066Sahrens     uint64_t *valuep, char *setpoint)
341fa9e4066Sahrens {
342fa9e4066Sahrens 	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
343fa9e4066Sahrens }
3453b2aab18SMatthew Ahrens int
dsl_prop_get_int_ds(dsl_dataset_t * ds,const char * propname,uint64_t * valuep)3463b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname,
3473b2aab18SMatthew Ahrens     uint64_t *valuep)
34892241e0bSTom Erickson {
3493b2aab18SMatthew Ahrens 	return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL));
35092241e0bSTom Erickson }
35192241e0bSTom Erickson 
35292241e0bSTom Erickson /*
35392241e0bSTom Erickson  * Predict the effective value of the given special property if it were set with
35492241e0bSTom Erickson  * the given value and source. This is not a general purpose function. It exists
35592241e0bSTom Erickson  * only to handle the special requirements of the quota and reservation
35692241e0bSTom Erickson  * properties. The fact that these properties are non-inheritable greatly
35792241e0bSTom Erickson  * simplifies the prediction logic.
35892241e0bSTom Erickson  *
35992241e0bSTom Erickson  * Returns 0 on success, a positive error code on failure, or -1 if called with
36092241e0bSTom Erickson  * a property not handled by this function.
36192241e0bSTom Erickson  */
36292241e0bSTom Erickson int
dsl_prop_predict(dsl_dir_t * dd,const char * propname,zprop_source_t source,uint64_t value,uint64_t * newvalp)3633b2aab18SMatthew Ahrens dsl_prop_predict(dsl_dir_t *dd, const char *propname,
3643b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, uint64_t *newvalp)
36592241e0bSTom Erickson {
36692241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
36792241e0bSTom Erickson 	objset_t *mos;
36892241e0bSTom Erickson 	uint64_t zapobj;
36992241e0bSTom Erickson 	uint64_t version;
37092241e0bSTom Erickson 	char *recvdstr;
37192241e0bSTom Erickson 	int err = 0;
37292241e0bSTom Erickson 
37392241e0bSTom Erickson 	switch (prop) {
37492241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
37592241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
37692241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
37792241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
37892241e0bSTom Erickson 		break;
37992241e0bSTom Erickson 	default:
38092241e0bSTom Erickson 		return (-1);
38192241e0bSTom Erickson 	}
38292241e0bSTom Erickson 
38392241e0bSTom Erickson 	mos = dd->dd_pool->dp_meta_objset;
384c1379625SJustin T. Gibbs 	zapobj = dsl_dir_phys(dd)->dd_props_zapobj;
38592241e0bSTom Erickson 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
38692241e0bSTom Erickson 
38792241e0bSTom Erickson 	version = spa_version(dd->dd_pool->dp_spa);
38892241e0bSTom Erickson 	if (version < SPA_VERSION_RECVD_PROPS) {
38992241e0bSTom Erickson 		if (source & ZPROP_SRC_NONE)
39092241e0bSTom Erickson 			source = ZPROP_SRC_NONE;
39192241e0bSTom Erickson 		else if (source & ZPROP_SRC_RECEIVED)
39292241e0bSTom Erickson 			source = ZPROP_SRC_LOCAL;
39392241e0bSTom Erickson 	}
39492241e0bSTom Erickson 
39592241e0bSTom Erickson 	switch (source) {
39692241e0bSTom Erickson 	case ZPROP_SRC_NONE:
39792241e0bSTom Erickson 		/* Revert to the received value, if any. */
3983b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp);
39992241e0bSTom Erickson 		if (err == ENOENT)
4003b2aab18SMatthew Ahrens 			*newvalp = 0;
40192241e0bSTom Erickson 		break;
40292241e0bSTom Erickson 	case ZPROP_SRC_LOCAL:
4033b2aab18SMatthew Ahrens 		*newvalp = value;
40492241e0bSTom Erickson 		break;
40592241e0bSTom Erickson 	case ZPROP_SRC_RECEIVED:
40692241e0bSTom Erickson 		/*
40792241e0bSTom Erickson 		 * If there's no local setting, then the new received value will
40892241e0bSTom Erickson 		 * be the effective value.
40992241e0bSTom Erickson 		 */
4103b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
41192241e0bSTom Erickson 		if (err == ENOENT)
4123b2aab18SMatthew Ahrens 			*newvalp = value;
41392241e0bSTom Erickson 		break;
41492241e0bSTom Erickson 	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
41592241e0bSTom Erickson 		/*
41692241e0bSTom Erickson 		 * We're clearing the received value, so the local setting (if
41792241e0bSTom Erickson 		 * it exists) remains the effective value.
41892241e0bSTom Erickson 		 */
4193b2aab18SMatthew Ahrens 		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
42092241e0bSTom Erickson 		if (err == ENOENT)
4213b2aab18SMatthew Ahrens 			*newvalp = 0;
42292241e0bSTom Erickson 		break;
42392241e0bSTom Erickson 	default:
4243b2aab18SMatthew Ahrens 		panic("unexpected property source: %d", source);
42592241e0bSTom Erickson 	}
42692241e0bSTom Erickson 
42792241e0bSTom Erickson 	strfree(recvdstr);
42892241e0bSTom Erickson 
42992241e0bSTom Erickson 	if (err == ENOENT)
43092241e0bSTom Erickson 		return (0);
43192241e0bSTom Erickson 
43292241e0bSTom Erickson 	return (err);
43392241e0bSTom Erickson }
43492241e0bSTom Erickson 
435fa9e4066Sahrens /*
43603bad06fSJustin Gibbs  * Unregister all callbacks that are registered with the
43703bad06fSJustin Gibbs  * given callback argument.
438fa9e4066Sahrens  */
43903bad06fSJustin Gibbs void
dsl_prop_unregister_all(dsl_dataset_t * ds,void * cbarg)44003bad06fSJustin Gibbs dsl_prop_unregister_all(dsl_dataset_t *ds, void *cbarg)
441fa9e4066Sahrens {
44203bad06fSJustin Gibbs 	dsl_prop_cb_record_t *cbr, *next_cbr;
44303bad06fSJustin Gibbs 
44499653d4eSeschrock 	dsl_dir_t *dd = ds->ds_dir;
446fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
44703bad06fSJustin Gibbs 	next_cbr = list_head(&ds->ds_prop_cbs);
44803bad06fSJustin Gibbs 	while (next_cbr != NULL) {
44903bad06fSJustin Gibbs 		cbr = next_cbr;
45003bad06fSJustin Gibbs 		next_cbr = list_next(&ds->ds_prop_cbs, cbr);
45103bad06fSJustin Gibbs 		if (cbr->cbr_arg == cbarg) {
45203bad06fSJustin Gibbs 			list_remove(&ds->ds_prop_cbs, cbr);
45303bad06fSJustin Gibbs 			list_remove(&cbr->cbr_pr->pr_cbs, cbr);
45403bad06fSJustin Gibbs 			kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
45503bad06fSJustin Gibbs 		}
456fa9e4066Sahrens 	}
457fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
458fa9e4066Sahrens }
4603b2aab18SMatthew Ahrens boolean_t
dsl_prop_hascb(dsl_dataset_t * ds)4613b2aab18SMatthew Ahrens dsl_prop_hascb(dsl_dataset_t *ds)
46299653d4eSeschrock {
46303bad06fSJustin Gibbs 	return (!list_is_empty(&ds->ds_prop_cbs));
4643b2aab18SMatthew Ahrens }
4663b2aab18SMatthew Ahrens /* ARGSUSED */
4673b2aab18SMatthew Ahrens static int
dsl_prop_notify_all_cb(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)4683b2aab18SMatthew Ahrens dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
4693b2aab18SMatthew Ahrens {
4703b2aab18SMatthew Ahrens 	dsl_dir_t *dd = ds->ds_dir;
47103bad06fSJustin Gibbs 	dsl_prop_record_t *pr;
4723b2aab18SMatthew Ahrens 	dsl_prop_cb_record_t *cbr;
4733b2aab18SMatthew Ahrens 
4743b2aab18SMatthew Ahrens 	mutex_enter(&dd->dd_lock);
47503bad06fSJustin Gibbs 	for (pr = list_head(&dd->dd_props);
47603bad06fSJustin Gibbs 	    pr; pr = list_next(&dd->dd_props, pr)) {
47703bad06fSJustin Gibbs 		for (cbr = list_head(&pr->pr_cbs); cbr;
47803bad06fSJustin Gibbs 		    cbr = list_next(&pr->pr_cbs, cbr)) {
47903bad06fSJustin Gibbs 			uint64_t value;
4803b2aab18SMatthew Ahrens 
48103bad06fSJustin Gibbs 			/*
48203bad06fSJustin Gibbs 			 * Callback entries do not have holds on their
48303bad06fSJustin Gibbs 			 * datasets so that datasets with registered
48403bad06fSJustin Gibbs 			 * callbacks are still eligible for eviction.
48503bad06fSJustin Gibbs 			 * Unlike operations to update properties on a
48603bad06fSJustin Gibbs 			 * single dataset, we are performing a recursive
48703bad06fSJustin Gibbs 			 * descent of related head datasets.  The caller
48803bad06fSJustin Gibbs 			 * of this function only has a dataset hold on
48903bad06fSJustin Gibbs 			 * the passed in head dataset, not the snapshots
49003bad06fSJustin Gibbs 			 * associated with this dataset.  Without a hold,
49103bad06fSJustin Gibbs 			 * the dataset pointer within callback records
49203bad06fSJustin Gibbs 			 * for snapshots can be invalidated by eviction
49303bad06fSJustin Gibbs 			 * at any time.
49403bad06fSJustin Gibbs 			 *
49503bad06fSJustin Gibbs 			 * Use dsl_dataset_try_add_ref() to verify
49603bad06fSJustin Gibbs 			 * that the dataset for a snapshot has not
49703bad06fSJustin Gibbs 			 * begun eviction processing and to prevent
49803bad06fSJustin Gibbs 			 * eviction from occurring for the duration of
49903bad06fSJustin Gibbs 			 * the callback.  If the hold attempt fails,
50003bad06fSJustin Gibbs 			 * this object is already being evicted and the
50103bad06fSJustin Gibbs 			 * callback can be safely ignored.
50203bad06fSJustin Gibbs 			 */
50303bad06fSJustin Gibbs 			if (ds != cbr->cbr_ds &&
50403bad06fSJustin Gibbs 			    !dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
50503bad06fSJustin Gibbs 				continue;
506e57a022bSJustin T. Gibbs 
50703bad06fSJustin Gibbs 			if (dsl_prop_get_ds(cbr->cbr_ds,
50803bad06fSJustin Gibbs 			    cbr->cbr_pr->pr_propname, sizeof (value), 1,
50903bad06fSJustin Gibbs 			    &value, NULL) == 0)
51003bad06fSJustin Gibbs 				cbr->cbr_func(cbr->cbr_arg, value);
511e57a022bSJustin T. Gibbs 
51203bad06fSJustin Gibbs 			if (ds != cbr->cbr_ds)
51303bad06fSJustin Gibbs 				dsl_dataset_rele(cbr->cbr_ds, FTAG);
51403bad06fSJustin Gibbs 		}
5153b2aab18SMatthew Ahrens 	}
5163b2aab18SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
5173b2aab18SMatthew Ahrens 
5183b2aab18SMatthew Ahrens 	return (0);
5193b2aab18SMatthew Ahrens }
5203b2aab18SMatthew Ahrens 
5213b2aab18SMatthew Ahrens /*
5223b2aab18SMatthew Ahrens  * Update all property values for ddobj & its descendants.  This is used
5233b2aab18SMatthew Ahrens  * when renaming the dir.
5243b2aab18SMatthew Ahrens  */
5253b2aab18SMatthew Ahrens void
dsl_prop_notify_all(dsl_dir_t * dd)5263b2aab18SMatthew Ahrens dsl_prop_notify_all(dsl_dir_t *dd)
5273b2aab18SMatthew Ahrens {
5283b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dd->dd_pool;
5293b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
5303b2aab18SMatthew Ahrens 	(void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb,
5313b2aab18SMatthew Ahrens 	    NULL, DS_FIND_CHILDREN);
53299653d4eSeschrock }
534fa9e4066Sahrens static void
dsl_prop_changed_notify(dsl_pool_t * dp,uint64_t ddobj,const char * propname,uint64_t value,int first)535fa9e4066Sahrens dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
536fa9e4066Sahrens     const char *propname, uint64_t value, int first)
537fa9e4066Sahrens {
538fa9e4066Sahrens 	dsl_dir_t *dd;
53903bad06fSJustin Gibbs 	dsl_prop_record_t *pr;
540fa9e4066Sahrens 	dsl_prop_cb_record_t *cbr;
541fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
5421d452cf5Sahrens 	zap_cursor_t zc;
543d8d77200Sahrens 	zap_attribute_t *za;
544fa9e4066Sahrens 	int err;
5463b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
5473b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
548ea8dc4b6Seschrock 	if (err)
549ea8dc4b6Seschrock 		return;
551fa9e4066Sahrens 	if (!first) {
552fa9e4066Sahrens 		/*
553fa9e4066Sahrens 		 * If the prop is set here, then this change is not
554fa9e4066Sahrens 		 * being inherited here or below; stop the recursion.
555fa9e4066Sahrens 		 */
556c1379625SJustin T. Gibbs 		err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
557c1379625SJustin T. Gibbs 		    propname);
558fa9e4066Sahrens 		if (err == 0) {
5593b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
560fa9e4066Sahrens 			return;
561fa9e4066Sahrens 		}
562fa9e4066Sahrens 		ASSERT3U(err, ==, ENOENT);
563fa9e4066Sahrens 	}
565fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
56603bad06fSJustin Gibbs 	pr = dsl_prop_record_find(dd, propname);
56703bad06fSJustin Gibbs 	if (pr != NULL) {
56803bad06fSJustin Gibbs 		for (cbr = list_head(&pr->pr_cbs); cbr;
56903bad06fSJustin Gibbs 		    cbr = list_next(&pr->pr_cbs, cbr)) {
57003bad06fSJustin Gibbs 			uint64_t propobj;
57203bad06fSJustin Gibbs 			/*
57303bad06fSJustin Gibbs 			 * cbr->cbr_ds may be invalidated due to eviction,
57403bad06fSJustin Gibbs 			 * requiring the use of dsl_dataset_try_add_ref().
57503bad06fSJustin Gibbs 			 * See comment block in dsl_prop_notify_all_cb()
57603bad06fSJustin Gibbs 			 * for details.
57703bad06fSJustin Gibbs 			 */
57803bad06fSJustin Gibbs 			if (!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
57903bad06fSJustin Gibbs 				continue;
58103bad06fSJustin Gibbs 			propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
582e57a022bSJustin T. Gibbs 
58303bad06fSJustin Gibbs 			/*
58403bad06fSJustin Gibbs 			 * If the property is not set on this ds, then it is
58503bad06fSJustin Gibbs 			 * inherited here; call the callback.
58603bad06fSJustin Gibbs 			 */
58703bad06fSJustin Gibbs 			if (propobj == 0 ||
58803bad06fSJustin Gibbs 			    zap_contains(mos, propobj, propname) != 0)
58903bad06fSJustin Gibbs 				cbr->cbr_func(cbr->cbr_arg, value);
59103bad06fSJustin Gibbs 			dsl_dataset_rele(cbr->cbr_ds, FTAG);
59203bad06fSJustin Gibbs 		}
593fa9e4066Sahrens 	}
594fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
596d8d77200Sahrens 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
5971d452cf5Sahrens 	for (zap_cursor_init(&zc, mos,
598c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj);
599d8d77200Sahrens 	    zap_cursor_retrieve(&zc, za) == 0;
6001d452cf5Sahrens 	    zap_cursor_advance(&zc)) {
601d8d77200Sahrens 		dsl_prop_changed_notify(dp, za->za_first_integer,
6021d452cf5Sahrens 		    propname, value, FALSE);
603fa9e4066Sahrens 	}
604d8d77200Sahrens 	kmem_free(za, sizeof (zap_attribute_t));
6051d452cf5Sahrens 	zap_cursor_fini(&zc);
6063b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
607fa9e4066Sahrens }
60992241e0bSTom Erickson void
dsl_prop_set_sync_impl(dsl_dataset_t * ds,const char * propname,zprop_source_t source,int intsz,int numints,const void * value,dmu_tx_t * tx)6103b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname,
6113b2aab18SMatthew Ahrens     zprop_source_t source, int intsz, int numints, const void *value,
6123b2aab18SMatthew Ahrens     dmu_tx_t *tx)
613fa9e4066Sahrens {
614bb0ade09Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
61592241e0bSTom Erickson 	uint64_t zapobj, intval, dummy;
6161d452cf5Sahrens 	int isint;
617ecd6cf80Smarks 	char valbuf[32];
6183b2aab18SMatthew Ahrens 	const char *valstr = NULL;
61992241e0bSTom Erickson 	char *inheritstr;
62092241e0bSTom Erickson 	char *recvdstr;
62192241e0bSTom Erickson 	char *tbuf = NULL;
62292241e0bSTom Erickson 	int err;
62392241e0bSTom Erickson 	uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa);
625d09e4475SAlex Wilson 	isint = (dodefault(zfs_name_to_prop(propname), 8, 1, &intval) == 0);
627bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
62892241e0bSTom Erickson 		ASSERT(version >= SPA_VERSION_SNAP_PROPS);
629c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_props_obj == 0) {
630bb0ade09Sahrens 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
631c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds)->ds_props_obj =
632bb0ade09Sahrens 			    zap_create(mos,
633bb0ade09Sahrens 			    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
634bb0ade09Sahrens 		}
635c1379625SJustin T. Gibbs 		zapobj = dsl_dataset_phys(ds)->ds_props_obj;
636bb0ade09Sahrens 	} else {
637c1379625SJustin T. Gibbs 		zapobj = dsl_dir_phys(ds->ds_dir)->dd_props_zapobj;
638bb0ade09Sahrens 	}
64092241e0bSTom Erickson 	if (version < SPA_VERSION_RECVD_PROPS) {
64192241e0bSTom Erickson 		if (source & ZPROP_SRC_NONE)
64292241e0bSTom Erickson 			source = ZPROP_SRC_NONE;
64392241e0bSTom Erickson 		else if (source & ZPROP_SRC_RECEIVED)
64492241e0bSTom Erickson 			source = ZPROP_SRC_LOCAL;
64592241e0bSTom Erickson 	}
64692241e0bSTom Erickson 
64792241e0bSTom Erickson 	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
64892241e0bSTom Erickson 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
64992241e0bSTom Erickson 
65092241e0bSTom Erickson 	switch (source) {
65192241e0bSTom Erickson 	case ZPROP_SRC_NONE:
65292241e0bSTom Erickson 		/*
65392241e0bSTom Erickson 		 * revert to received value, if any (inherit -S)
65492241e0bSTom Erickson 		 * - remove propname
65592241e0bSTom Erickson 		 * - remove propname$inherit
65692241e0bSTom Erickson 		 */
65792241e0bSTom Erickson 		err = zap_remove(mos, zapobj, propname, tx);
65892241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
65992241e0bSTom Erickson 		err = zap_remove(mos, zapobj, inheritstr, tx);
66092241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
66192241e0bSTom Erickson 		break;
66292241e0bSTom Erickson 	case ZPROP_SRC_LOCAL:
66392241e0bSTom Erickson 		/*
66492241e0bSTom Erickson 		 * remove propname$inherit
66592241e0bSTom Erickson 		 * set propname -> value
66692241e0bSTom Erickson 		 */
66792241e0bSTom Erickson 		err = zap_remove(mos, zapobj, inheritstr, tx);
66892241e0bSTom Erickson 		ASSERT(err == 0 || err == ENOENT);
6693b2aab18SMatthew Ahrens 		VERIFY0(zap_update(mos, zapobj, propname,
6703b2aab18SMatthew Ahrens 		    intsz, numints, value, tx));
67192241e0bSTom Erickson 		break;
67292241e0bSTom Erickson 	case ZPROP_SRC_INHERITED:
67392241e0bSTom Erickson 		/*
67492241e0bSTom Erickson 		 * explicitly inherit
67592241e0bSTom Erickson 		 * - remove propname
67692241e0bSTom Erickson 		 * - set propname$inherit
67792241e0bSTom Erickson 		 */
67892241e0bSTom Erickson 		err = zap_remove(mos, zapobj, propname, tx);
6791d452cf5Sahrens 		ASSERT(err == 0 || err == ENOENT);
68092241e0bSTom Erickson 		if (version >= SPA_VERSION_RECVD_PROPS &&
6813b2aab18SMatthew Ahrens 		    dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) {
68292241e0bSTom Erickson 			dummy = 0;
6833b2aab18SMatthew Ahrens 			VERIFY0(zap_update(mos, zapobj, inheritstr,
6843b2aab18SMatthew Ahrens 			    8, 1, &dummy, tx));
685fa9e4066Sahrens 		}
68692241e0bSTom Erickson 		break;
68792241e0bSTom Erickson 	case ZPROP_SRC_RECEIVED:
68892241e0bSTom Erickson 		/*
68992241e0bSTom Erickson 		 * set propname$recvd -> value
69092241e0bSTom Erickson 		 */
69192241e0bSTom Erickson 		err = zap_update(mos, zapobj, recvdstr,
6923b2aab18SMatthew Ahrens 		    intsz, numints, value, tx);
69392241e0bSTom Erickson 		ASSERT(err == 0);
69492241e0bSTom Erickson 		break;
69692241e0bSTom Erickson 		/*