2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
223f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
239adfa60Matthew Ahrens * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24013023dMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved.
252d85dedJason King * Copyright 2019 Joyent, Inc.
26fa9e406ahrens */
2892241e0Tom Erickson#include <sys/zfs_context.h>
29fa9e406ahrens#include <sys/dmu.h>
307f7322feschrock#include <sys/dmu_objset.h>
31fa9e406ahrens#include <sys/dmu_tx.h>
32fa9e406ahrens#include <sys/dsl_dataset.h>
33fa9e406ahrens#include <sys/dsl_dir.h>
34fa9e406ahrens#include <sys/dsl_prop.h>
351d452cfahrens#include <sys/dsl_synctask.h>
36fa9e406ahrens#include <sys/spa.h>
37fa9e406ahrens#include <sys/zap.h>
38fa9e406ahrens#include <sys/fs/zfs.h>
40fa9e406ahrens#include "zfs_prop.h"
4292241e0Tom Erickson#define	ZPROP_INHERIT_SUFFIX "$inherit"
4392241e0Tom Erickson#define	ZPROP_RECVD_SUFFIX "$recvd"
4492241e0Tom Erickson
45fa9e406ahrensstatic int
46d09e447Alex Wilsondodefault(zfs_prop_t prop, int intsz, int numints, void *buf)
48da6c28aamw	/*
49da6c28aamw	 * The setonce properties are read-only, BUT they still
50da6c28aamw	 * have a default value that can be used as the initial
51da6c28aamw	 * value.
52da6c28aamw	 */
53d09e447Alex Wilson	if (prop == ZPROP_INVAL ||
54da6c28aamw	    (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
55be6fd75Matthew Ahrens		return (SET_ERROR(ENOENT));
5791ebeefahrens	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
58fa9e406ahrens		if (intsz != 1)
59be6fd75Matthew Ahrens			return (SET_ERROR(EOVERFLOW));
60990b485lling		(void) strncpy(buf, zfs_prop_default_string(prop),
6192241e0Tom Erickson		    numints);
62fa9e406ahrens	} else {
6392241e0Tom Erickson		if (intsz != 8 || numints < 1)
64be6fd75Matthew Ahrens			return (SET_ERROR(EOVERFLOW));
66fa9e406ahrens		*(uint64_t *)buf = zfs_prop_default_numeric(prop);
67fa9e406ahrens	}
69fa9e406ahrens	return (0);
73bb0ade0ahrensdsl_prop_get_dd(dsl_dir_t *dd, const char *propname,
7492241e0Tom Erickson    int intsz, int numints, void *buf, char *setpoint, boolean_t snapshot)
7699653d4eschrock	int err = ENOENT;
7792241e0Tom Erickson	dsl_dir_t *target = dd;
78bb0ade0ahrens	objset_t *mos = dd->dd_pool->dp_meta_objset;
79e9dbad6eschrock	zfs_prop_t prop;
8092241e0Tom Erickson	boolean_t inheritable;
8192241e0Tom Erickson	boolean_t inheriting = B_FALSE;
8292241e0Tom Erickson	char *inheritstr;
8392241e0Tom Erickson	char *recvdstr;
853b2aab1Matthew Ahrens	ASSERT(dsl_pool_config_held(dd->dd_pool));
87fa9e406ahrens	if (setpoint)
88fa9e406ahrens		setpoint[0] = '\0';
90e9dbad6eschrock	prop = zfs_name_to_prop(propname);
9192241e0Tom Erickson	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
9292241e0Tom Erickson	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
9392241e0Tom Erickson	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
9599653d4eschrock	/*
9692241e0Tom Erickson	 * Note: dd may become NULL, therefore we shouldn't dereference it
9792241e0Tom Erickson	 * after this loop.
9899653d4eschrock	 */
9999653d4eschrock	for (; dd != NULL; dd = dd->dd_parent) {
10092241e0Tom Erickson		if (dd != target || snapshot) {
10192241e0Tom Erickson			if (!inheritable)
10292241e0Tom Erickson				break;
10392241e0Tom Erickson			inheriting = B_TRUE;
10492241e0Tom Erickson		}
10592241e0Tom Erickson
10692241e0Tom Erickson		/* Check for a local value. */
107c137962Justin T. Gibbs		err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
108c137962Justin T. Gibbs		    propname, intsz, numints, buf);
109fa9e406ahrens		if (err != ENOENT) {
11092241e0Tom Erickson			if (setpoint != NULL && err == 0)
111fa9e406ahrens				dsl_dir_name(dd, setpoint);
112fa9e406ahrens			break;
113fa9e406ahrens		}
115e9dbad6eschrock		/*
11692241e0Tom Erickson		 * Skip the check for a received value if there is an explicit
11792241e0Tom Erickson		 * inheritance entry.
118e9dbad6eschrock		 */
119c137962Justin T. Gibbs		err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
12092241e0Tom Erickson		    inheritstr);
12192241e0Tom Erickson		if (err != 0 && err != ENOENT)
122e9dbad6eschrock			break;
12392241e0Tom Erickson
12492241e0Tom Erickson		if (err == ENOENT) {
12592241e0Tom Erickson			/* Check for a received value. */
126c137962Justin T. Gibbs			err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
12792241e0Tom Erickson			    recvdstr, intsz, numints, buf);
12892241e0Tom Erickson			if (err != ENOENT) {
12992241e0Tom Erickson				if (setpoint != NULL && err == 0) {
13092241e0Tom Erickson					if (inheriting) {
13192241e0Tom Erickson						dsl_dir_name(dd, setpoint);
13292241e0Tom Erickson					} else {
13392241e0Tom Erickson						(void) strcpy(setpoint,
13492241e0Tom Erickson						    ZPROP_SOURCE_VAL_RECVD);
13592241e0Tom Erickson					}
13692241e0Tom Erickson				}
13792241e0Tom Erickson				break;
13892241e0Tom Erickson			}
13992241e0Tom Erickson		}
14092241e0Tom Erickson
14192241e0Tom Erickson		/*
14292241e0Tom Erickson		 * If we found an explicit inheritance entry, err is zero even
14392241e0Tom Erickson		 * though we haven't yet found the value, so reinitializing err
14492241e0Tom Erickson		 * at the end of the loop (instead of at the beginning) ensures
14592241e0Tom Erickson		 * that err has a valid post-loop value.
14692241e0Tom Erickson		 */
147be6fd75Matthew Ahrens		err = SET_ERROR(ENOENT);
148fa9e406ahrens	}
14992241e0Tom Erickson
150fa9e406ahrens	if (err == ENOENT)
151d09e447Alex Wilson		err = dodefault(prop, intsz, numints, buf);
15292241e0Tom Erickson
15392241e0Tom Erickson	strfree(inheritstr);
15492241e0Tom Erickson	strfree(recvdstr);
156fa9e406ahrens	return (err);
160bb0ade0ahrensdsl_prop_get_ds(dsl_dataset_t *ds, const char *propname,
16192241e0Tom Erickson    int intsz, int numints, void *buf, char *setpoint)
16392241e0Tom Erickson	zfs_prop_t prop = zfs_name_to_prop(propname);
16492241e0Tom Erickson	boolean_t inheritable;
16592241e0Tom Erickson	uint64_t zapobj;
16692241e0Tom Erickson
1673b2aab1Matthew Ahrens	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
16892241e0Tom Erickson	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
169c137962Justin T. Gibbs	zapobj = dsl_dataset_phys(ds)->ds_props_obj;
17092241e0Tom Erickson
17192241e0Tom Erickson	if (zapobj != 0) {
17292241e0Tom Erickson		objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
17392241e0Tom Erickson		int err;
175bc9014eJustin Gibbs		ASSERT(ds->ds_is_snapshot);
17692241e0Tom Erickson
17792241e0Tom Erickson		/* Check for a local value. */
17892241e0Tom Erickson		err = zap_lookup(mos, zapobj, propname, intsz, numints, buf);
179bb0ade0ahrens		if (err != ENOENT) {
18092241e0Tom Erickson			if (setpoint != NULL && err == 0)
181bb0ade0ahrens				dsl_dataset_name(ds, setpoint);
182bb0ade0ahrens			return (err);
183bb0ade0ahrens		}
18492241e0Tom Erickson
18592241e0Tom Erickson		/*
18692241e0Tom Erickson		 * Skip the check for a received value if there is an explicit
18792241e0Tom Erickson		 * inheritance entry.
18892241e0Tom Erickson		 */
18992241e0Tom Erickson		if (inheritable) {
19092241e0Tom Erickson			char *inheritstr = kmem_asprintf("%s%s", propname,
19192241e0Tom Erickson			    ZPROP_INHERIT_SUFFIX);
19292241e0Tom Erickson			err = zap_contains(mos, zapobj, inheritstr);
19392241e0Tom Erickson			strfree(inheritstr);
19492241e0Tom Erickson			if (err != 0 && err != ENOENT)
19592241e0Tom Erickson				return (err);
19692241e0Tom Erickson		}
19792241e0Tom Erickson
19892241e0Tom Erickson		if (err == ENOENT) {
19992241e0Tom Erickson			/* Check for a received value. */
20092241e0Tom Erickson			char *recvdstr = kmem_asprintf("%s%s", propname,
20192241e0Tom Erickson			    ZPROP_RECVD_SUFFIX);
20292241e0Tom Erickson			err = zap_lookup(mos, zapobj, recvdstr,
20392241e0Tom Erickson			    intsz, numints, buf);
20492241e0Tom Erickson			strfree(recvdstr);
20592241e0Tom Erickson			if (err != ENOENT) {
20692241e0Tom Erickson				if (setpoint != NULL && err == 0)
20792241e0Tom Erickson					(void) strcpy(setpoint,
20892241e0Tom Erickson					    ZPROP_SOURCE_VAL_RECVD);
20992241e0Tom Erickson				return (err);
21092241e0Tom Erickson			}
21192241e0Tom Erickson		}
212bb0ade0ahrens	}
214bb0ade0ahrens	return (dsl_prop_get_dd(ds->ds_dir, propname,
215bc9014eJustin Gibbs	    intsz, numints, buf, setpoint, ds->ds_is_snapshot));
21803bad06Justin Gibbsstatic dsl_prop_record_t *
21903bad06Justin Gibbsdsl_prop_record_find(dsl_dir_t *dd, const char *propname)
22003bad06Justin Gibbs{
22103bad06Justin Gibbs	dsl_prop_record_t *pr = NULL;
22203bad06Justin Gibbs
22303bad06Justin Gibbs	ASSERT(MUTEX_HELD(&dd->dd_lock));
22403bad06Justin Gibbs
22503bad06Justin Gibbs	for (pr = list_head(&dd->dd_props);
22603bad06Justin Gibbs	    pr != NULL; pr = list_next(&dd->dd_props, pr)) {
22703bad06Justin Gibbs		if (strcmp(pr->pr_propname, propname) == 0)
22803bad06Justin Gibbs			break;
22903bad06Justin Gibbs	}
23003bad06Justin Gibbs
23103bad06Justin Gibbs	return (pr);
23203bad06Justin Gibbs}
23303bad06Justin Gibbs
23403bad06Justin Gibbsstatic dsl_prop_record_t *
23503bad06Justin Gibbsdsl_prop_record_create(dsl_dir_t *dd, const char *propname)
23603bad06Justin Gibbs{
23703bad06Justin Gibbs	dsl_prop_record_t *pr;
23803bad06Justin Gibbs
23903bad06Justin Gibbs	ASSERT(MUTEX_HELD(&dd->dd_lock));
24003bad06Justin Gibbs
24103bad06Justin Gibbs	pr = kmem_alloc(sizeof (dsl_prop_record_t), KM_SLEEP);
24203bad06Justin Gibbs	pr->pr_propname = spa_strdup(propname);
24303bad06Justin Gibbs	list_create(&pr->pr_cbs, sizeof (dsl_prop_cb_record_t),
24403bad06Justin Gibbs	    offsetof(dsl_prop_cb_record_t, cbr_pr_node));
24503bad06Justin Gibbs	list_insert_head(&dd->dd_props, pr);
24603bad06Justin Gibbs
24703bad06Justin Gibbs	return (pr);
24803bad06Justin Gibbs}
24903bad06Justin Gibbs
25003bad06Justin Gibbsvoid
25103bad06Justin Gibbsdsl_prop_init(dsl_dir_t *dd)
25203bad06Justin Gibbs{
25303bad06Justin Gibbs	list_create(&dd->dd_props, sizeof (dsl_prop_record_t),
25403bad06Justin Gibbs	    offsetof(dsl_prop_record_t, pr_node));
25503bad06Justin Gibbs}
25603bad06Justin Gibbs
25703bad06Justin Gibbsvoid
25803bad06Justin Gibbsdsl_prop_fini(dsl_dir_t *dd)
25903bad06Justin Gibbs{
26003bad06Justin Gibbs	dsl_prop_record_t *pr;
26103bad06Justin Gibbs
26203bad06Justin Gibbs	while ((pr = list_remove_head(&dd->dd_props)) != NULL) {
26303bad06Justin Gibbs		list_destroy(&pr->pr_cbs);
26403bad06Justin Gibbs		strfree((char *)pr->pr_propname);
26503bad06Justin Gibbs		kmem_free(pr, sizeof (dsl_prop_record_t));
26603bad06Justin Gibbs	}
26703bad06Justin Gibbs	list_destroy(&dd->dd_props);
26803bad06Justin Gibbs}
26903bad06Justin Gibbs
271fa9e406ahrens * Register interest in the named property.  We'll call the callback
272fa9e406ahrens * once to notify it of the current property value, and again each time
273fa9e406ahrens * the property changes, until this callback is unregistered.
274fa9e406ahrens *
275fa9e406ahrens * Return 0 on success, errno if the prop is not an integer value.
276fa9e406ahrens */
278fa9e406ahrensdsl_prop_register(dsl_dataset_t *ds, const char *propname,
279fa9e406ahrens    dsl_prop_changed_cb_t *callback, void *cbarg)
28199653d4eschrock	dsl_dir_t *dd = ds->ds_dir;
282bb0ade0ahrens	dsl_pool_t *dp = dd->dd_pool;
283fa9e406ahrens	uint64_t value;
28403bad06Justin Gibbs	dsl_prop_record_t *pr;
285fa9e406ahrens	dsl_prop_cb_record_t *cbr;
286fa9e406ahrens	int err;
2883b2aab1Matthew Ahrens	ASSERT(dsl_pool_config_held(dp));
2903b2aab1Matthew Ahrens	err = dsl_prop_get_int_ds(ds, propname, &value);
2913b2aab1Matthew Ahrens	if (err != 0)
292fa9e406ahrens		return (err);
294fa9e406ahrens	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
29599653d4eschrock	cbr->cbr_ds = ds;
296fa9e406ahrens	cbr->cbr_func = callback;
297fa9e406ahrens	cbr->cbr_arg = cbarg;
29803bad06Justin Gibbs
299fa9e406ahrens	mutex_enter(&dd->dd_lock);
30003bad06Justin Gibbs	pr = dsl_prop_record_find(dd, propname);
30103bad06Justin Gibbs	if (pr == NULL)
30203bad06Justin Gibbs		pr = dsl_prop_record_create(dd, propname);
30303bad06Justin Gibbs	cbr->cbr_pr = pr;
30403bad06Justin Gibbs	list_insert_head(&pr->pr_cbs, cbr);
30503bad06Justin Gibbs	list_insert_head(&ds->ds_prop_cbs, cbr);
306fa9e406ahrens	mutex_exit(&dd->dd_lock);
308fa9e406ahrens	cbr->cbr_func(cbr->cbr_arg, value);
309fa9e406ahrens	return (0);
313bb0ade0ahrensdsl_prop_get(const char *dsname, const char *propname,
314fa9e406ahrens    int intsz, int numints, void *buf, char *setpoint)
3163b2aab1Matthew Ahrens	objset_t *os;
3173b2aab1Matthew Ahrens	int error;
3193b2aab1Matthew Ahrens	error = dmu_objset_hold(dsname, FTAG, &os);
3203b2aab1Matthew Ahrens	if (error != 0)
3213b2aab1Matthew Ahrens		return (error);
3233b2aab1Matthew Ahrens	error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
3243b2aab1Matthew Ahrens	    intsz, numints, buf, setpoint);
3263b2aab1Matthew Ahrens	dmu_objset_rele(os, FTAG);
3273b2aab1Matthew Ahrens	return (error);
331fa9e406ahrens * Get the current property value.  It may have changed by the time this
332fa9e406ahrens * function returns, so it is NOT safe to follow up with
333fa9e406ahrens * dsl_prop_register() and assume that the value has not changed in
334fa9e406ahrens * between.
335fa9e406ahrens *
336fa9e406ahrens * Return 0 on success, ENOENT if ddname is invalid.
337fa9e406ahrens */
339fa9e406ahrensdsl_prop_get_integer(const char *ddname, const char *propname,
340fa9e406ahrens    uint64_t *valuep, char *setpoint)
342fa9e406ahrens	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
3453b2aab1Matthew Ahrensint
3463b2aab1Matthew Ahrensdsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname,
3473b2aab1Matthew Ahrens    uint64_t *valuep)
34892241e0Tom Erickson{
3493b2aab1Matthew Ahrens	return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL));
35092241e0Tom Erickson}
35192241e0Tom Erickson
35292241e0Tom Erickson/*
35392241e0Tom Erickson * Predict the effective value of the given special property if it were set with
35492241e0Tom Erickson * the given value and source. This is not a general purpose function. It exists
35592241e0Tom Erickson * only to handle the special requirements of the quota and reservation
35692241e0Tom Erickson * properties. The fact that these properties are non-inheritable greatly
35792241e0Tom Erickson * simplifies the prediction logic.
35892241e0Tom Erickson *
35992241e0Tom Erickson * Returns 0 on success, a positive error code on failure, or -1 if called with
36092241e0Tom Erickson * a property not handled by this function.
36192241e0Tom Erickson */
36292241e0Tom Ericksonint
3633b2aab1Matthew Ahrensdsl_prop_predict(dsl_dir_t *dd, const char *propname,
3643b2aab1Matthew Ahrens    zprop_source_t source, uint64_t value, uint64_t *newvalp)
36592241e0Tom Erickson{
36692241e0Tom Erickson	zfs_prop_t prop = zfs_name_to_prop(propname);
36792241e0Tom Erickson	objset_t *mos;
36892241e0Tom Erickson	uint64_t zapobj;
36992241e0Tom Erickson	uint64_t version;
37092241e0Tom Erickson	char *recvdstr;
37192241e0Tom Erickson	int err = 0;
37292241e0Tom Erickson
37392241e0Tom Erickson	switch (prop) {
37492241e0Tom Erickson	case ZFS_PROP_QUOTA:
37592241e0Tom Erickson	case ZFS_PROP_RESERVATION:
37692241e0Tom Erickson	case ZFS_PROP_REFQUOTA:
37792241e0Tom Erickson	case ZFS_PROP_REFRESERVATION:
37892241e0Tom Erickson		break;
37992241e0Tom Erickson	default:
38092241e0Tom Erickson		return (-1);
38192241e0Tom Erickson	}
38292241e0Tom Erickson
38392241e0Tom Erickson	mos = dd->dd_pool->dp_meta_objset;
384c137962Justin T. Gibbs	zapobj = dsl_dir_phys(dd)->dd_props_zapobj;
38592241e0Tom Erickson	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
38692241e0Tom Erickson
38792241e0Tom Erickson	version = spa_version(dd->dd_pool->dp_spa);
38892241e0Tom Erickson	if (version < SPA_VERSION_RECVD_PROPS) {
38992241e0Tom Erickson		if (source & ZPROP_SRC_NONE)
39092241e0Tom Erickson			source = ZPROP_SRC_NONE;
39192241e0Tom Erickson		else if (source & ZPROP_SRC_RECEIVED)
39292241e0Tom Erickson			source = ZPROP_SRC_LOCAL;
39392241e0Tom Erickson	}
39492241e0Tom Erickson
39592241e0Tom Erickson	switch (source) {
39692241e0Tom Erickson	case ZPROP_SRC_NONE:
39792241e0Tom Erickson		/* Revert to the received value, if any. */
3983b2aab1Matthew Ahrens		err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp);
39992241e0Tom Erickson		if (err == ENOENT)
4003b2aab1Matthew Ahrens			*newvalp = 0;
40192241e0Tom Erickson		break;
40292241e0Tom Erickson	case ZPROP_SRC_LOCAL:
4033b2aab1Matthew Ahrens		*newvalp = value;
40492241e0Tom Erickson		break;
40592241e0Tom Erickson	case ZPROP_SRC_RECEIVED:
40692241e0Tom Erickson		/*
40792241e0Tom Erickson		 * If there's no local setting, then the new received value will
40892241e0Tom Erickson		 * be the effective value.
40992241e0Tom Erickson		 */
4103b2aab1Matthew Ahrens		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
41192241e0Tom Erickson		if (err == ENOENT)
4123b2aab1Matthew Ahrens			*newvalp = value;
41392241e0Tom Erickson		break;
41492241e0Tom Erickson	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
41592241e0Tom Erickson		/*
41692241e0Tom Erickson		 * We're clearing the received value, so the local setting (if
41792241e0Tom Erickson		 * it exists) remains the effective value.
41892241e0Tom Erickson		 */
4193b2aab1Matthew Ahrens		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
42092241e0Tom Erickson		if (err == ENOENT)
4213b2aab1Matthew Ahrens			*newvalp = 0;
42292241e0Tom Erickson		break;
42392241e0Tom Erickson	default:
4243b2aab1Matthew Ahrens		panic("unexpected property source: %d", source);
42592241e0Tom Erickson	}
42692241e0Tom Erickson
42792241e0Tom Erickson	strfree(recvdstr);
42892241e0Tom Erickson
42992241e0Tom Erickson	if (err == ENOENT)
43092241e0Tom Erickson		return (0);
43192241e0Tom Erickson
43292241e0Tom Erickson	return (err);
43392241e0Tom Erickson}
43492241e0Tom Erickson
43603bad06Justin Gibbs * Unregister all callbacks that are registered with the
43703bad06Justin Gibbs * given callback argument.
438fa9e406ahrens */
43903bad06Justin Gibbsvoid
44003bad06Justin Gibbsdsl_prop_unregister_all(dsl_dataset_t *ds, void *cbarg)
44203bad06Justin Gibbs	dsl_prop_cb_record_t *cbr, *next_cbr;
44303bad06Justin Gibbs
44499653d4eschrock	dsl_dir_t *dd = ds->ds_dir;
446fa9e406ahrens	mutex_enter(&dd->dd_lock);
44703bad06Justin Gibbs	next_cbr = list_head(&ds->ds_prop_cbs);
44803bad06Justin Gibbs	while (next_cbr != NULL) {
44903bad06Justin Gibbs		cbr = next_cbr;
45003bad06Justin Gibbs		next_cbr = list_next(&ds->ds_prop_cbs, cbr);
45103bad06Justin Gibbs		if (cbr->cbr_arg == cbarg) {
45203bad06Justin Gibbs			list_remove(&ds->ds_prop_cbs, cbr);
45303bad06Justin Gibbs			list_remove(&cbr->cbr_pr->pr_cbs, cbr);
45403bad06Justin Gibbs			kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
45503bad06Justin Gibbs		}
456fa9e406ahrens	}
457fa9e406ahrens	mutex_exit(&dd->dd_lock);
4603b2aab1Matthew Ahrensboolean_t
4613b2aab1Matthew Ahrensdsl_prop_hascb(dsl_dataset_t *ds)
46303bad06Justin Gibbs	return (!list_is_empty(&ds->ds_prop_cbs));
4643b2aab1Matthew Ahrens}
4663b2aab1Matthew Ahrens/* ARGSUSED */
4673b2aab1Matthew Ahrensstatic int
4683b2aab1Matthew Ahrensdsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
4693b2aab1Matthew Ahrens{
4703b2aab1Matthew Ahrens	dsl_dir_t *dd = ds->ds_dir;
47103bad06Justin Gibbs	dsl_prop_record_t *pr;
4723b2aab1Matthew Ahrens	dsl_prop_cb_record_t *cbr;
4733b2aab1Matthew Ahrens
4743b2aab1Matthew Ahrens	mutex_enter(&dd->dd_lock);
47503bad06Justin Gibbs	for (pr = list_head(&dd->dd_props);
47603bad06Justin Gibbs	    pr; pr = list_next(&dd->dd_props, pr)) {
47703bad06Justin Gibbs		for (cbr = list_head(&pr->pr_cbs); cbr;
47803bad06Justin Gibbs		    cbr = list_next(&pr->pr_cbs, cbr)) {
47903bad06Justin Gibbs			uint64_t value;
4803b2aab1Matthew Ahrens
48103bad06Justin Gibbs			/*
48203bad06Justin Gibbs			 * Callback entries do not have holds on their
48303bad06Justin Gibbs			 * datasets so that datasets with registered
48403bad06Justin Gibbs			 * callbacks are still eligible for eviction.
48503bad06Justin Gibbs			 * Unlike operations to update properties on a
48603bad06Justin Gibbs			 * single dataset, we are performing a recursive
48703bad06Justin Gibbs			 * descent of related head datasets.  The caller
48803bad06Justin Gibbs			 * of this function only has a dataset hold on
48903bad06Justin Gibbs			 * the passed in head dataset, not the snapshots
49003bad06Justin Gibbs			 * associated with this dataset.  Without a hold,
49103bad06Justin Gibbs			 * the dataset pointer within callback records
49203bad06Justin Gibbs			 * for snapshots can be invalidated by eviction
49303bad06Justin Gibbs			 * at any time.
49403bad06Justin Gibbs			 *
49503bad06Justin Gibbs			 * Use dsl_dataset_try_add_ref() to verify
49603bad06Justin Gibbs			 * that the dataset for a snapshot has not
49703bad06Justin Gibbs			 * begun eviction processing and to prevent
49803bad06Justin Gibbs			 * eviction from occurring for the duration of
49903bad06Justin Gibbs			 * the callback.  If the hold attempt fails,
50003bad06Justin Gibbs			 * this object is already being evicted and the
50103bad06Justin Gibbs			 * callback can be safely ignored.
50203bad06Justin Gibbs			 */
50303bad06Justin Gibbs			if (ds != cbr->cbr_ds &&
50403bad06Justin Gibbs			    !dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
50503bad06Justin Gibbs				continue;
506e57a022Justin T. Gibbs
50703bad06Justin Gibbs			if (dsl_prop_get_ds(cbr->cbr_ds,
50803bad06Justin Gibbs			    cbr->cbr_pr->pr_propname, sizeof (value), 1,
50903bad06Justin Gibbs			    &value, NULL) == 0)
51003bad06Justin Gibbs				cbr->cbr_func(cbr->cbr_arg, value);
511e57a022Justin T. Gibbs
51203bad06Justin Gibbs			if (ds != cbr->cbr_ds)
51303bad06Justin Gibbs				dsl_dataset_rele(cbr->cbr_ds, FTAG);
51403bad06Justin Gibbs		}
5153b2aab1Matthew Ahrens	}
5163b2aab1Matthew Ahrens	mutex_exit(&dd->dd_lock);
5173b2aab1Matthew Ahrens
5183b2aab1Matthew Ahrens	return (0);
5193b2aab1Matthew Ahrens}
5203b2aab1Matthew Ahrens
5213b2aab1Matthew Ahrens/*
5223b2aab1Matthew Ahrens * Update all property values for ddobj & its descendants.  This is used
5233b2aab1Matthew Ahrens * when renaming the dir.
5243b2aab1Matthew Ahrens */
5253b2aab1Matthew Ahrensvoid
5263b2aab1Matthew Ahrensdsl_prop_notify_all(dsl_dir_t *dd)
5273b2aab1Matthew Ahrens{
5283b2aab1Matthew Ahrens	dsl_pool_t *dp = dd->dd_pool;
5293b2aab1Matthew Ahrens	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
5303b2aab1Matthew Ahrens	(void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb,
5313b2aab1Matthew Ahrens	    NULL, DS_FIND_CHILDREN);
534fa9e406ahrensstatic void
535fa9e406ahrensdsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
536fa9e406ahrens    const char *propname, uint64_t value, int first)
538fa9e406ahrens	dsl_dir_t *dd;
53903bad06Justin Gibbs	dsl_prop_record_t *pr;
540fa9e406ahrens	dsl_prop_cb_record_t *cbr;
541fa9e406ahrens	objset_t *mos = dp->dp_meta_objset;
5421d452cfahrens	zap_cursor_t zc;
543d8d7720ahrens	zap_attribute_t *za;
544fa9e406ahrens	int err;
5463b2aab1Matthew Ahrens	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
5473b2aab1Matthew Ahrens	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
548ea8dc4beschrock	if (err)
549ea8dc4beschrock		return;
551fa9e406ahrens	if (!first) {
552fa9e406ahrens		/*
553fa9e406ahrens		 * If the prop is set here, then this change is not
554fa9e406ahrens		 * being inherited here or below; stop the recursion.
555fa9e406ahrens		 */
556c137962Justin T. Gibbs		err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
557c137962Justin T. Gibbs		    propname);
558fa9e406ahrens		if (err == 0) {
5593b2aab1Matthew Ahrens			dsl_dir_rele(dd, FTAG);
560fa9e406ahrens			return;
561fa9e406ahrens		}
562fa9e406ahrens		ASSERT3U(err, ==, ENOENT);
563fa9e406ahrens	}
565fa9e406ahrens	mutex_enter(&dd->dd_lock);
56603bad06Justin Gibbs	pr = dsl_prop_record_find(dd, propname);
56703bad06Justin Gibbs	if (pr != NULL) {
56803bad06Justin Gibbs		for (cbr = list_head(&pr->pr_cbs); cbr;
56903bad06Justin Gibbs		    cbr = list_next(&pr->pr_cbs, cbr)) {
57003bad06Justin Gibbs			uint64_t propobj;
57203bad06Justin Gibbs			/*
57303bad06Justin Gibbs			 * cbr->cbr_ds may be invalidated due to eviction,
57403bad06Justin Gibbs			 * requiring the use of dsl_dataset_try_add_ref().
57503bad06Justin Gibbs			 * See comment block in dsl_prop_notify_all_cb()
57603bad06Justin Gibbs			 * for details.
57703bad06Justin Gibbs			 */
57803bad06Justin Gibbs			if (!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
57903bad06Justin Gibbs				continue;
58103bad06Justin Gibbs			propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
582e57a022Justin T. Gibbs
58303bad06Justin Gibbs			/*
58403bad06Justin Gibbs			 * If the property is not set on this ds, then it is
58503bad06Justin Gibbs			 * inherited here; call the callback.
58603bad06Justin Gibbs			 */
58703bad06Justin Gibbs			if (propobj == 0 ||
58803bad06Justin Gibbs			    zap_contains(mos, propobj, propname) != 0)
58903bad06Justin Gibbs				cbr->cbr_func(cbr->cbr_arg, value);
59103bad06Justin Gibbs			dsl_dataset_rele(cbr->cbr_ds, FTAG);
59203bad06Justin Gibbs		}
593fa9e406ahrens	}
594fa9e406ahrens	mutex_exit(&dd->dd_lock);
596d8d7720ahrens	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
5971d452cfahrens	for (zap_cursor_init(&zc, mos,
598c137962Justin T. Gibbs	    dsl_dir_phys(dd)->dd_child_dir_zapobj);
599d8d7720ahrens	    zap_cursor_retrieve(&zc, za) == 0;
6001d452cfahrens	    zap_cursor_advance(&zc)) {
601d8d7720ahrens		dsl_prop_changed_notify(dp, za->za_first_integer,
6021d452cfahrens		    propname, value, FALSE);
603fa9e406ahrens	}
604d8d7720ahrens	kmem_free(za, sizeof (zap_attribute_t));
6051d452cfahrens	zap_cursor_fini(&zc);
6063b2aab1Matthew Ahrens	dsl_dir_rele(dd, FTAG);
60992241e0Tom Ericksonvoid
6103b2aab1Matthew Ahrensdsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname,
6113b2aab1Matthew Ahrens    zprop_source_t source, int intsz, int numints, const void *value,
6123b2aab1Matthew Ahrens    dmu_tx_t *tx)
614bb0ade0ahrens	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
61592241e0Tom Erickson	uint64_t zapobj, intval, dummy;
6161d452cfahrens	int isint;
617ecd6cf8marks	char valbuf[32];
6183b2aab1Matthew Ahrens	const char *valstr = NULL;
61992241e0Tom Erickson	char *inheritstr;
62092241e0Tom Erickson	char *recvdstr;
62192241e0Tom Erickson	char *tbuf = NULL;
62292241e0Tom Erickson	int err;
62392241e0Tom Erickson	uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa);
625d09e447Alex Wilson	isint = (dodefault(zfs_name_to_prop(propname), 8, 1, &intval) == 0);
627bc9014eJustin Gibbs	if (ds->ds_is_snapshot) {
62892241e0Tom Erickson		ASSERT(version >= SPA_VERSION_SNAP_PROPS);
629c137962Justin T. Gibbs		if (dsl_dataset_phys(ds)->ds_props_obj == 0) {
630bb0ade0ahrens			dmu_buf_will_dirty(ds->ds_dbuf, tx);
631c137962Justin T. Gibbs			dsl_dataset_phys(ds)->ds_props_obj =
632bb0ade0ahrens			    zap_create(mos,
633bb0ade0ahrens			    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
634bb0ade0ahrens		}
635c137962Justin T. Gibbs		zapobj = dsl_dataset_phys(ds)->ds_props_obj;
636bb0ade0ahrens	} else {
637c137962Justin T. Gibbs		zapobj = dsl_dir_phys(ds->ds_dir)->dd_props_zapobj;
638bb0ade0ahrens	}
64092241e0Tom Erickson	if (version < SPA_VERSION_RECVD_PROPS) {
64192241e0Tom Erickson		if (source & ZPROP_SRC_NONE)
64292241e0Tom Erickson			source = ZPROP_SRC_NONE;
64392241e0Tom Erickson		else if (source & ZPROP_SRC_RECEIVED)
64492241e0Tom Erickson			source = ZPROP_SRC_LOCAL;
64592241e0Tom Erickson	}
64692241e0Tom Erickson
64792241e0Tom Erickson	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
64892241e0Tom Erickson	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
64992241e0Tom Erickson
65092241e0Tom Erickson	switch (source) {
65192241e0Tom Erickson	case ZPROP_SRC_NONE:
65292241e0Tom Erickson		/*
65392241e0Tom Erickson		 * revert to received value, if any (inherit -S)
65492241e0Tom Erickson		 * - remove propname
65592241e0Tom Erickson		 * - remove propname$inherit
65692241e0Tom Erickson		 */
65792241e0Tom Erickson		err = zap_remove(mos, zapobj, propname, tx);
65892241e0Tom Erickson		ASSERT(err == 0 || err == ENOENT);
65992241e0Tom Erickson		err = zap_remove(mos, zapobj, inheritstr, tx);
66092241e0Tom Erickson		ASSERT(err == 0 || err == ENOENT);
66192241e0Tom Erickson		break;
66292241e0Tom Erickson	case ZPROP_SRC_LOCAL:
66392241e0Tom Erickson		/*
66492241e0Tom Erickson		 * remove propname$inherit
66592241e0Tom Erickson		 * set propname -> value
66692241e0Tom Erickson		 */
66792241e0Tom Erickson		err = zap_remove(mos, zapobj, inheritstr, tx);
66892241e0Tom Erickson		ASSERT(err == 0 || err == ENOENT);
6693b2aab1Matthew Ahrens		VERIFY0(zap_update(mos, zapobj, propname,
6703b2aab1Matthew Ahrens		    intsz, numints, value, tx));
67192241e0Tom Erickson		break;
67292241e0Tom Erickson	case ZPROP_SRC_INHERITED:
67392241e0Tom Erickson		/*
67492241e0Tom Erickson		 * explicitly inherit
67592241e0Tom Erickson		 * - remove propname
67692241e0Tom Erickson		 * - set propname$inherit
67792241e0Tom Erickson		 */
67892241e0Tom Erickson		err = zap_remove(mos, zapobj, propname, tx);
6791d452cfahrens		ASSERT(err == 0 || err == ENOENT);
68092241e0Tom Erickson		if (version >= SPA_VERSION_RECVD_PROPS &&
6813b2aab1Matthew Ahrens		    dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) {
68292241e0Tom Erickson			dummy = 0;
6833b2aab1Matthew Ahrens			VERIFY0(zap_update(mos, zapobj, inheritstr,
6843b2aab1Matthew Ahrens			    8, 1, &dummy, tx));
685fa9e406ahrens		}
68692241e0Tom Erickson		break;
68792241e0Tom Erickson	case ZPROP_SRC_RECEIVED:
68892241e0Tom Erickson		/*
68992241e0Tom Erickson		 * set propname$recvd -> value
69092241e0Tom Erickson		 */
69192241e0Tom Erickson		err = zap_update(mos, zapobj, recvdstr,
6923b2aab1Matthew Ahrens		    intsz, numints, value, tx);
69392241e0Tom Erickson		ASSERT(err == 0);
69492241e0Tom Erickson		break;
69692241e0Tom Erickson		/*
69792241e0Tom Erickson		 * clear local and received settings
69892241e0Tom Erickson		 * - remove propname
69992241e0Tom Erickson		 * - remove propname$inherit
70092241e0Tom Erickson		 * - remove propname$recvd
70192241e0Tom Erickson		 */
70292241e0Tom Erickson		err = zap_remove(mos, zapobj, propname, tx);
70392241e0Tom Erickson		ASSERT(err == 0 || err == ENOENT);
70492241e0Tom Erickson		err = zap_remove(mos, zapobj, inheritstr, tx);
70592241e0Tom Erickson		ASSERT(err == 0 || err == ENOENT);
70692241e0Tom Erickson		/* FALLTHRU */
70792241e0Tom Erickson	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
70892241e0Tom Erickson		/*
70992241e0Tom Erickson		 * remove propname$recvd
71092241e0Tom Erickson		 */
71192241e0Tom Erickson		err = zap_remove(mos, zapobj, recvdstr, tx);
71292241e0Tom Erickson		ASSERT(err == 0 || err == ENOENT);
71392241e0Tom Erickson		break;
71492241e0Tom Erickson	default:
71592241e0Tom Erickson		cmn_err(CE_PANIC, "unexpected property source: %d", source);
716fa9e406ahrens	}
71892241e0Tom Erickson	strfree(inheritstr);
71992241e0Tom Erickson	strfree(recvdstr);
72092241e0Tom Erickson
7211d452cfahrens	if (isint) {
7223b2aab1Matthew Ahrens		VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval));
72392241e0Tom Erickson
724bc9014eJustin Gibbs		if (ds->ds_is_snapshot) {
725bb0ade0ahrens			dsl_prop_cb_record_t *cbr;
726bb0ade0ahrens			/*
727bb0ade0ahrens			 * It's a snapshot; nothing can inherit this
728bb0ade0ahrens			 * property, so just look for callbacks on this
729bb0ade0ahrens			 * ds here.
730bb0ade0ahrens			 */
731bb0ade0ahrens			mutex_enter(&ds->ds_dir->dd_lock);
73203bad06Justin Gibbs			for (cbr = list_head(&ds->ds_prop_cbs); cbr;
73303bad06Justin Gibbs			    cbr = list_next(&ds->ds_prop_cbs, cbr)) {
73403bad06Justin Gibbs				if (strcmp(cbr->cbr_pr->pr_propname,
73503bad06Justin Gibbs				    propname) == 0)
736bb0ade0ahrens					cbr->cbr_func(cbr->cbr_arg, intval);
737bb0ade0ahrens			}
738bb0ade0ahrens			mutex_exit(&ds->ds_dir->dd_lock);
739bb0ade0ahrens		} else {
740bb0ade0ahrens			dsl_prop_changed_notify(ds->ds_dir->dd_pool,
74192241e0Tom Erickson			    ds->ds_dir->dd_object, propname, intval, TRUE);
742bb0ade0ahrens		}
74392241e0Tom Erickson
744ecd6cf8marks		(void) snprintf(valbuf, sizeof (valbuf),
745ecd6cf8marks		    "%lld", (longlong_t)intval);
746ecd6cf8marks		valstr = valbuf;
747ecd6cf8marks	} else {
74892241e0Tom Erickson		if (source == ZPROP_SRC_LOCAL) {
7493b2aab1Matthew Ahrens			valstr = value;
75092241e0Tom Erickson		} else {
75192241e0Tom Erickson			tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
75292241e0Tom Erickson			if (dsl_prop_get_ds(ds, propname, 1,
75392241e0Tom Erickson			    ZAP_MAXVALUELEN, tbuf, NULL) == 0)
75492241e0Tom Erickson				valstr = tbuf;
75592241e0Tom Erickson		}
756ecd6cf8marks	}
75792241e0Tom Erickson
7584445fffMatthew Ahrens	spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE ||
7594445fffMatthew Ahrens	    source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx,
7604445fffMatthew Ahrens	    "%s=%s", propname, (valstr == NULL ? "" : valstr));
76192241e0Tom Erickson
76292241e0Tom Erickson	if (tbuf != NULL)
76392241e0Tom Erickson		kmem_free(tbuf, ZAP_MAXVALUELEN);
7663b2aab1Matthew Ahrensint
7673b2aab1Matthew Ahrensdsl_prop_set_int(const char *dsname, const char *propname,
7683b2aab1Matthew Ahrens    zprop_source_t source, uint64_t value)
7695c0b6a7Rich Morris{
7703b2aab1Matthew Ahrens	nvlist_t *nvl = fnvlist_alloc();
7713b2aab1Matthew Ahrens	int error;
77292241e0Tom Erickson
7733b2aab1Matthew Ahrens	fnvlist_add_uint64(nvl, propname, value);
7743b2aab1Matthew Ahrens	error = dsl_props_set(dsname, source, nvl);
7753b2aab1Matthew Ahrens	fnvlist_free(nvl);
7763b2aab1Matthew Ahrens	return (error);
7775c0b6a7Rich Morris}
7785c0b6a7Rich Morris
7803b2aab1Matthew Ahrensdsl_prop_set_string(const char *dsname, const char *propname,
7813b2aab1Matthew Ahrens    zprop_source_t source, const char *value)
7833b2aab1Matthew Ahrens	nvlist_t *nvl = fnvlist_alloc();
7843b2aab1Matthew Ahrens	int error;
7863b2aab1Matthew Ahrens	fnvlist_add_string(nvl, propname, value);
7873b2aab1Matthew Ahrens	error = dsl_props_set(dsname, source, nvl);
7883b2aab1Matthew Ahrens	fnvlist_free(nvl);
7893b2aab1Matthew Ahrens	return (error);
7903b2aab1Matthew Ahrens}
79192241e0Tom Erickson
7923b2aab1Matthew Ahrensint
7933b2aab1Matthew Ahrensdsl_prop_inherit(const char *dsname, const char *propname,
7943b2aab1Matthew Ahrens    zprop_source_t source)
7953b2aab1Matthew Ahrens{
7963b2aab1Matthew Ahrens	nvlist_t *nvl = fnvlist_alloc();
7973b2aab1Matthew Ahrens	int error;
7993b2aab1Matthew Ahrens	fnvlist_add_boolean(nvl, propname);
8003b2aab1Matthew Ahrens	error = dsl_props_set(dsname, source, nvl);
8013b2aab1Matthew Ahrens	fnvlist_free(nvl);
8023b2aab1Matthew Ahrens	return (error);
8052d85dedJason Kingint
8063b2aab1Matthew Ahrensdsl_props_set_check(void *arg, dmu_tx_t *tx)
8075c0b6a7Rich Morris{
8083b2aab1Matthew Ahrens	dsl_props_set_arg_t *dpsa = arg;
8093b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
8105c0b6a7Rich Morris	dsl_dataset_t *ds;
811478ed9aEric Taylor	uint64_t version;
8125c0b6a7Rich Morris	nvpair_t *elem = NULL;
8135c0b6a7Rich Morris	int err;
8145c0b6a7Rich Morris
8153b2aab1Matthew Ahrens	err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds);
8163b2aab1Matthew Ahrens	if (err != 0)
817478ed9aEric Taylor		return (err);
8183b2aab1Matthew Ahrens
819478ed9aEric Taylor	version = spa_version(ds->ds_dir->dd_pool->dp_spa);
8203b2aab1Matthew Ahrens	while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) {
821478ed9aEric Taylor		if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
822478ed9aEric Taylor			dsl_dataset_rele(ds, FTAG);
823be6fd75Matthew Ahrens			return (SET_ERROR(ENAMETOOLONG));
824478ed9aEric Taylor		}
8255c0b6a7Rich Morris		if (nvpair_type(elem) == DATA_TYPE_STRING) {
8263b2aab1Matthew Ahrens			char *valstr = fnvpair_value_string(elem);
827478ed9aEric Taylor			if (strlen(valstr) >= (version <
828478ed9aEric Taylor			    SPA_VERSION_STMF_PROP ?
829478ed9aEric Taylor			    ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) {
830478ed9aEric Taylor				dsl_dataset_rele(ds, FTAG);
831ccba080Rich Morris				return (E2BIG);
832478ed9aEric Taylor			}
8335c0b6a7Rich Morris		}
8345c0b6a7Rich Morris	}
8355c0b6a7Rich Morris
836bc9014eJustin Gibbs	if (ds->ds_is_snapshot && version < SPA_VERSION_SNAP_PROPS) {
8375c0b6a7Rich Morris		dsl_dataset_rele(ds, FTAG);
838be6fd75Matthew Ahrens		return (SET_ERROR(ENOTSUP));
8395c0b6a7Rich Morris	}
8403b2aab1Matthew Ahrens	dsl_dataset_rele(ds, FTAG);
8413b2aab1Matthew Ahrens	return (0);
8423b2aab1Matthew Ahrens}
8435c0b6a7Rich Morris
8443b2aab1Matthew Ahrensvoid
8453b2aab1Matthew Ahrensdsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source,
8463b2aab1Matthew Ahrens    nvlist_t *props, dmu_tx_t *tx)
8473b2aab1Matthew Ahrens{
8483b2aab1Matthew Ahrens	nvpair_t *elem = NULL;
84992241e0Tom Erickson
8503b2aab1Matthew Ahrens	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
8513b2aab1Matthew Ahrens		nvpair_t *pair = elem;
85285723e5loli		const char *name = nvpair_name(pair);
8535c0b6a7Rich Morris
8543b2aab1Matthew Ahrens		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
8553b2aab1Matthew Ahrens			/*
85685723e5loli			 * This usually happens when we reuse the nvlist_t data
85785723e5loli			 * returned by the counterpart dsl_prop_get_all_impl().
85885723e5loli			 * For instance we do this to restore the original
85985723e5loli			 * received properties when an error occurs in the
86085723e5loli			 * zfs_ioc_recv() codepath.
8613b2aab1Matthew Ahrens			 */
8623b2aab1Matthew Ahrens			nvlist_t *attrs = fnvpair_value_nvlist(pair);
8633b2aab1Matthew Ahrens			pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);