1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 237d46dc6cSMatthew Ahrens * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 24013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 2792241e0bSTom Erickson #include <sys/zfs_context.h> 28fa9e4066Sahrens #include <sys/dmu.h> 297f7322feSeschrock #include <sys/dmu_objset.h> 30fa9e4066Sahrens #include <sys/dmu_tx.h> 31fa9e4066Sahrens #include <sys/dsl_dataset.h> 32fa9e4066Sahrens #include <sys/dsl_dir.h> 33fa9e4066Sahrens #include <sys/dsl_prop.h> 341d452cf5Sahrens #include <sys/dsl_synctask.h> 35fa9e4066Sahrens #include <sys/spa.h> 36fa9e4066Sahrens #include <sys/zap.h> 37fa9e4066Sahrens #include <sys/fs/zfs.h> 38fa9e4066Sahrens 39fa9e4066Sahrens #include "zfs_prop.h" 40fa9e4066Sahrens 4192241e0bSTom Erickson #define ZPROP_INHERIT_SUFFIX "$inherit" 4292241e0bSTom Erickson #define ZPROP_RECVD_SUFFIX "$recvd" 4392241e0bSTom Erickson 44fa9e4066Sahrens static int 4592241e0bSTom Erickson dodefault(const char *propname, int intsz, int numints, void *buf) 46fa9e4066Sahrens { 47fa9e4066Sahrens zfs_prop_t prop; 48fa9e4066Sahrens 49da6c28aaSamw /* 50da6c28aaSamw * The setonce properties are read-only, BUT they still 51da6c28aaSamw * have a default value that can be used as the initial 52da6c28aaSamw * value. 53da6c28aaSamw */ 54990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL || 55da6c28aaSamw (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop))) 56be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 57fa9e4066Sahrens 5891ebeef5Sahrens if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 59fa9e4066Sahrens if (intsz != 1) 60be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 61990b4856Slling (void) strncpy(buf, zfs_prop_default_string(prop), 6292241e0bSTom Erickson numints); 63fa9e4066Sahrens } else { 6492241e0bSTom Erickson if (intsz != 8 || numints < 1) 65be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 66fa9e4066Sahrens 67fa9e4066Sahrens *(uint64_t *)buf = zfs_prop_default_numeric(prop); 68fa9e4066Sahrens } 69fa9e4066Sahrens 70fa9e4066Sahrens return (0); 71fa9e4066Sahrens } 72fa9e4066Sahrens 73bb0ade09Sahrens int 74bb0ade09Sahrens dsl_prop_get_dd(dsl_dir_t *dd, const char *propname, 7592241e0bSTom Erickson int intsz, int numints, void *buf, char *setpoint, boolean_t snapshot) 76fa9e4066Sahrens { 7799653d4eSeschrock int err = ENOENT; 7892241e0bSTom Erickson dsl_dir_t *target = dd; 79bb0ade09Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 80e9dbad6fSeschrock zfs_prop_t prop; 8192241e0bSTom Erickson boolean_t inheritable; 8292241e0bSTom Erickson boolean_t inheriting = B_FALSE; 8392241e0bSTom Erickson char *inheritstr; 8492241e0bSTom Erickson char *recvdstr; 85fa9e4066Sahrens 863b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dd->dd_pool)); 87bb0ade09Sahrens 88fa9e4066Sahrens if (setpoint) 89fa9e4066Sahrens setpoint[0] = '\0'; 90fa9e4066Sahrens 91e9dbad6fSeschrock prop = zfs_name_to_prop(propname); 9292241e0bSTom Erickson inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop)); 9392241e0bSTom Erickson inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX); 9492241e0bSTom Erickson recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX); 95e9dbad6fSeschrock 9699653d4eSeschrock /* 9792241e0bSTom Erickson * Note: dd may become NULL, therefore we shouldn't dereference it 9892241e0bSTom Erickson * after this loop. 9999653d4eSeschrock */ 10099653d4eSeschrock for (; dd != NULL; dd = dd->dd_parent) { 10192241e0bSTom Erickson if (dd != target || snapshot) { 10292241e0bSTom Erickson if (!inheritable) 10392241e0bSTom Erickson break; 10492241e0bSTom Erickson inheriting = B_TRUE; 10592241e0bSTom Erickson } 10692241e0bSTom Erickson 10792241e0bSTom Erickson /* Check for a local value. */ 108c1379625SJustin T. Gibbs err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj, 109c1379625SJustin T. Gibbs propname, intsz, numints, buf); 110fa9e4066Sahrens if (err != ENOENT) { 11192241e0bSTom Erickson if (setpoint != NULL && err == 0) 112fa9e4066Sahrens dsl_dir_name(dd, setpoint); 113fa9e4066Sahrens break; 114fa9e4066Sahrens } 115e9dbad6fSeschrock 116e9dbad6fSeschrock /* 11792241e0bSTom Erickson * Skip the check for a received value if there is an explicit 11892241e0bSTom Erickson * inheritance entry. 119e9dbad6fSeschrock */ 120c1379625SJustin T. Gibbs err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj, 12192241e0bSTom Erickson inheritstr); 12292241e0bSTom Erickson if (err != 0 && err != ENOENT) 123e9dbad6fSeschrock break; 12492241e0bSTom Erickson 12592241e0bSTom Erickson if (err == ENOENT) { 12692241e0bSTom Erickson /* Check for a received value. */ 127c1379625SJustin T. Gibbs err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj, 12892241e0bSTom Erickson recvdstr, intsz, numints, buf); 12992241e0bSTom Erickson if (err != ENOENT) { 13092241e0bSTom Erickson if (setpoint != NULL && err == 0) { 13192241e0bSTom Erickson if (inheriting) { 13292241e0bSTom Erickson dsl_dir_name(dd, setpoint); 13392241e0bSTom Erickson } else { 13492241e0bSTom Erickson (void) strcpy(setpoint, 13592241e0bSTom Erickson ZPROP_SOURCE_VAL_RECVD); 13692241e0bSTom Erickson } 13792241e0bSTom Erickson } 13892241e0bSTom Erickson break; 13992241e0bSTom Erickson } 14092241e0bSTom Erickson } 14192241e0bSTom Erickson 14292241e0bSTom Erickson /* 14392241e0bSTom Erickson * If we found an explicit inheritance entry, err is zero even 14492241e0bSTom Erickson * though we haven't yet found the value, so reinitializing err 14592241e0bSTom Erickson * at the end of the loop (instead of at the beginning) ensures 14692241e0bSTom Erickson * that err has a valid post-loop value. 14792241e0bSTom Erickson */ 148be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 149fa9e4066Sahrens } 15092241e0bSTom Erickson 151fa9e4066Sahrens if (err == ENOENT) 15292241e0bSTom Erickson err = dodefault(propname, intsz, numints, buf); 15392241e0bSTom Erickson 15492241e0bSTom Erickson strfree(inheritstr); 15592241e0bSTom Erickson strfree(recvdstr); 156fa9e4066Sahrens 157fa9e4066Sahrens return (err); 158fa9e4066Sahrens } 159fa9e4066Sahrens 160bb0ade09Sahrens int 161bb0ade09Sahrens dsl_prop_get_ds(dsl_dataset_t *ds, const char *propname, 16292241e0bSTom Erickson int intsz, int numints, void *buf, char *setpoint) 163bb0ade09Sahrens { 16492241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 16592241e0bSTom Erickson boolean_t inheritable; 16692241e0bSTom Erickson uint64_t zapobj; 16792241e0bSTom Erickson 1683b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 16992241e0bSTom Erickson inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop)); 170c1379625SJustin T. Gibbs zapobj = dsl_dataset_phys(ds)->ds_props_obj; 17192241e0bSTom Erickson 17292241e0bSTom Erickson if (zapobj != 0) { 17392241e0bSTom Erickson objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 17492241e0bSTom Erickson int err; 175bb0ade09Sahrens 176*bc9014e6SJustin Gibbs ASSERT(ds->ds_is_snapshot); 17792241e0bSTom Erickson 17892241e0bSTom Erickson /* Check for a local value. */ 17992241e0bSTom Erickson err = zap_lookup(mos, zapobj, propname, intsz, numints, buf); 180bb0ade09Sahrens if (err != ENOENT) { 18192241e0bSTom Erickson if (setpoint != NULL && err == 0) 182bb0ade09Sahrens dsl_dataset_name(ds, setpoint); 183bb0ade09Sahrens return (err); 184bb0ade09Sahrens } 18592241e0bSTom Erickson 18692241e0bSTom Erickson /* 18792241e0bSTom Erickson * Skip the check for a received value if there is an explicit 18892241e0bSTom Erickson * inheritance entry. 18992241e0bSTom Erickson */ 19092241e0bSTom Erickson if (inheritable) { 19192241e0bSTom Erickson char *inheritstr = kmem_asprintf("%s%s", propname, 19292241e0bSTom Erickson ZPROP_INHERIT_SUFFIX); 19392241e0bSTom Erickson err = zap_contains(mos, zapobj, inheritstr); 19492241e0bSTom Erickson strfree(inheritstr); 19592241e0bSTom Erickson if (err != 0 && err != ENOENT) 19692241e0bSTom Erickson return (err); 19792241e0bSTom Erickson } 19892241e0bSTom Erickson 19992241e0bSTom Erickson if (err == ENOENT) { 20092241e0bSTom Erickson /* Check for a received value. */ 20192241e0bSTom Erickson char *recvdstr = kmem_asprintf("%s%s", propname, 20292241e0bSTom Erickson ZPROP_RECVD_SUFFIX); 20392241e0bSTom Erickson err = zap_lookup(mos, zapobj, recvdstr, 20492241e0bSTom Erickson intsz, numints, buf); 20592241e0bSTom Erickson strfree(recvdstr); 20692241e0bSTom Erickson if (err != ENOENT) { 20792241e0bSTom Erickson if (setpoint != NULL && err == 0) 20892241e0bSTom Erickson (void) strcpy(setpoint, 20992241e0bSTom Erickson ZPROP_SOURCE_VAL_RECVD); 21092241e0bSTom Erickson return (err); 21192241e0bSTom Erickson } 21292241e0bSTom Erickson } 213bb0ade09Sahrens } 214bb0ade09Sahrens 215bb0ade09Sahrens return (dsl_prop_get_dd(ds->ds_dir, propname, 216*bc9014e6SJustin Gibbs intsz, numints, buf, setpoint, ds->ds_is_snapshot)); 217bb0ade09Sahrens } 218bb0ade09Sahrens 219fa9e4066Sahrens /* 220fa9e4066Sahrens * Register interest in the named property. We'll call the callback 221fa9e4066Sahrens * once to notify it of the current property value, and again each time 222fa9e4066Sahrens * the property changes, until this callback is unregistered. 223fa9e4066Sahrens * 224fa9e4066Sahrens * Return 0 on success, errno if the prop is not an integer value. 225fa9e4066Sahrens */ 226fa9e4066Sahrens int 227fa9e4066Sahrens dsl_prop_register(dsl_dataset_t *ds, const char *propname, 228fa9e4066Sahrens dsl_prop_changed_cb_t *callback, void *cbarg) 229fa9e4066Sahrens { 23099653d4eSeschrock dsl_dir_t *dd = ds->ds_dir; 231bb0ade09Sahrens dsl_pool_t *dp = dd->dd_pool; 232fa9e4066Sahrens uint64_t value; 233fa9e4066Sahrens dsl_prop_cb_record_t *cbr; 234fa9e4066Sahrens int err; 235fa9e4066Sahrens 2363b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 237fa9e4066Sahrens 2383b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, propname, &value); 2393b2aab18SMatthew Ahrens if (err != 0) 240fa9e4066Sahrens return (err); 241fa9e4066Sahrens 242fa9e4066Sahrens cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP); 24399653d4eSeschrock cbr->cbr_ds = ds; 244fa9e4066Sahrens cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP); 245fa9e4066Sahrens (void) strcpy((char *)cbr->cbr_propname, propname); 246fa9e4066Sahrens cbr->cbr_func = callback; 247fa9e4066Sahrens cbr->cbr_arg = cbarg; 248fa9e4066Sahrens mutex_enter(&dd->dd_lock); 249fa9e4066Sahrens list_insert_head(&dd->dd_prop_cbs, cbr); 250fa9e4066Sahrens mutex_exit(&dd->dd_lock); 251fa9e4066Sahrens 252fa9e4066Sahrens cbr->cbr_func(cbr->cbr_arg, value); 253fa9e4066Sahrens return (0); 254fa9e4066Sahrens } 255fa9e4066Sahrens 256fa9e4066Sahrens int 257bb0ade09Sahrens dsl_prop_get(const char *dsname, const char *propname, 258fa9e4066Sahrens int intsz, int numints, void *buf, char *setpoint) 259fa9e4066Sahrens { 2603b2aab18SMatthew Ahrens objset_t *os; 2613b2aab18SMatthew Ahrens int error; 262fa9e4066Sahrens 2633b2aab18SMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os); 2643b2aab18SMatthew Ahrens if (error != 0) 2653b2aab18SMatthew Ahrens return (error); 266fa9e4066Sahrens 2673b2aab18SMatthew Ahrens error = dsl_prop_get_ds(dmu_objset_ds(os), propname, 2683b2aab18SMatthew Ahrens intsz, numints, buf, setpoint); 269fa9e4066Sahrens 2703b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 2713b2aab18SMatthew Ahrens return (error); 272fa9e4066Sahrens } 273fa9e4066Sahrens 274fa9e4066Sahrens /* 275fa9e4066Sahrens * Get the current property value. It may have changed by the time this 276fa9e4066Sahrens * function returns, so it is NOT safe to follow up with 277fa9e4066Sahrens * dsl_prop_register() and assume that the value has not changed in 278fa9e4066Sahrens * between. 279fa9e4066Sahrens * 280fa9e4066Sahrens * Return 0 on success, ENOENT if ddname is invalid. 281fa9e4066Sahrens */ 282fa9e4066Sahrens int 283fa9e4066Sahrens dsl_prop_get_integer(const char *ddname, const char *propname, 284fa9e4066Sahrens uint64_t *valuep, char *setpoint) 285fa9e4066Sahrens { 286fa9e4066Sahrens return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint)); 287fa9e4066Sahrens } 288fa9e4066Sahrens 2893b2aab18SMatthew Ahrens int 2903b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname, 2913b2aab18SMatthew Ahrens uint64_t *valuep) 29292241e0bSTom Erickson { 2933b2aab18SMatthew Ahrens return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL)); 29492241e0bSTom Erickson } 29592241e0bSTom Erickson 29692241e0bSTom Erickson /* 29792241e0bSTom Erickson * Predict the effective value of the given special property if it were set with 29892241e0bSTom Erickson * the given value and source. This is not a general purpose function. It exists 29992241e0bSTom Erickson * only to handle the special requirements of the quota and reservation 30092241e0bSTom Erickson * properties. The fact that these properties are non-inheritable greatly 30192241e0bSTom Erickson * simplifies the prediction logic. 30292241e0bSTom Erickson * 30392241e0bSTom Erickson * Returns 0 on success, a positive error code on failure, or -1 if called with 30492241e0bSTom Erickson * a property not handled by this function. 30592241e0bSTom Erickson */ 30692241e0bSTom Erickson int 3073b2aab18SMatthew Ahrens dsl_prop_predict(dsl_dir_t *dd, const char *propname, 3083b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, uint64_t *newvalp) 30992241e0bSTom Erickson { 31092241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 31192241e0bSTom Erickson objset_t *mos; 31292241e0bSTom Erickson uint64_t zapobj; 31392241e0bSTom Erickson uint64_t version; 31492241e0bSTom Erickson char *recvdstr; 31592241e0bSTom Erickson int err = 0; 31692241e0bSTom Erickson 31792241e0bSTom Erickson switch (prop) { 31892241e0bSTom Erickson case ZFS_PROP_QUOTA: 31992241e0bSTom Erickson case ZFS_PROP_RESERVATION: 32092241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 32192241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 32292241e0bSTom Erickson break; 32392241e0bSTom Erickson default: 32492241e0bSTom Erickson return (-1); 32592241e0bSTom Erickson } 32692241e0bSTom Erickson 32792241e0bSTom Erickson mos = dd->dd_pool->dp_meta_objset; 328c1379625SJustin T. Gibbs zapobj = dsl_dir_phys(dd)->dd_props_zapobj; 32992241e0bSTom Erickson recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX); 33092241e0bSTom Erickson 33192241e0bSTom Erickson version = spa_version(dd->dd_pool->dp_spa); 33292241e0bSTom Erickson if (version < SPA_VERSION_RECVD_PROPS) { 33392241e0bSTom Erickson if (source & ZPROP_SRC_NONE) 33492241e0bSTom Erickson source = ZPROP_SRC_NONE; 33592241e0bSTom Erickson else if (source & ZPROP_SRC_RECEIVED) 33692241e0bSTom Erickson source = ZPROP_SRC_LOCAL; 33792241e0bSTom Erickson } 33892241e0bSTom Erickson 33992241e0bSTom Erickson switch (source) { 34092241e0bSTom Erickson case ZPROP_SRC_NONE: 34192241e0bSTom Erickson /* Revert to the received value, if any. */ 3423b2aab18SMatthew Ahrens err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp); 34392241e0bSTom Erickson if (err == ENOENT) 3443b2aab18SMatthew Ahrens *newvalp = 0; 34592241e0bSTom Erickson break; 34692241e0bSTom Erickson case ZPROP_SRC_LOCAL: 3473b2aab18SMatthew Ahrens *newvalp = value; 34892241e0bSTom Erickson break; 34992241e0bSTom Erickson case ZPROP_SRC_RECEIVED: 35092241e0bSTom Erickson /* 35192241e0bSTom Erickson * If there's no local setting, then the new received value will 35292241e0bSTom Erickson * be the effective value. 35392241e0bSTom Erickson */ 3543b2aab18SMatthew Ahrens err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp); 35592241e0bSTom Erickson if (err == ENOENT) 3563b2aab18SMatthew Ahrens *newvalp = value; 35792241e0bSTom Erickson break; 35892241e0bSTom Erickson case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED): 35992241e0bSTom Erickson /* 36092241e0bSTom Erickson * We're clearing the received value, so the local setting (if 36192241e0bSTom Erickson * it exists) remains the effective value. 36292241e0bSTom Erickson */ 3633b2aab18SMatthew Ahrens err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp); 36492241e0bSTom Erickson if (err == ENOENT) 3653b2aab18SMatthew Ahrens *newvalp = 0; 36692241e0bSTom Erickson break; 36792241e0bSTom Erickson default: 3683b2aab18SMatthew Ahrens panic("unexpected property source: %d", source); 36992241e0bSTom Erickson } 37092241e0bSTom Erickson 37192241e0bSTom Erickson strfree(recvdstr); 37292241e0bSTom Erickson 37392241e0bSTom Erickson if (err == ENOENT) 37492241e0bSTom Erickson return (0); 37592241e0bSTom Erickson 37692241e0bSTom Erickson return (err); 37792241e0bSTom Erickson } 37892241e0bSTom Erickson 379fa9e4066Sahrens /* 380fa9e4066Sahrens * Unregister this callback. Return 0 on success, ENOENT if ddname is 381f7170741SWill Andrews * invalid, or ENOMSG if no matching callback registered. 382fa9e4066Sahrens */ 383fa9e4066Sahrens int 384fa9e4066Sahrens dsl_prop_unregister(dsl_dataset_t *ds, const char *propname, 385fa9e4066Sahrens dsl_prop_changed_cb_t *callback, void *cbarg) 386fa9e4066Sahrens { 38799653d4eSeschrock dsl_dir_t *dd = ds->ds_dir; 388fa9e4066Sahrens dsl_prop_cb_record_t *cbr; 389fa9e4066Sahrens 390fa9e4066Sahrens mutex_enter(&dd->dd_lock); 391fa9e4066Sahrens for (cbr = list_head(&dd->dd_prop_cbs); 392fa9e4066Sahrens cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) { 39399653d4eSeschrock if (cbr->cbr_ds == ds && 394fa9e4066Sahrens cbr->cbr_func == callback && 39599653d4eSeschrock cbr->cbr_arg == cbarg && 39699653d4eSeschrock strcmp(cbr->cbr_propname, propname) == 0) 397fa9e4066Sahrens break; 398fa9e4066Sahrens } 399fa9e4066Sahrens 400fa9e4066Sahrens if (cbr == NULL) { 401fa9e4066Sahrens mutex_exit(&dd->dd_lock); 402be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMSG)); 403fa9e4066Sahrens } 404fa9e4066Sahrens 405fa9e4066Sahrens list_remove(&dd->dd_prop_cbs, cbr); 406fa9e4066Sahrens mutex_exit(&dd->dd_lock); 407fa9e4066Sahrens kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1); 408fa9e4066Sahrens kmem_free(cbr, sizeof (dsl_prop_cb_record_t)); 409fa9e4066Sahrens 410fa9e4066Sahrens return (0); 411fa9e4066Sahrens } 412fa9e4066Sahrens 4133b2aab18SMatthew Ahrens boolean_t 4143b2aab18SMatthew Ahrens dsl_prop_hascb(dsl_dataset_t *ds) 41599653d4eSeschrock { 41699653d4eSeschrock dsl_dir_t *dd = ds->ds_dir; 4173b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 41899653d4eSeschrock dsl_prop_cb_record_t *cbr; 41999653d4eSeschrock 42099653d4eSeschrock mutex_enter(&dd->dd_lock); 4213b2aab18SMatthew Ahrens for (cbr = list_head(&dd->dd_prop_cbs); cbr; 4223b2aab18SMatthew Ahrens cbr = list_next(&dd->dd_prop_cbs, cbr)) { 4233b2aab18SMatthew Ahrens if (cbr->cbr_ds == ds) { 4243b2aab18SMatthew Ahrens rv = B_TRUE; 4253b2aab18SMatthew Ahrens break; 4263b2aab18SMatthew Ahrens } 42799653d4eSeschrock } 42899653d4eSeschrock mutex_exit(&dd->dd_lock); 4293b2aab18SMatthew Ahrens return (rv); 4303b2aab18SMatthew Ahrens } 43199653d4eSeschrock 4323b2aab18SMatthew Ahrens /* ARGSUSED */ 4333b2aab18SMatthew Ahrens static int 4343b2aab18SMatthew Ahrens dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 4353b2aab18SMatthew Ahrens { 4363b2aab18SMatthew Ahrens dsl_dir_t *dd = ds->ds_dir; 4373b2aab18SMatthew Ahrens dsl_prop_cb_record_t *cbr; 4383b2aab18SMatthew Ahrens 4393b2aab18SMatthew Ahrens mutex_enter(&dd->dd_lock); 4403b2aab18SMatthew Ahrens for (cbr = list_head(&dd->dd_prop_cbs); cbr; 4413b2aab18SMatthew Ahrens cbr = list_next(&dd->dd_prop_cbs, cbr)) { 4423b2aab18SMatthew Ahrens uint64_t value; 4433b2aab18SMatthew Ahrens 4443b2aab18SMatthew Ahrens if (dsl_prop_get_ds(cbr->cbr_ds, cbr->cbr_propname, 4453b2aab18SMatthew Ahrens sizeof (value), 1, &value, NULL) == 0) 4463b2aab18SMatthew Ahrens cbr->cbr_func(cbr->cbr_arg, value); 4473b2aab18SMatthew Ahrens } 4483b2aab18SMatthew Ahrens mutex_exit(&dd->dd_lock); 4493b2aab18SMatthew Ahrens 4503b2aab18SMatthew Ahrens return (0); 4513b2aab18SMatthew Ahrens } 4523b2aab18SMatthew Ahrens 4533b2aab18SMatthew Ahrens /* 4543b2aab18SMatthew Ahrens * Update all property values for ddobj & its descendants. This is used 4553b2aab18SMatthew Ahrens * when renaming the dir. 4563b2aab18SMatthew Ahrens */ 4573b2aab18SMatthew Ahrens void 4583b2aab18SMatthew Ahrens dsl_prop_notify_all(dsl_dir_t *dd) 4593b2aab18SMatthew Ahrens { 4603b2aab18SMatthew Ahrens dsl_pool_t *dp = dd->dd_pool; 4613b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 4623b2aab18SMatthew Ahrens (void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb, 4633b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 46499653d4eSeschrock } 46599653d4eSeschrock 466fa9e4066Sahrens static void 467fa9e4066Sahrens dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj, 468fa9e4066Sahrens const char *propname, uint64_t value, int first) 469fa9e4066Sahrens { 470fa9e4066Sahrens dsl_dir_t *dd; 471fa9e4066Sahrens dsl_prop_cb_record_t *cbr; 472fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 4731d452cf5Sahrens zap_cursor_t zc; 474d8d77200Sahrens zap_attribute_t *za; 475fa9e4066Sahrens int err; 476fa9e4066Sahrens 4773b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 4783b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd); 479ea8dc4b6Seschrock if (err) 480ea8dc4b6Seschrock return; 481fa9e4066Sahrens 482fa9e4066Sahrens if (!first) { 483fa9e4066Sahrens /* 484fa9e4066Sahrens * If the prop is set here, then this change is not 485fa9e4066Sahrens * being inherited here or below; stop the recursion. 486fa9e4066Sahrens */ 487c1379625SJustin T. Gibbs err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj, 488c1379625SJustin T. Gibbs propname); 489fa9e4066Sahrens if (err == 0) { 4903b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 491fa9e4066Sahrens return; 492fa9e4066Sahrens } 493fa9e4066Sahrens ASSERT3U(err, ==, ENOENT); 494fa9e4066Sahrens } 495fa9e4066Sahrens 496fa9e4066Sahrens mutex_enter(&dd->dd_lock); 497bb0ade09Sahrens for (cbr = list_head(&dd->dd_prop_cbs); cbr; 498bb0ade09Sahrens cbr = list_next(&dd->dd_prop_cbs, cbr)) { 499c1379625SJustin T. Gibbs uint64_t propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj; 500bb0ade09Sahrens 501bb0ade09Sahrens if (strcmp(cbr->cbr_propname, propname) != 0) 502bb0ade09Sahrens continue; 503bb0ade09Sahrens 504bb0ade09Sahrens /* 505bb0ade09Sahrens * If the property is set on this ds, then it is not 506bb0ade09Sahrens * inherited here; don't call the callback. 507bb0ade09Sahrens */ 50892241e0bSTom Erickson if (propobj && 0 == zap_contains(mos, propobj, propname)) 509bb0ade09Sahrens continue; 510bb0ade09Sahrens 511bb0ade09Sahrens cbr->cbr_func(cbr->cbr_arg, value); 512fa9e4066Sahrens } 513fa9e4066Sahrens mutex_exit(&dd->dd_lock); 514fa9e4066Sahrens 515d8d77200Sahrens za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 5161d452cf5Sahrens for (zap_cursor_init(&zc, mos, 517c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj); 518d8d77200Sahrens zap_cursor_retrieve(&zc, za) == 0; 5191d452cf5Sahrens zap_cursor_advance(&zc)) { 520d8d77200Sahrens dsl_prop_changed_notify(dp, za->za_first_integer, 5211d452cf5Sahrens propname, value, FALSE); 522fa9e4066Sahrens } 523d8d77200Sahrens kmem_free(za, sizeof (zap_attribute_t)); 5241d452cf5Sahrens zap_cursor_fini(&zc); 5253b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 526fa9e4066Sahrens } 527fa9e4066Sahrens 52892241e0bSTom Erickson void 5293b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname, 5303b2aab18SMatthew Ahrens zprop_source_t source, int intsz, int numints, const void *value, 5313b2aab18SMatthew Ahrens dmu_tx_t *tx) 532fa9e4066Sahrens { 533bb0ade09Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 53492241e0bSTom Erickson uint64_t zapobj, intval, dummy; 5351d452cf5Sahrens int isint; 536ecd6cf80Smarks char valbuf[32]; 5373b2aab18SMatthew Ahrens const char *valstr = NULL; 53892241e0bSTom Erickson char *inheritstr; 53992241e0bSTom Erickson char *recvdstr; 54092241e0bSTom Erickson char *tbuf = NULL; 54192241e0bSTom Erickson int err; 54292241e0bSTom Erickson uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa); 543fa9e4066Sahrens 54492241e0bSTom Erickson isint = (dodefault(propname, 8, 1, &intval) == 0); 545fa9e4066Sahrens 546*bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 54792241e0bSTom Erickson ASSERT(version >= SPA_VERSION_SNAP_PROPS); 548c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_props_obj == 0) { 549bb0ade09Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 550c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_props_obj = 551bb0ade09Sahrens zap_create(mos, 552bb0ade09Sahrens DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 553bb0ade09Sahrens } 554c1379625SJustin T. Gibbs zapobj = dsl_dataset_phys(ds)->ds_props_obj; 555bb0ade09Sahrens } else { 556c1379625SJustin T. Gibbs zapobj = dsl_dir_phys(ds->ds_dir)->dd_props_zapobj; 557bb0ade09Sahrens } 558bb0ade09Sahrens 55992241e0bSTom Erickson if (version < SPA_VERSION_RECVD_PROPS) { 56092241e0bSTom Erickson if (source & ZPROP_SRC_NONE) 56192241e0bSTom Erickson source = ZPROP_SRC_NONE; 56292241e0bSTom Erickson else if (source & ZPROP_SRC_RECEIVED) 56392241e0bSTom Erickson source = ZPROP_SRC_LOCAL; 56492241e0bSTom Erickson } 56592241e0bSTom Erickson 56692241e0bSTom Erickson inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX); 56792241e0bSTom Erickson recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX); 56892241e0bSTom Erickson 56992241e0bSTom Erickson switch (source) { 57092241e0bSTom Erickson case ZPROP_SRC_NONE: 57192241e0bSTom Erickson /* 57292241e0bSTom Erickson * revert to received value, if any (inherit -S) 57392241e0bSTom Erickson * - remove propname 57492241e0bSTom Erickson * - remove propname$inherit 57592241e0bSTom Erickson */ 57692241e0bSTom Erickson err = zap_remove(mos, zapobj, propname, tx); 57792241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 57892241e0bSTom Erickson err = zap_remove(mos, zapobj, inheritstr, tx); 57992241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 58092241e0bSTom Erickson break; 58192241e0bSTom Erickson case ZPROP_SRC_LOCAL: 58292241e0bSTom Erickson /* 58392241e0bSTom Erickson * remove propname$inherit 58492241e0bSTom Erickson * set propname -> value 58592241e0bSTom Erickson */ 58692241e0bSTom Erickson err = zap_remove(mos, zapobj, inheritstr, tx); 58792241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 5883b2aab18SMatthew Ahrens VERIFY0(zap_update(mos, zapobj, propname, 5893b2aab18SMatthew Ahrens intsz, numints, value, tx)); 59092241e0bSTom Erickson break; 59192241e0bSTom Erickson case ZPROP_SRC_INHERITED: 59292241e0bSTom Erickson /* 59392241e0bSTom Erickson * explicitly inherit 59492241e0bSTom Erickson * - remove propname 59592241e0bSTom Erickson * - set propname$inherit 59692241e0bSTom Erickson */ 59792241e0bSTom Erickson err = zap_remove(mos, zapobj, propname, tx); 5981d452cf5Sahrens ASSERT(err == 0 || err == ENOENT); 59992241e0bSTom Erickson if (version >= SPA_VERSION_RECVD_PROPS && 6003b2aab18SMatthew Ahrens dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) { 60192241e0bSTom Erickson dummy = 0; 6023b2aab18SMatthew Ahrens VERIFY0(zap_update(mos, zapobj, inheritstr, 6033b2aab18SMatthew Ahrens 8, 1, &dummy, tx)); 604fa9e4066Sahrens } 60592241e0bSTom Erickson break; 60692241e0bSTom Erickson case ZPROP_SRC_RECEIVED: 60792241e0bSTom Erickson /* 60892241e0bSTom Erickson * set propname$recvd -> value 60992241e0bSTom Erickson */ 61092241e0bSTom Erickson err = zap_update(mos, zapobj, recvdstr, 6113b2aab18SMatthew Ahrens intsz, numints, value, tx); 61292241e0bSTom Erickson ASSERT(err == 0); 61392241e0bSTom Erickson break; 61492241e0bSTom Erickson case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED): 61592241e0bSTom Erickson /* 61692241e0bSTom Erickson * clear local and received settings 61792241e0bSTom Erickson * - remove propname 61892241e0bSTom Erickson * - remove propname$inherit 61992241e0bSTom Erickson * - remove propname$recvd 62092241e0bSTom Erickson */ 62192241e0bSTom Erickson err = zap_remove(mos, zapobj, propname, tx); 62292241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 62392241e0bSTom Erickson err = zap_remove(mos, zapobj, inheritstr, tx); 62492241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 62592241e0bSTom Erickson /* FALLTHRU */ 62692241e0bSTom Erickson case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED): 62792241e0bSTom Erickson /* 62892241e0bSTom Erickson * remove propname$recvd 62992241e0bSTom Erickson */ 63092241e0bSTom Erickson err = zap_remove(mos, zapobj, recvdstr, tx); 63192241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 63292241e0bSTom Erickson break; 63392241e0bSTom Erickson default: 63492241e0bSTom Erickson cmn_err(CE_PANIC, "unexpected property source: %d", source); 635fa9e4066Sahrens } 636fa9e4066Sahrens 63792241e0bSTom Erickson strfree(inheritstr); 63892241e0bSTom Erickson strfree(recvdstr); 63992241e0bSTom Erickson 6401d452cf5Sahrens if (isint) { 6413b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval)); 64292241e0bSTom Erickson 643*bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 644bb0ade09Sahrens dsl_prop_cb_record_t *cbr; 645bb0ade09Sahrens /* 646bb0ade09Sahrens * It's a snapshot; nothing can inherit this 647bb0ade09Sahrens * property, so just look for callbacks on this 648bb0ade09Sahrens * ds here. 649bb0ade09Sahrens */ 650bb0ade09Sahrens mutex_enter(&ds->ds_dir->dd_lock); 651bb0ade09Sahrens for (cbr = list_head(&ds->ds_dir->dd_prop_cbs); cbr; 652bb0ade09Sahrens cbr = list_next(&ds->ds_dir->dd_prop_cbs, cbr)) { 653bb0ade09Sahrens if (cbr->cbr_ds == ds && 65492241e0bSTom Erickson strcmp(cbr->cbr_propname, propname) == 0) 655bb0ade09Sahrens cbr->cbr_func(cbr->cbr_arg, intval); 656bb0ade09Sahrens } 657bb0ade09Sahrens mutex_exit(&ds->ds_dir->dd_lock); 658bb0ade09Sahrens } else { 659bb0ade09Sahrens dsl_prop_changed_notify(ds->ds_dir->dd_pool, 66092241e0bSTom Erickson ds->ds_dir->dd_object, propname, intval, TRUE); 661bb0ade09Sahrens } 66292241e0bSTom Erickson 663ecd6cf80Smarks (void) snprintf(valbuf, sizeof (valbuf), 664ecd6cf80Smarks "%lld", (longlong_t)intval); 665ecd6cf80Smarks valstr = valbuf; 666ecd6cf80Smarks } else { 66792241e0bSTom Erickson if (source == ZPROP_SRC_LOCAL) { 6683b2aab18SMatthew Ahrens valstr = value; 66992241e0bSTom Erickson } else { 67092241e0bSTom Erickson tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP); 67192241e0bSTom Erickson if (dsl_prop_get_ds(ds, propname, 1, 67292241e0bSTom Erickson ZAP_MAXVALUELEN, tbuf, NULL) == 0) 67392241e0bSTom Erickson valstr = tbuf; 67492241e0bSTom Erickson } 675ecd6cf80Smarks } 67692241e0bSTom Erickson 6774445fffbSMatthew Ahrens spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE || 6784445fffbSMatthew Ahrens source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx, 6794445fffbSMatthew Ahrens "%s=%s", propname, (valstr == NULL ? "" : valstr)); 68092241e0bSTom Erickson 68192241e0bSTom Erickson if (tbuf != NULL) 68292241e0bSTom Erickson kmem_free(tbuf, ZAP_MAXVALUELEN); 683fa9e4066Sahrens } 684fa9e4066Sahrens 6853b2aab18SMatthew Ahrens int 6863b2aab18SMatthew Ahrens dsl_prop_set_int(const char *dsname, const char *propname, 6873b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value) 6885c0b6a79SRich Morris { 6893b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc(); 6903b2aab18SMatthew Ahrens int error; 69192241e0bSTom Erickson 6923b2aab18SMatthew Ahrens fnvlist_add_uint64(nvl, propname, value); 6933b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl); 6943b2aab18SMatthew Ahrens fnvlist_free(nvl); 6953b2aab18SMatthew Ahrens return (error); 6965c0b6a79SRich Morris } 6975c0b6a79SRich Morris 698a2eea2e1Sahrens int 6993b2aab18SMatthew Ahrens dsl_prop_set_string(const char *dsname, const char *propname, 7003b2aab18SMatthew Ahrens zprop_source_t source, const char *value) 701fa9e4066Sahrens { 7023b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc(); 7033b2aab18SMatthew Ahrens int error; 704bb0ade09Sahrens 7053b2aab18SMatthew Ahrens fnvlist_add_string(nvl, propname, value); 7063b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl); 7073b2aab18SMatthew Ahrens fnvlist_free(nvl); 7083b2aab18SMatthew Ahrens return (error); 7093b2aab18SMatthew Ahrens } 71092241e0bSTom Erickson 7113b2aab18SMatthew Ahrens int 7123b2aab18SMatthew Ahrens dsl_prop_inherit(const char *dsname, const char *propname, 7133b2aab18SMatthew Ahrens zprop_source_t source) 7143b2aab18SMatthew Ahrens { 7153b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc(); 7163b2aab18SMatthew Ahrens int error; 717bb0ade09Sahrens 7183b2aab18SMatthew Ahrens fnvlist_add_boolean(nvl, propname); 7193b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl); 7203b2aab18SMatthew Ahrens fnvlist_free(nvl); 7213b2aab18SMatthew Ahrens return (error); 722fa9e4066Sahrens } 7237f7322feSeschrock 7243b2aab18SMatthew Ahrens typedef struct dsl_props_set_arg { 7253b2aab18SMatthew Ahrens const char *dpsa_dsname; 7263b2aab18SMatthew Ahrens zprop_source_t dpsa_source; 7273b2aab18SMatthew Ahrens nvlist_t *dpsa_props; 7283b2aab18SMatthew Ahrens } dsl_props_set_arg_t; 7293b2aab18SMatthew Ahrens 7303b2aab18SMatthew Ahrens static int 7313b2aab18SMatthew Ahrens dsl_props_set_check(void *arg, dmu_tx_t *tx) 7325c0b6a79SRich Morris { 7333b2aab18SMatthew Ahrens dsl_props_set_arg_t *dpsa = arg; 7343b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 7355c0b6a79SRich Morris dsl_dataset_t *ds; 736478ed9adSEric Taylor uint64_t version; 7375c0b6a79SRich Morris nvpair_t *elem = NULL; 7385c0b6a79SRich Morris int err; 7395c0b6a79SRich Morris 7403b2aab18SMatthew Ahrens err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds); 7413b2aab18SMatthew Ahrens if (err != 0) 742478ed9adSEric Taylor return (err); 7433b2aab18SMatthew Ahrens 744478ed9adSEric Taylor version = spa_version(ds->ds_dir->dd_pool->dp_spa); 7453b2aab18SMatthew Ahrens while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) { 746478ed9adSEric Taylor if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 747478ed9adSEric Taylor dsl_dataset_rele(ds, FTAG); 748be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 749478ed9adSEric Taylor } 7505c0b6a79SRich Morris if (nvpair_type(elem) == DATA_TYPE_STRING) { 7513b2aab18SMatthew Ahrens char *valstr = fnvpair_value_string(elem); 752478ed9adSEric Taylor if (strlen(valstr) >= (version < 753478ed9adSEric Taylor SPA_VERSION_STMF_PROP ? 754478ed9adSEric Taylor ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) { 755478ed9adSEric Taylor dsl_dataset_rele(ds, FTAG); 756ccba0801SRich Morris return (E2BIG); 757478ed9adSEric Taylor } 7585c0b6a79SRich Morris } 7595c0b6a79SRich Morris } 7605c0b6a79SRich Morris 761*bc9014e6SJustin Gibbs if (ds->ds_is_snapshot && version < SPA_VERSION_SNAP_PROPS) { 7625c0b6a79SRich Morris dsl_dataset_rele(ds, FTAG); 763be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 7645c0b6a79SRich Morris } 7653b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 7663b2aab18SMatthew Ahrens return (0); 7673b2aab18SMatthew Ahrens } 7685c0b6a79SRich Morris 7693b2aab18SMatthew Ahrens void 7703b2aab18SMatthew Ahrens dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source, 7713b2aab18SMatthew Ahrens nvlist_t *props, dmu_tx_t *tx) 7723b2aab18SMatthew Ahrens { 7733b2aab18SMatthew Ahrens nvpair_t *elem = NULL; 77492241e0bSTom Erickson 7753b2aab18SMatthew Ahrens while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 7763b2aab18SMatthew Ahrens nvpair_t *pair = elem; 7775c0b6a79SRich Morris 7783b2aab18SMatthew Ahrens if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 7793b2aab18SMatthew Ahrens /* 7803b2aab18SMatthew Ahrens * dsl_prop_get_all_impl() returns properties in this 7813b2aab18SMatthew Ahrens * format. 7823b2aab18SMatthew Ahrens */ 7833b2aab18SMatthew Ahrens nvlist_t *attrs = fnvpair_value_nvlist(pair); 7843b2aab18SMatthew Ahrens pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE); 7853b2aab18SMatthew Ahrens } 7863b2aab18SMatthew Ahrens 7873b2aab18SMatthew Ahrens if (nvpair_type(pair) == DATA_TYPE_STRING) { 7883b2aab18SMatthew Ahrens const char *value = fnvpair_value_string(pair); 7893b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair), 7903b2aab18SMatthew Ahrens source, 1, strlen(value) + 1, value, tx); 7913b2aab18SMatthew Ahrens } else if (nvpair_type(pair) == DATA_TYPE_UINT64) { 7923b2aab18SMatthew Ahrens uint64_t intval = fnvpair_value_uint64(pair); 7933b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair), 7943b2aab18SMatthew Ahrens source, sizeof (intval), 1, &intval, tx); 7953b2aab18SMatthew Ahrens } else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) { 7963b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair), 7973b2aab18SMatthew Ahrens source, 0, 0, NULL, tx); 7983b2aab18SMatthew Ahrens } else { 7993b2aab18SMatthew Ahrens panic("invalid nvpair type"); 8003b2aab18SMatthew Ahrens } 8013b2aab18SMatthew Ahrens } 8023b2aab18SMatthew Ahrens } 8033b2aab18SMatthew Ahrens 8043b2aab18SMatthew Ahrens static void 8053b2aab18SMatthew Ahrens dsl_props_set_sync(void *arg, dmu_tx_t *tx) 8063b2aab18SMatthew Ahrens { 8073b2aab18SMatthew Ahrens dsl_props_set_arg_t *dpsa = arg; 8083b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 8093b2aab18SMatthew Ahrens dsl_dataset_t *ds; 8103b2aab18SMatthew Ahrens 8113b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds)); 8123b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx); 8135c0b6a79SRich Morris dsl_dataset_rele(ds, FTAG); 8143b2aab18SMatthew Ahrens } 8153b2aab18SMatthew Ahrens 8163b2aab18SMatthew Ahrens /* 8173b2aab18SMatthew Ahrens * All-or-nothing; if any prop can't be set, nothing will be modified. 8183b2aab18SMatthew Ahrens */ 8193b2aab18SMatthew Ahrens int 8203b2aab18SMatthew Ahrens dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props) 8213b2aab18SMatthew Ahrens { 8223b2aab18SMatthew Ahrens dsl_props_set_arg_t dpsa; 8233b2aab18SMatthew Ahrens int nblks = 0; 8243b2aab18SMatthew Ahrens 8253b2aab18SMatthew Ahrens dpsa.dpsa_dsname = dsname; 8263b2aab18SMatthew Ahrens dpsa.dpsa_source = source; 8273b2aab18SMatthew Ahrens dpsa.dpsa_props = props; 8283b2aab18SMatthew Ahrens 8293b2aab18SMatthew Ahrens /* 8303b2aab18SMatthew Ahrens * If the source includes NONE, then we will only be removing entries 8313b2aab18SMatthew Ahrens * from the ZAP object. In that case don't check for ENOSPC. 8323b2aab18SMatthew Ahrens */ 8333b2aab18SMatthew Ahrens if ((source & ZPROP_SRC_NONE) == 0) 8343b2aab18SMatthew Ahrens nblks = 2 * fnvlist_num_pairs(props); 8353b2aab18SMatthew Ahrens 8363b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync, 8377d46dc6cSMatthew Ahrens &dpsa, nblks, ZFS_SPACE_CHECK_RESERVED)); 8385c0b6a79SRich Morris } 8395c0b6a79SRich Morris 84092241e0bSTom Erickson typedef enum dsl_prop_getflags { 84192241e0bSTom Erickson DSL_PROP_GET_INHERITING = 0x1, /* searching parent of target ds */ 84292241e0bSTom Erickson DSL_PROP_GET_SNAPSHOT = 0x2, /* snapshot dataset */ 84392241e0bSTom Erickson DSL_PROP_GET_LOCAL = 0x4, /* local properties */ 84492241e0bSTom Erickson DSL_PROP_GET_RECEIVED = 0x8 /* received properties */ 84592241e0bSTom Erickson } dsl_prop_getflags_t; 84692241e0bSTom Erickson 84792241e0bSTom Erickson static int 84892241e0bSTom Erickson dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj, 84992241e0bSTom Erickson const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv) 85092241e0bSTom Erickson { 85192241e0bSTom Erickson zap_cursor_t zc; 85292241e0bSTom Erickson zap_attribute_t za; 85392241e0bSTom Erickson int err = 0; 85492241e0bSTom Erickson 85592241e0bSTom Erickson for (zap_cursor_init(&zc, mos, propobj); 85692241e0bSTom Erickson (err = zap_cursor_retrieve(&zc, &za)) == 0; 85792241e0bSTom Erickson zap_cursor_advance(&zc)) { 85892241e0bSTom Erickson nvlist_t *propval; 85992241e0bSTom Erickson zfs_prop_t prop; 86092241e0bSTom Erickson char buf[ZAP_MAXNAMELEN]; 86192241e0bSTom Erickson char *valstr; 86292241e0bSTom Erickson const char *suffix; 86392241e0bSTom Erickson const char *propname; 86492241e0bSTom Erickson const char *source; 86592241e0bSTom Erickson 86692241e0bSTom Erickson suffix = strchr(za.za_name, '$'); 86792241e0bSTom Erickson 86892241e0bSTom Erickson if (suffix == NULL) { 86992241e0bSTom Erickson /* 87092241e0bSTom Erickson * Skip local properties if we only want received 87192241e0bSTom Erickson * properties. 87292241e0bSTom Erickson */ 87392241e0bSTom Erickson if (flags & DSL_PROP_GET_RECEIVED) 87492241e0bSTom Erickson continue; 87592241e0bSTom Erickson 87692241e0bSTom Erickson propname = za.za_name; 87792241e0bSTom Erickson source = setpoint; 87892241e0bSTom Erickson } else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) { 87992241e0bSTom Erickson /* Skip explicitly inherited entries. */ 88092241e0bSTom Erickson continue; 88192241e0bSTom Erickson } else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) { 88292241e0bSTom Erickson if (flags & DSL_PROP_GET_LOCAL) 88392241e0bSTom Erickson continue; 88492241e0bSTom Erickson 88592241e0bSTom Erickson (void) strncpy(buf, za.za_name, (suffix - za.za_name)); 88692241e0bSTom Erickson buf[suffix - za.za_name] = '\0'; 88792241e0bSTom Erickson propname = buf; 88892241e0bSTom Erickson 88992241e0bSTom Erickson if (!(flags & DSL_PROP_GET_RECEIVED)) { 89092241e0bSTom Erickson /* Skip if locally overridden. */ 89192241e0bSTom Erickson err = zap_contains(mos, propobj, propname); 89292241e0bSTom Erickson if (err == 0) 89392241e0bSTom Erickson continue; 89492241e0bSTom Erickson if (err != ENOENT) 89592241e0bSTom Erickson break; 89692241e0bSTom Erickson 89792241e0bSTom Erickson /* Skip if explicitly inherited. */ 89892241e0bSTom Erickson valstr = kmem_asprintf("%s%s", propname, 89992241e0bSTom Erickson ZPROP_INHERIT_SUFFIX); 90092241e0bSTom Erickson err = zap_contains(mos, propobj, valstr); 90192241e0bSTom Erickson strfree(valstr); 90292241e0bSTom Erickson if (err == 0) 90392241e0bSTom Erickson continue; 90492241e0bSTom Erickson if (err != ENOENT) 90592241e0bSTom Erickson break; 90692241e0bSTom Erickson } 90792241e0bSTom Erickson 90892241e0bSTom Erickson source = ((flags & DSL_PROP_GET_INHERITING) ? 90992241e0bSTom Erickson setpoint : ZPROP_SOURCE_VAL_RECVD); 91092241e0bSTom Erickson } else { 91192241e0bSTom Erickson /* 91292241e0bSTom Erickson * For backward compatibility, skip suffixes we don't 91392241e0bSTom Erickson * recognize. 91492241e0bSTom Erickson */ 91592241e0bSTom Erickson continue; 91692241e0bSTom Erickson } 91792241e0bSTom Erickson 91892241e0bSTom Erickson prop = zfs_name_to_prop(propname); 91992241e0bSTom Erickson 92092241e0bSTom Erickson /* Skip non-inheritable properties. */ 92192241e0bSTom Erickson if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL && 92292241e0bSTom Erickson !zfs_prop_inheritable(prop)) 92392241e0bSTom Erickson continue; 92492241e0bSTom Erickson 92592241e0bSTom Erickson /* Skip properties not valid for this type. */ 92692241e0bSTom Erickson if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL && 92792241e0bSTom Erickson !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT)) 92892241e0bSTom Erickson continue; 92992241e0bSTom Erickson 93092241e0bSTom Erickson /* Skip properties already defined. */ 93192241e0bSTom Erickson if (nvlist_exists(nv, propname)) 93292241e0bSTom Erickson continue; 93392241e0bSTom Erickson 93492241e0bSTom Erickson VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 93592241e0bSTom Erickson if (za.za_integer_length == 1) { 93692241e0bSTom Erickson /* 93792241e0bSTom Erickson * String property 93892241e0bSTom Erickson */ 93992241e0bSTom Erickson char *tmp = kmem_alloc(za.za_num_integers, 94092241e0bSTom Erickson KM_SLEEP); 94192241e0bSTom Erickson err = zap_lookup(mos, propobj, 94292241e0bSTom Erickson za.za_name, 1, za.za_num_integers, tmp); 94392241e0bSTom Erickson if (err != 0) { 94492241e0bSTom Erickson kmem_free(tmp, za.za_num_integers); 94592241e0bSTom Erickson break; 94692241e0bSTom Erickson } 94792241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_VALUE, 94892241e0bSTom Erickson tmp) == 0); 94992241e0bSTom Erickson kmem_free(tmp, za.za_num_integers); 95092241e0bSTom Erickson } else { 95192241e0bSTom Erickson /* 95292241e0bSTom Erickson * Integer property 95392241e0bSTom Erickson */ 95492241e0bSTom Erickson ASSERT(za.za_integer_length == 8); 95592241e0bSTom Erickson (void) nvlist_add_uint64(propval, ZPROP_VALUE, 95692241e0bSTom Erickson za.za_first_integer); 95792241e0bSTom Erickson } 95892241e0bSTom Erickson 95992241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0); 96092241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 96192241e0bSTom Erickson nvlist_free(propval); 96292241e0bSTom Erickson } 96392241e0bSTom Erickson zap_cursor_fini(&zc); 96492241e0bSTom Erickson if (err == ENOENT) 96592241e0bSTom Erickson err = 0; 96692241e0bSTom Erickson return (err); 96792241e0bSTom Erickson } 96892241e0bSTom Erickson 9697f7322feSeschrock /* 9707f7322feSeschrock * Iterate over all properties for this dataset and return them in an nvlist. 9717f7322feSeschrock */ 97292241e0bSTom Erickson static int 97392241e0bSTom Erickson dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp, 97492241e0bSTom Erickson dsl_prop_getflags_t flags) 9757f7322feSeschrock { 97699653d4eSeschrock dsl_dir_t *dd = ds->ds_dir; 977bb0ade09Sahrens dsl_pool_t *dp = dd->dd_pool; 978bb0ade09Sahrens objset_t *mos = dp->dp_meta_objset; 97992241e0bSTom Erickson int err = 0; 98092241e0bSTom Erickson char setpoint[MAXNAMELEN]; 9817f7322feSeschrock 9827f7322feSeschrock VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 9837f7322feSeschrock 984*bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) 98592241e0bSTom Erickson flags |= DSL_PROP_GET_SNAPSHOT; 9867f7322feSeschrock 9873b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 98892241e0bSTom Erickson 989c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_props_obj != 0) { 99092241e0bSTom Erickson ASSERT(flags & DSL_PROP_GET_SNAPSHOT); 99192241e0bSTom Erickson dsl_dataset_name(ds, setpoint); 992c1379625SJustin T. Gibbs err = dsl_prop_get_all_impl(mos, 993c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_props_obj, setpoint, flags, *nvp); 99492241e0bSTom Erickson if (err) 99592241e0bSTom Erickson goto out; 99692241e0bSTom Erickson } 99792241e0bSTom Erickson 99892241e0bSTom Erickson for (; dd != NULL; dd = dd->dd_parent) { 99992241e0bSTom Erickson if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) { 100092241e0bSTom Erickson if (flags & (DSL_PROP_GET_LOCAL | 100192241e0bSTom Erickson DSL_PROP_GET_RECEIVED)) 100292241e0bSTom Erickson break; 100392241e0bSTom Erickson flags |= DSL_PROP_GET_INHERITING; 1004bb0ade09Sahrens } 100592241e0bSTom Erickson dsl_dir_name(dd, setpoint); 1006c1379625SJustin T. Gibbs err = dsl_prop_get_all_impl(mos, 1007c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_props_zapobj, setpoint, flags, *nvp); 100892241e0bSTom Erickson if (err) 100992241e0bSTom Erickson break; 101092241e0bSTom Erickson } 101192241e0bSTom Erickson out: 101292241e0bSTom Erickson return (err); 101392241e0bSTom Erickson } 1014a2eea2e1Sahrens 101592241e0bSTom Erickson boolean_t 10163b2aab18SMatthew Ahrens dsl_prop_get_hasrecvd(const char *dsname) 101792241e0bSTom Erickson { 101892241e0bSTom Erickson uint64_t dummy; 1019bb0ade09Sahrens 10203b2aab18SMatthew Ahrens return (0 == 10213b2aab18SMatthew Ahrens dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL)); 102292241e0bSTom Erickson } 1023e9dbad6fSeschrock 10243b2aab18SMatthew Ahrens static int 10253b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source) 102692241e0bSTom Erickson { 10273b2aab18SMatthew Ahrens uint64_t version; 10283b2aab18SMatthew Ahrens spa_t *spa; 10293b2aab18SMatthew Ahrens int error = 0; 10307f7322feSeschrock 10313b2aab18SMatthew Ahrens VERIFY0(spa_open(dsname, &spa, FTAG)); 10323b2aab18SMatthew Ahrens version = spa_version(spa); 10333b2aab18SMatthew Ahrens spa_close(spa, FTAG); 10347f7322feSeschrock 10353b2aab18SMatthew Ahrens if (version >= SPA_VERSION_RECVD_PROPS) 10363b2aab18SMatthew Ahrens error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0); 10373b2aab18SMatthew Ahrens return (error); 103892241e0bSTom Erickson } 10397f7322feSeschrock 104092241e0bSTom Erickson /* 104192241e0bSTom Erickson * Call after successfully receiving properties to ensure that only the first 104292241e0bSTom Erickson * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties. 104392241e0bSTom Erickson */ 10443b2aab18SMatthew Ahrens int 10453b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd(const char *dsname) 104692241e0bSTom Erickson { 10473b2aab18SMatthew Ahrens int error = 0; 10483b2aab18SMatthew Ahrens if (!dsl_prop_get_hasrecvd(dsname)) 10493b2aab18SMatthew Ahrens error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL); 10503b2aab18SMatthew Ahrens return (error); 105192241e0bSTom Erickson } 10527f7322feSeschrock 105392241e0bSTom Erickson void 10543b2aab18SMatthew Ahrens dsl_prop_unset_hasrecvd(const char *dsname) 105592241e0bSTom Erickson { 10563b2aab18SMatthew Ahrens VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE)); 105792241e0bSTom Erickson } 105892241e0bSTom Erickson 105992241e0bSTom Erickson int 106092241e0bSTom Erickson dsl_prop_get_all(objset_t *os, nvlist_t **nvp) 106192241e0bSTom Erickson { 106292241e0bSTom Erickson return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0)); 106392241e0bSTom Erickson } 106492241e0bSTom Erickson 106592241e0bSTom Erickson int 10663b2aab18SMatthew Ahrens dsl_prop_get_received(const char *dsname, nvlist_t **nvp) 106792241e0bSTom Erickson { 10683b2aab18SMatthew Ahrens objset_t *os; 10693b2aab18SMatthew Ahrens int error; 10703b2aab18SMatthew Ahrens 107192241e0bSTom Erickson /* 107292241e0bSTom Erickson * Received properties are not distinguishable from local properties 107392241e0bSTom Erickson * until the dataset has received properties on or after 107492241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS. 107592241e0bSTom Erickson */ 10763b2aab18SMatthew Ahrens dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ? 107792241e0bSTom Erickson DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL); 10783b2aab18SMatthew Ahrens 10793b2aab18SMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os); 10803b2aab18SMatthew Ahrens if (error != 0) 10813b2aab18SMatthew Ahrens return (error); 10823b2aab18SMatthew Ahrens error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags); 10833b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 10843b2aab18SMatthew Ahrens return (error); 10857f7322feSeschrock } 1086a2eea2e1Sahrens 1087a2eea2e1Sahrens void 1088a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value) 1089a2eea2e1Sahrens { 1090a2eea2e1Sahrens nvlist_t *propval; 109192241e0bSTom Erickson const char *propname = zfs_prop_to_name(prop); 109292241e0bSTom Erickson uint64_t default_value; 109392241e0bSTom Erickson 109492241e0bSTom Erickson if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) { 109592241e0bSTom Erickson VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0); 109692241e0bSTom Erickson return; 109792241e0bSTom Erickson } 1098a2eea2e1Sahrens 1099a2eea2e1Sahrens VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1100990b4856Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0); 110192241e0bSTom Erickson /* Indicate the default source if we can. */ 110292241e0bSTom Erickson if (dodefault(propname, 8, 1, &default_value) == 0 && 110392241e0bSTom Erickson value == default_value) { 110492241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0); 110592241e0bSTom Erickson } 110692241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 1107a2eea2e1Sahrens nvlist_free(propval); 1108a2eea2e1Sahrens } 1109a2eea2e1Sahrens 1110a2eea2e1Sahrens void 1111a2eea2e1Sahrens dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value) 1112a2eea2e1Sahrens { 1113a2eea2e1Sahrens nvlist_t *propval; 111492241e0bSTom Erickson const char *propname = zfs_prop_to_name(prop); 111592241e0bSTom Erickson 111692241e0bSTom Erickson if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) { 111792241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0); 111892241e0bSTom Erickson return; 111992241e0bSTom Erickson } 1120a2eea2e1Sahrens 1121a2eea2e1Sahrens VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1122990b4856Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0); 112392241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 1124a2eea2e1Sahrens nvlist_free(propval); 1125a2eea2e1Sahrens } 1126