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.
23*40a5c998SMatthew Ahrens * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved.
25d09e4475SAlex Wilson * Copyright 2015, Joyent, Inc.
26fa9e4066Sahrens */
27fa9e4066Sahrens
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>
39fa9e4066Sahrens
40fa9e4066Sahrens #include "zfs_prop.h"
41fa9e4066Sahrens
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));
56fa9e4066Sahrens
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));
65fa9e4066Sahrens
66fa9e4066Sahrens *(uint64_t *)buf = zfs_prop_default_numeric(prop);
67fa9e4066Sahrens }
68fa9e4066Sahrens
69fa9e4066Sahrens return (0);
70fa9e4066Sahrens }
71fa9e4066Sahrens
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;
84fa9e4066Sahrens
853b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dd->dd_pool));
86bb0ade09Sahrens
87fa9e4066Sahrens if (setpoint)
88fa9e4066Sahrens setpoint[0] = '\0';
89fa9e4066Sahrens
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);
94e9dbad6fSeschrock
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 }
114e9dbad6fSeschrock
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)
12292241e0bSTom Erickson 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 }
137e9dbad6fSeschrock break;
138fa9e4066Sahrens }
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);
14892241e0bSTom Erickson }
14992241e0bSTom Erickson
150fa9e4066Sahrens if (err == ENOENT)
151d09e4475SAlex Wilson err = dodefault(prop, intsz, numints, buf);
15292241e0bSTom Erickson
15392241e0bSTom Erickson strfree(inheritstr);
15492241e0bSTom Erickson strfree(recvdstr);
155fa9e4066Sahrens
156fa9e4066Sahrens return (err);
157fa9e4066Sahrens }
158fa9e4066Sahrens
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;
166bb0ade09Sahrens
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;
17492241e0bSTom Erickson
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 }
213bb0ade09Sahrens
214bb0ade09Sahrens return (dsl_prop_get_dd(ds->ds_dir, propname,
215bc9014e6SJustin Gibbs intsz, numints, buf, setpoint, ds->ds_is_snapshot));
216bb0ade09Sahrens }
217bb0ade09Sahrens
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;
287fa9e4066Sahrens
2883b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp));
289fa9e4066Sahrens
2903b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, propname, &value);
2913b2aab18SMatthew Ahrens if (err != 0)
292fa9e4066Sahrens return (err);
293fa9e4066Sahrens
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);
307fa9e4066Sahrens
308fa9e4066Sahrens cbr->cbr_func(cbr->cbr_arg, value);
309fa9e4066Sahrens return (0);
310fa9e4066Sahrens }
311fa9e4066Sahrens
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;
318fa9e4066Sahrens
3193b2aab18SMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os);
3203b2aab18SMatthew Ahrens if (error != 0)
3213b2aab18SMatthew Ahrens return (error);
322fa9e4066Sahrens
3233b2aab18SMatthew Ahrens error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
3243b2aab18SMatthew Ahrens intsz, numints, buf, setpoint);
325fa9e4066Sahrens
3263b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG);
3273b2aab18SMatthew Ahrens return (error);
328fa9e4066Sahrens }
329fa9e4066Sahrens
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 }
344fa9e4066Sahrens
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;
445fa9e4066Sahrens
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);
454fa9e4066Sahrens kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
45503bad06fSJustin Gibbs }
45603bad06fSJustin Gibbs }
45703bad06fSJustin Gibbs mutex_exit(&dd->dd_lock);
458fa9e4066Sahrens }
459fa9e4066Sahrens
4603b2aab18SMatthew Ahrens boolean_t
dsl_prop_hascb(dsl_dataset_t * ds)4613b2aab18SMatthew Ahrens dsl_prop_hascb(dsl_dataset_t *ds)
4623b2aab18SMatthew Ahrens {
46303bad06fSJustin Gibbs return (!list_is_empty(&ds->ds_prop_cbs));
4643b2aab18SMatthew Ahrens }
4653b2aab18SMatthew 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)
46999653d4eSeschrock {
47099653d4eSeschrock dsl_dir_t *dd = ds->ds_dir;
47103bad06fSJustin Gibbs dsl_prop_record_t *pr;
47299653d4eSeschrock dsl_prop_cb_record_t *cbr;
47399653d4eSeschrock
47499653d4eSeschrock 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)) {
4793b2aab18SMatthew Ahrens uint64_t value;
4803b2aab18SMatthew Ahrens
481e57a022bSJustin T. 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.
494e57a022bSJustin T. 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.
502e57a022bSJustin T. Gibbs */
50303bad06fSJustin Gibbs if (ds != cbr->cbr_ds &&
50403bad06fSJustin Gibbs !dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
505e57a022bSJustin T. 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)
5103b2aab18SMatthew Ahrens cbr->cbr_func(cbr->cbr_arg, value);
511e57a022bSJustin T. Gibbs
51203bad06fSJustin Gibbs if (ds != cbr->cbr_ds)
513e57a022bSJustin T. Gibbs dsl_dataset_rele(cbr->cbr_ds, FTAG);
51499653d4eSeschrock }
51503bad06fSJustin Gibbs }
51699653d4eSeschrock mutex_exit(&dd->dd_lock);
51799653d4eSeschrock
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 }
53399653d4eSeschrock
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;
545fa9e4066Sahrens
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;
550fa9e4066Sahrens
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 }
564fa9e4066Sahrens
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)) {
570e57a022bSJustin T. Gibbs uint64_t propobj;
571bb0ade09Sahrens
572bb0ade09Sahrens /*
57303bad06fSJustin Gibbs * cbr->cbr_ds may be invalidated due to eviction,
574e57a022bSJustin T. Gibbs * requiring the use of dsl_dataset_try_add_ref().
575e57a022bSJustin T. Gibbs * See comment block in dsl_prop_notify_all_cb()
576e57a022bSJustin T. Gibbs * for details.
577bb0ade09Sahrens */
57803bad06fSJustin Gibbs if (!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
579bb0ade09Sahrens continue;
580bb0ade09Sahrens
581e57a022bSJustin T. Gibbs propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
582e57a022bSJustin T. Gibbs
583e57a022bSJustin T. Gibbs /*
584e57a022bSJustin T. Gibbs * If the property is not set on this ds, then it is
585e57a022bSJustin T. Gibbs * inherited here; call the callback.
586e57a022bSJustin T. Gibbs */
58703bad06fSJustin Gibbs if (propobj == 0 ||
58803bad06fSJustin Gibbs zap_contains(mos, propobj, propname) != 0)
589fa9e4066Sahrens cbr->cbr_func(cbr->cbr_arg, value);
590e57a022bSJustin T. Gibbs
591e57a022bSJustin T. Gibbs dsl_dataset_rele(cbr->cbr_ds, FTAG);
592fa9e4066Sahrens }
59303bad06fSJustin Gibbs }
594fa9e4066Sahrens mutex_exit(&dd->dd_lock);
595fa9e4066Sahrens
596d8d77200Sahrens za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
597fa9e4066Sahrens for (zap_cursor_init(&zc, mos,
598c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj);
599d8d77200Sahrens zap_cursor_retrieve(&zc, za) == 0;
600fa9e4066Sahrens zap_cursor_advance(&zc)) {
601d8d77200Sahrens dsl_prop_changed_notify(dp, za->za_first_integer,
602fa9e4066Sahrens propname, value, FALSE);
603fa9e4066Sahrens }
604d8d77200Sahrens kmem_free(za, sizeof (zap_attribute_t));
60587e5029aSahrens zap_cursor_fini(&zc);
6063b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
607fa9e4066Sahrens }
608fa9e4066Sahrens
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);
624fa9e4066Sahrens
625d09e4475SAlex Wilson isint = (dodefault(zfs_name_to_prop(propname), 8, 1, &intval) == 0);
626fa9e4066Sahrens
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 }
639bb0ade09Sahrens
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;
645fa9e4066Sahrens }
646fa9e4066Sahrens
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);
67992241e0bSTom Erickson 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));
68592241e0bSTom Erickson }
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;
69592241e0bSTom Erickson case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED):
69692241e0bSTom Erickson /*
69792241e0bSTom Erickson * clear local and received settings
69892241e0bSTom Erickson * - remove propname
69992241e0bSTom Erickson * - remove propname$inherit
70092241e0bSTom Erickson * - remove propname$recvd
70192241e0bSTom Erickson */
70292241e0bSTom Erickson err = zap_remove(mos, zapobj, propname, tx);
70392241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT);
70492241e0bSTom Erickson err = zap_remove(mos, zapobj, inheritstr, tx);
70592241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT);
70692241e0bSTom Erickson /* FALLTHRU */
70792241e0bSTom Erickson case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
70892241e0bSTom Erickson /*
70992241e0bSTom Erickson * remove propname$recvd
71092241e0bSTom Erickson */
71192241e0bSTom Erickson err = zap_remove(mos, zapobj, recvdstr, tx);
71292241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT);
71392241e0bSTom Erickson break;
71492241e0bSTom Erickson default:
71592241e0bSTom Erickson cmn_err(CE_PANIC, "unexpected property source: %d", source);
71692241e0bSTom Erickson }
71792241e0bSTom Erickson
71892241e0bSTom Erickson strfree(inheritstr);
71992241e0bSTom Erickson strfree(recvdstr);
72092241e0bSTom Erickson
7211d452cf5Sahrens if (isint) {
7223b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval));
72392241e0bSTom Erickson
724bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) {
725bb0ade09Sahrens dsl_prop_cb_record_t *cbr;
726bb0ade09Sahrens /*
727bb0ade09Sahrens * It's a snapshot; nothing can inherit this
728bb0ade09Sahrens * property, so just look for callbacks on this
729bb0ade09Sahrens * ds here.
730bb0ade09Sahrens */
731bb0ade09Sahrens mutex_enter(&ds->ds_dir->dd_lock);
73203bad06fSJustin Gibbs for (cbr = list_head(&ds->ds_prop_cbs); cbr;
73303bad06fSJustin Gibbs cbr = list_next(&ds->ds_prop_cbs, cbr)) {
73403bad06fSJustin Gibbs if (strcmp(cbr->cbr_pr->pr_propname,
73503bad06fSJustin Gibbs propname) == 0)
736bb0ade09Sahrens cbr->cbr_func(cbr->cbr_arg, intval);
737bb0ade09Sahrens }
738bb0ade09Sahrens mutex_exit(&ds->ds_dir->dd_lock);
739bb0ade09Sahrens } else {
740bb0ade09Sahrens dsl_prop_changed_notify(ds->ds_dir->dd_pool,
74192241e0bSTom Erickson ds->ds_dir->dd_object, propname, intval, TRUE);
742bb0ade09Sahrens }
74392241e0bSTom Erickson
744ecd6cf80Smarks (void) snprintf(valbuf, sizeof (valbuf),
745ecd6cf80Smarks "%lld", (longlong_t)intval);
746ecd6cf80Smarks valstr = valbuf;
747ecd6cf80Smarks } else {
74892241e0bSTom Erickson if (source == ZPROP_SRC_LOCAL) {
7493b2aab18SMatthew Ahrens valstr = value;
75092241e0bSTom Erickson } else {
75192241e0bSTom Erickson tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
75292241e0bSTom Erickson if (dsl_prop_get_ds(ds, propname, 1,
75392241e0bSTom Erickson ZAP_MAXVALUELEN, tbuf, NULL) == 0)
75492241e0bSTom Erickson valstr = tbuf;
755ecd6cf80Smarks }
75692241e0bSTom Erickson }
75792241e0bSTom Erickson
7584445fffbSMatthew Ahrens spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE ||
7594445fffbSMatthew Ahrens source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx,
7604445fffbSMatthew Ahrens "%s=%s", propname, (valstr == NULL ? "" : valstr));
76192241e0bSTom Erickson
76292241e0bSTom Erickson if (tbuf != NULL)
76392241e0bSTom Erickson kmem_free(tbuf, ZAP_MAXVALUELEN);
764fa9e4066Sahrens }
765fa9e4066Sahrens
7663b2aab18SMatthew Ahrens int
dsl_prop_set_int(const char * dsname,const char * propname,zprop_source_t source,uint64_t value)7673b2aab18SMatthew Ahrens dsl_prop_set_int(const char *dsname, const char *propname,
7683b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value)
7695c0b6a79SRich Morris {
7703b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc();
7713b2aab18SMatthew Ahrens int error;
7725c0b6a79SRich Morris
7733b2aab18SMatthew Ahrens fnvlist_add_uint64(nvl, propname, value);
7743b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl);
7753b2aab18SMatthew Ahrens fnvlist_free(nvl);
7763b2aab18SMatthew Ahrens return (error);
7775c0b6a79SRich Morris }
7785c0b6a79SRich Morris
779fa9e4066Sahrens int
dsl_prop_set_string(const char * dsname,const char * propname,zprop_source_t source,const char * value)7803b2aab18SMatthew Ahrens dsl_prop_set_string(const char *dsname, const char *propname,
7813b2aab18SMatthew Ahrens zprop_source_t source, const char *value)
782a2eea2e1Sahrens {
7833b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc();
7843b2aab18SMatthew Ahrens int error;
785fa9e4066Sahrens
7863b2aab18SMatthew Ahrens fnvlist_add_string(nvl, propname, value);
7873b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl);
7883b2aab18SMatthew Ahrens fnvlist_free(nvl);
7893b2aab18SMatthew Ahrens return (error);
790fa9e4066Sahrens }
7917f7322feSeschrock
7925c0b6a79SRich Morris int
dsl_prop_inherit(const char * dsname,const char * propname,zprop_source_t source)7933b2aab18SMatthew Ahrens dsl_prop_inherit(const char *dsname, const char *propname,
7943b2aab18SMatthew Ahrens zprop_source_t source)
7955c0b6a79SRich Morris {
7963b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc();
7973b2aab18SMatthew Ahrens int error;
7983b2aab18SMatthew Ahrens
7993b2aab18SMatthew Ahrens fnvlist_add_boolean(nvl, propname);
8003b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl);
8013b2aab18SMatthew Ahrens fnvlist_free(nvl);
8023b2aab18SMatthew Ahrens return (error);
8033b2aab18SMatthew Ahrens }
8043b2aab18SMatthew Ahrens
8053b2aab18SMatthew Ahrens typedef struct dsl_props_set_arg {
8063b2aab18SMatthew Ahrens const char *dpsa_dsname;
8073b2aab18SMatthew Ahrens zprop_source_t dpsa_source;
8083b2aab18SMatthew Ahrens nvlist_t *dpsa_props;
8093b2aab18SMatthew Ahrens } dsl_props_set_arg_t;
8103b2aab18SMatthew Ahrens
8113b2aab18SMatthew Ahrens static int
dsl_props_set_check(void * arg,dmu_tx_t * tx)8123b2aab18SMatthew Ahrens dsl_props_set_check(void *arg, dmu_tx_t *tx)
8133b2aab18SMatthew Ahrens {
8143b2aab18SMatthew Ahrens dsl_props_set_arg_t *dpsa = arg;
8153b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
8165c0b6a79SRich Morris dsl_dataset_t *ds;
817478ed9adSEric Taylor uint64_t version;
8185c0b6a79SRich Morris nvpair_t *elem = NULL;
8195c0b6a79SRich Morris int err;
8205c0b6a79SRich Morris
8213b2aab18SMatthew Ahrens err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds);
8223b2aab18SMatthew Ahrens if (err != 0)
823478ed9adSEric Taylor return (err);
8243b2aab18SMatthew Ahrens
825478ed9adSEric Taylor version = spa_version(ds->ds_dir->dd_pool->dp_spa);
8263b2aab18SMatthew Ahrens while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) {
827478ed9adSEric Taylor if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
828478ed9adSEric Taylor dsl_dataset_rele(ds, FTAG);
829be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
830478ed9adSEric Taylor }
8315c0b6a79SRich Morris if (nvpair_type(elem) == DATA_TYPE_STRING) {
8323b2aab18SMatthew Ahrens char *valstr = fnvpair_value_string(elem);
833478ed9adSEric Taylor if (strlen(valstr) >= (version <
834478ed9adSEric Taylor SPA_VERSION_STMF_PROP ?
835478ed9adSEric Taylor ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) {
836478ed9adSEric Taylor dsl_dataset_rele(ds, FTAG);
837ccba0801SRich Morris return (E2BIG);
8385c0b6a79SRich Morris }
8395c0b6a79SRich Morris }
840478ed9adSEric Taylor }
8415c0b6a79SRich Morris
842bc9014e6SJustin Gibbs if (ds->ds_is_snapshot && version < SPA_VERSION_SNAP_PROPS) {
8435c0b6a79SRich Morris dsl_dataset_rele(ds, FTAG);
844be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
8455c0b6a79SRich Morris }
8465c0b6a79SRich Morris dsl_dataset_rele(ds, FTAG);
8473b2aab18SMatthew Ahrens return (0);
8483b2aab18SMatthew Ahrens }
8493b2aab18SMatthew Ahrens
8503b2aab18SMatthew Ahrens void
dsl_props_set_sync_impl(dsl_dataset_t * ds,zprop_source_t source,nvlist_t * props,dmu_tx_t * tx)8513b2aab18SMatthew Ahrens dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source,
8523b2aab18SMatthew Ahrens nvlist_t *props, dmu_tx_t *tx)
8533b2aab18SMatthew Ahrens {
8543b2aab18SMatthew Ahrens nvpair_t *elem = NULL;
8553b2aab18SMatthew Ahrens
8563b2aab18SMatthew Ahrens while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
8573b2aab18SMatthew Ahrens nvpair_t *pair = elem;
8583b2aab18SMatthew Ahrens
8593b2aab18SMatthew Ahrens if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
8603b2aab18SMatthew Ahrens /*
8613b2aab18SMatthew Ahrens * dsl_prop_get_all_impl() returns properties in this
8623b2aab18SMatthew Ahrens * format.
8633b2aab18SMatthew Ahrens */
8643b2aab18SMatthew Ahrens nvlist_t *attrs = fnvpair_value_nvlist(pair);
8653b2aab18SMatthew Ahrens pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
8663b2aab18SMatthew Ahrens }
8673b2aab18SMatthew Ahrens
8683b2aab18SMatthew Ahrens if (nvpair_type(pair) == DATA_TYPE_STRING) {
8693b2aab18SMatthew Ahrens const char *value = fnvpair_value_string(pair);
8703b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair),
8713b2aab18SMatthew Ahrens source, 1, strlen(value) + 1, value, tx);
8723b2aab18SMatthew Ahrens } else if (nvpair_type(pair) == DATA_TYPE_UINT64) {
8733b2aab18SMatthew Ahrens uint64_t intval = fnvpair_value_uint64(pair);
8743b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair),
8753b2aab18SMatthew Ahrens source, sizeof (intval), 1, &intval, tx);
8763b2aab18SMatthew Ahrens } else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) {
8773b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair),
8783b2aab18SMatthew Ahrens source, 0, 0, NULL, tx);
8793b2aab18SMatthew Ahrens } else {
8803b2aab18SMatthew Ahrens panic("invalid nvpair type");
8813b2aab18SMatthew Ahrens }
8823b2aab18SMatthew Ahrens }
8833b2aab18SMatthew Ahrens }
8843b2aab18SMatthew Ahrens
8853b2aab18SMatthew Ahrens static void
dsl_props_set_sync(void * arg,dmu_tx_t * tx)8863b2aab18SMatthew Ahrens dsl_props_set_sync(void *arg, dmu_tx_t *tx)
8873b2aab18SMatthew Ahrens {
8883b2aab18SMatthew Ahrens dsl_props_set_arg_t *dpsa = arg;
8893b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
8903b2aab18SMatthew Ahrens dsl_dataset_t *ds;
8913b2aab18SMatthew Ahrens
8923b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds));
8933b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx);
8943b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
8953b2aab18SMatthew Ahrens }
8963b2aab18SMatthew Ahrens
8973b2aab18SMatthew Ahrens /*
8983b2aab18SMatthew Ahrens * All-or-nothing; if any prop can't be set, nothing will be modified.
8993b2aab18SMatthew Ahrens */
9003b2aab18SMatthew Ahrens int
dsl_props_set(const char * dsname,zprop_source_t source,nvlist_t * props)9013b2aab18SMatthew Ahrens dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props)
9023b2aab18SMatthew Ahrens {
9033b2aab18SMatthew Ahrens dsl_props_set_arg_t dpsa;
9043b2aab18SMatthew Ahrens int nblks = 0;
9053b2aab18SMatthew Ahrens
9063b2aab18SMatthew Ahrens dpsa.dpsa_dsname = dsname;
9073b2aab18SMatthew Ahrens dpsa.dpsa_source = source;
9083b2aab18SMatthew Ahrens dpsa.dpsa_props = props;
9093b2aab18SMatthew Ahrens
9103b2aab18SMatthew Ahrens /*
9113b2aab18SMatthew Ahrens * If the source includes NONE, then we will only be removing entries
9123b2aab18SMatthew Ahrens * from the ZAP object. In that case don't check for ENOSPC.
9133b2aab18SMatthew Ahrens */
9143b2aab18SMatthew Ahrens if ((source & ZPROP_SRC_NONE) == 0)
9153b2aab18SMatthew Ahrens nblks = 2 * fnvlist_num_pairs(props);
9163b2aab18SMatthew Ahrens
9173b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync,
9187d46dc6cSMatthew Ahrens &dpsa, nblks, ZFS_SPACE_CHECK_RESERVED));
9195c0b6a79SRich Morris }
9205c0b6a79SRich Morris
92192241e0bSTom Erickson typedef enum dsl_prop_getflags {
92292241e0bSTom Erickson DSL_PROP_GET_INHERITING = 0x1, /* searching parent of target ds */
92392241e0bSTom Erickson DSL_PROP_GET_SNAPSHOT = 0x2, /* snapshot dataset */
92492241e0bSTom Erickson DSL_PROP_GET_LOCAL = 0x4, /* local properties */
92592241e0bSTom Erickson DSL_PROP_GET_RECEIVED = 0x8 /* received properties */
92692241e0bSTom Erickson } dsl_prop_getflags_t;
92792241e0bSTom Erickson
92892241e0bSTom Erickson static int
dsl_prop_get_all_impl(objset_t * mos,uint64_t propobj,const char * setpoint,dsl_prop_getflags_t flags,nvlist_t * nv)92992241e0bSTom Erickson dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj,
93092241e0bSTom Erickson const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv)
9317f7322feSeschrock {
932a2eea2e1Sahrens zap_cursor_t zc;
933a2eea2e1Sahrens zap_attribute_t za;
93492241e0bSTom Erickson int err = 0;
9357f7322feSeschrock
936bb0ade09Sahrens for (zap_cursor_init(&zc, mos, propobj);
9377f7322feSeschrock (err = zap_cursor_retrieve(&zc, &za)) == 0;
9387f7322feSeschrock zap_cursor_advance(&zc)) {
939a2eea2e1Sahrens nvlist_t *propval;
94092241e0bSTom Erickson zfs_prop_t prop;
94192241e0bSTom Erickson char buf[ZAP_MAXNAMELEN];
94292241e0bSTom Erickson char *valstr;
94392241e0bSTom Erickson const char *suffix;
94492241e0bSTom Erickson const char *propname;
94592241e0bSTom Erickson const char *source;
94692241e0bSTom Erickson
94792241e0bSTom Erickson suffix = strchr(za.za_name, '$');
94892241e0bSTom Erickson
94992241e0bSTom Erickson if (suffix == NULL) {
95092241e0bSTom Erickson /*
95192241e0bSTom Erickson * Skip local properties if we only want received
95292241e0bSTom Erickson * properties.
95392241e0bSTom Erickson */
95492241e0bSTom Erickson if (flags & DSL_PROP_GET_RECEIVED)
95592241e0bSTom Erickson continue;
95692241e0bSTom Erickson
95792241e0bSTom Erickson propname = za.za_name;
95892241e0bSTom Erickson source = setpoint;
95992241e0bSTom Erickson } else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) {
96092241e0bSTom Erickson /* Skip explicitly inherited entries. */
96192241e0bSTom Erickson continue;
96292241e0bSTom Erickson } else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) {
96392241e0bSTom Erickson if (flags & DSL_PROP_GET_LOCAL)
96492241e0bSTom Erickson continue;
96592241e0bSTom Erickson
96692241e0bSTom Erickson (void) strncpy(buf, za.za_name, (suffix - za.za_name));
96792241e0bSTom Erickson buf[suffix - za.za_name] = '\0';
96892241e0bSTom Erickson propname = buf;
96992241e0bSTom Erickson
97092241e0bSTom Erickson if (!(flags & DSL_PROP_GET_RECEIVED)) {
97192241e0bSTom Erickson /* Skip if locally overridden. */
97292241e0bSTom Erickson err = zap_contains(mos, propobj, propname);
97392241e0bSTom Erickson if (err == 0)
97492241e0bSTom Erickson continue;
97592241e0bSTom Erickson if (err != ENOENT)
97692241e0bSTom Erickson break;
97792241e0bSTom Erickson
97892241e0bSTom Erickson /* Skip if explicitly inherited. */
97992241e0bSTom Erickson valstr = kmem_asprintf("%s%s", propname,
98092241e0bSTom Erickson ZPROP_INHERIT_SUFFIX);
98192241e0bSTom Erickson err = zap_contains(mos, propobj, valstr);
98292241e0bSTom Erickson strfree(valstr);
98392241e0bSTom Erickson if (err == 0)
98492241e0bSTom Erickson continue;
98592241e0bSTom Erickson if (err != ENOENT)
98692241e0bSTom Erickson break;
98792241e0bSTom Erickson }
98892241e0bSTom Erickson
98992241e0bSTom Erickson source = ((flags & DSL_PROP_GET_INHERITING) ?
99092241e0bSTom Erickson setpoint : ZPROP_SOURCE_VAL_RECVD);
99192241e0bSTom Erickson } else {
99292241e0bSTom Erickson /*
99392241e0bSTom Erickson * For backward compatibility, skip suffixes we don't
99492241e0bSTom Erickson * recognize.
99592241e0bSTom Erickson */
99692241e0bSTom Erickson continue;
99792241e0bSTom Erickson }
99892241e0bSTom Erickson
99992241e0bSTom Erickson prop = zfs_name_to_prop(propname);
1000bb0ade09Sahrens
1001bb0ade09Sahrens /* Skip non-inheritable properties. */
100292241e0bSTom Erickson if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL &&
100392241e0bSTom Erickson !zfs_prop_inheritable(prop))
10047f7322feSeschrock continue;
10057f7322feSeschrock
1006bb0ade09Sahrens /* Skip properties not valid for this type. */
100792241e0bSTom Erickson if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL &&
1008da6c28aaSamw !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
1009da6c28aaSamw continue;
1010da6c28aaSamw
101192241e0bSTom Erickson /* Skip properties already defined. */
101292241e0bSTom Erickson if (nvlist_exists(nv, propname))
1013e9dbad6fSeschrock continue;
1014e9dbad6fSeschrock
101592241e0bSTom Erickson VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10167f7322feSeschrock if (za.za_integer_length == 1) {
10177f7322feSeschrock /*
10187f7322feSeschrock * String property
10197f7322feSeschrock */
1020a2eea2e1Sahrens char *tmp = kmem_alloc(za.za_num_integers,
1021a2eea2e1Sahrens KM_SLEEP);
1022bb0ade09Sahrens err = zap_lookup(mos, propobj,
1023bb0ade09Sahrens za.za_name, 1, za.za_num_integers, tmp);
10247f7322feSeschrock if (err != 0) {
10257f7322feSeschrock kmem_free(tmp, za.za_num_integers);
10267f7322feSeschrock break;
10277f7322feSeschrock }
1028990b4856Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
1029990b4856Slling tmp) == 0);
10307f7322feSeschrock kmem_free(tmp, za.za_num_integers);
10317f7322feSeschrock } else {
10327f7322feSeschrock /*
10337f7322feSeschrock * Integer property
10347f7322feSeschrock */
10357f7322feSeschrock ASSERT(za.za_integer_length == 8);
1036990b4856Slling (void) nvlist_add_uint64(propval, ZPROP_VALUE,
1037990b4856Slling za.za_first_integer);
10387f7322feSeschrock }
10397f7322feSeschrock
104092241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0);
104192241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1042e9dbad6fSeschrock nvlist_free(propval);
10437f7322feSeschrock }
10447f7322feSeschrock zap_cursor_fini(&zc);
104592241e0bSTom Erickson if (err == ENOENT)
10467f7322feSeschrock err = 0;
10477f7322feSeschrock return (err);
10487f7322feSeschrock }
1049a2eea2e1Sahrens
105092241e0bSTom Erickson /*
105192241e0bSTom Erickson * Iterate over all properties for this dataset and return them in an nvlist.
105292241e0bSTom Erickson */
105392241e0bSTom Erickson static int
dsl_prop_get_all_ds(dsl_dataset_t * ds,nvlist_t ** nvp,dsl_prop_getflags_t flags)105492241e0bSTom Erickson dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp,
105592241e0bSTom Erickson dsl_prop_getflags_t flags)
105692241e0bSTom Erickson {
105792241e0bSTom Erickson dsl_dir_t *dd = ds->ds_dir;
105892241e0bSTom Erickson dsl_pool_t *dp = dd->dd_pool;
105992241e0bSTom Erickson objset_t *mos = dp->dp_meta_objset;
106092241e0bSTom Erickson int err = 0;
1061*40a5c998SMatthew Ahrens char setpoint[ZFS_MAX_DATASET_NAME_LEN];
106292241e0bSTom Erickson
106392241e0bSTom Erickson VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
106492241e0bSTom Erickson
1065bc9014e6SJustin Gibbs if (ds->ds_is_snapshot)
106692241e0bSTom Erickson flags |= DSL_PROP_GET_SNAPSHOT;
106792241e0bSTom Erickson
10683b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp));
106992241e0bSTom Erickson
1070c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_props_obj != 0) {
107192241e0bSTom Erickson ASSERT(flags & DSL_PROP_GET_SNAPSHOT);
107292241e0bSTom Erickson dsl_dataset_name(ds, setpoint);
1073c1379625SJustin T. Gibbs err = dsl_prop_get_all_impl(mos,
1074c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_props_obj, setpoint, flags, *nvp);
107592241e0bSTom Erickson if (err)
107692241e0bSTom Erickson goto out;
107792241e0bSTom Erickson }
107892241e0bSTom Erickson
107992241e0bSTom Erickson for (; dd != NULL; dd = dd->dd_parent) {
108092241e0bSTom Erickson if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) {
108192241e0bSTom Erickson if (flags & (DSL_PROP_GET_LOCAL |
108292241e0bSTom Erickson DSL_PROP_GET_RECEIVED))
108392241e0bSTom Erickson break;
108492241e0bSTom Erickson flags |= DSL_PROP_GET_INHERITING;
108592241e0bSTom Erickson }
108692241e0bSTom Erickson dsl_dir_name(dd, setpoint);
1087c1379625SJustin T. Gibbs err = dsl_prop_get_all_impl(mos,
1088c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_props_zapobj, setpoint, flags, *nvp);
108992241e0bSTom Erickson if (err)
109092241e0bSTom Erickson break;
109192241e0bSTom Erickson }
109292241e0bSTom Erickson out:
109392241e0bSTom Erickson return (err);
109492241e0bSTom Erickson }
109592241e0bSTom Erickson
109692241e0bSTom Erickson boolean_t
dsl_prop_get_hasrecvd(const char * dsname)10973b2aab18SMatthew Ahrens dsl_prop_get_hasrecvd(const char *dsname)
109892241e0bSTom Erickson {
109992241e0bSTom Erickson uint64_t dummy;
110092241e0bSTom Erickson
11013b2aab18SMatthew Ahrens return (0 ==
11023b2aab18SMatthew Ahrens dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL));
110392241e0bSTom Erickson }
110492241e0bSTom Erickson
11053b2aab18SMatthew Ahrens static int
dsl_prop_set_hasrecvd_impl(const char * dsname,zprop_source_t source)11063b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source)
110792241e0bSTom Erickson {
11083b2aab18SMatthew Ahrens uint64_t version;
11093b2aab18SMatthew Ahrens spa_t *spa;
11103b2aab18SMatthew Ahrens int error = 0;
111192241e0bSTom Erickson
11123b2aab18SMatthew Ahrens VERIFY0(spa_open(dsname, &spa, FTAG));
11133b2aab18SMatthew Ahrens version = spa_version(spa);
11143b2aab18SMatthew Ahrens spa_close(spa, FTAG);
111592241e0bSTom Erickson
11163b2aab18SMatthew Ahrens if (version >= SPA_VERSION_RECVD_PROPS)
11173b2aab18SMatthew Ahrens error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0);
11183b2aab18SMatthew Ahrens return (error);
111992241e0bSTom Erickson }
112092241e0bSTom Erickson
112192241e0bSTom Erickson /*
112292241e0bSTom Erickson * Call after successfully receiving properties to ensure that only the first
112392241e0bSTom Erickson * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
112492241e0bSTom Erickson */
11253b2aab18SMatthew Ahrens int
dsl_prop_set_hasrecvd(const char * dsname)11263b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd(const char *dsname)
112792241e0bSTom Erickson {
11283b2aab18SMatthew Ahrens int error = 0;
11293b2aab18SMatthew Ahrens if (!dsl_prop_get_hasrecvd(dsname))
11303b2aab18SMatthew Ahrens error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL);
11313b2aab18SMatthew Ahrens return (error);
113292241e0bSTom Erickson }
113392241e0bSTom Erickson
113492241e0bSTom Erickson void
dsl_prop_unset_hasrecvd(const char * dsname)11353b2aab18SMatthew Ahrens dsl_prop_unset_hasrecvd(const char *dsname)
113692241e0bSTom Erickson {
11373b2aab18SMatthew Ahrens VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE));
113892241e0bSTom Erickson }
113992241e0bSTom Erickson
114092241e0bSTom Erickson int
dsl_prop_get_all(objset_t * os,nvlist_t ** nvp)114192241e0bSTom Erickson dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
114292241e0bSTom Erickson {
114392241e0bSTom Erickson return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0));
114492241e0bSTom Erickson }
114592241e0bSTom Erickson
114692241e0bSTom Erickson int
dsl_prop_get_received(const char * dsname,nvlist_t ** nvp)11473b2aab18SMatthew Ahrens dsl_prop_get_received(const char *dsname, nvlist_t **nvp)
114892241e0bSTom Erickson {
11493b2aab18SMatthew Ahrens objset_t *os;
11503b2aab18SMatthew Ahrens int error;
11513b2aab18SMatthew Ahrens
115292241e0bSTom Erickson /*
115392241e0bSTom Erickson * Received properties are not distinguishable from local properties
115492241e0bSTom Erickson * until the dataset has received properties on or after
115592241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS.
115692241e0bSTom Erickson */
11573b2aab18SMatthew Ahrens dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ?
115892241e0bSTom Erickson DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL);
11593b2aab18SMatthew Ahrens
11603b2aab18SMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os);
11613b2aab18SMatthew Ahrens if (error != 0)
11623b2aab18SMatthew Ahrens return (error);
11633b2aab18SMatthew Ahrens error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags);
11643b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG);
11653b2aab18SMatthew Ahrens return (error);
116692241e0bSTom Erickson }
116792241e0bSTom Erickson
1168a2eea2e1Sahrens void
dsl_prop_nvlist_add_uint64(nvlist_t * nv,zfs_prop_t prop,uint64_t value)1169a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
1170a2eea2e1Sahrens {
1171a2eea2e1Sahrens nvlist_t *propval;
117292241e0bSTom Erickson const char *propname = zfs_prop_to_name(prop);
117392241e0bSTom Erickson uint64_t default_value;
117492241e0bSTom Erickson
117592241e0bSTom Erickson if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
117692241e0bSTom Erickson VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
117792241e0bSTom Erickson return;
117892241e0bSTom Erickson }
1179a2eea2e1Sahrens
1180a2eea2e1Sahrens VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1181990b4856Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
118292241e0bSTom Erickson /* Indicate the default source if we can. */
1183d09e4475SAlex Wilson if (dodefault(prop, 8, 1, &default_value) == 0 &&
118492241e0bSTom Erickson value == default_value) {
118592241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0);
118692241e0bSTom Erickson }
118792241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1188a2eea2e1Sahrens nvlist_free(propval);
1189a2eea2e1Sahrens }
1190a2eea2e1Sahrens
1191a2eea2e1Sahrens void
dsl_prop_nvlist_add_string(nvlist_t * nv,zfs_prop_t prop,const char * value)1192a2eea2e1Sahrens dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
1193a2eea2e1Sahrens {
1194a2eea2e1Sahrens nvlist_t *propval;
119592241e0bSTom Erickson const char *propname = zfs_prop_to_name(prop);
119692241e0bSTom Erickson
119792241e0bSTom Erickson if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
119892241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
119992241e0bSTom Erickson return;
120092241e0bSTom Erickson }
1201a2eea2e1Sahrens
1202a2eea2e1Sahrens VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1203990b4856Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
120492241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1205a2eea2e1Sahrens nvlist_free(propval);
1206a2eea2e1Sahrens }
1207