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*be6fd75aSMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 2692241e0bSTom Erickson #include <sys/zfs_context.h> 27fa9e4066Sahrens #include <sys/dmu.h> 287f7322feSeschrock #include <sys/dmu_objset.h> 29fa9e4066Sahrens #include <sys/dmu_tx.h> 30fa9e4066Sahrens #include <sys/dsl_dataset.h> 31fa9e4066Sahrens #include <sys/dsl_dir.h> 32fa9e4066Sahrens #include <sys/dsl_prop.h> 331d452cf5Sahrens #include <sys/dsl_synctask.h> 34fa9e4066Sahrens #include <sys/spa.h> 35fa9e4066Sahrens #include <sys/zap.h> 36fa9e4066Sahrens #include <sys/fs/zfs.h> 37fa9e4066Sahrens 38fa9e4066Sahrens #include "zfs_prop.h" 39fa9e4066Sahrens 4092241e0bSTom Erickson #define ZPROP_INHERIT_SUFFIX "$inherit" 4192241e0bSTom Erickson #define ZPROP_RECVD_SUFFIX "$recvd" 4292241e0bSTom Erickson 43fa9e4066Sahrens static int 4492241e0bSTom Erickson dodefault(const char *propname, int intsz, int numints, void *buf) 45fa9e4066Sahrens { 46fa9e4066Sahrens zfs_prop_t prop; 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 */ 53990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL || 54da6c28aaSamw (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop))) 55*be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 56fa9e4066Sahrens 5791ebeef5Sahrens if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 58fa9e4066Sahrens if (intsz != 1) 59*be6fd75aSMatthew 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) 64*be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 65fa9e4066Sahrens 66fa9e4066Sahrens *(uint64_t *)buf = zfs_prop_default_numeric(prop); 67fa9e4066Sahrens } 68fa9e4066Sahrens 69fa9e4066Sahrens return (0); 70fa9e4066Sahrens } 71fa9e4066Sahrens 72bb0ade09Sahrens int 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. */ 10792241e0bSTom Erickson err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname, 10892241e0bSTom Erickson 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 */ 11992241e0bSTom Erickson err = zap_contains(mos, dd->dd_phys->dd_props_zapobj, 12092241e0bSTom Erickson inheritstr); 12192241e0bSTom Erickson if (err != 0 && err != ENOENT) 122e9dbad6fSeschrock break; 12392241e0bSTom Erickson 12492241e0bSTom Erickson if (err == ENOENT) { 12592241e0bSTom Erickson /* Check for a received value. */ 12692241e0bSTom Erickson err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, 12792241e0bSTom Erickson recvdstr, intsz, numints, buf); 12892241e0bSTom Erickson if (err != ENOENT) { 12992241e0bSTom Erickson if (setpoint != NULL && err == 0) { 13092241e0bSTom Erickson if (inheriting) { 13192241e0bSTom Erickson dsl_dir_name(dd, setpoint); 13292241e0bSTom Erickson } else { 13392241e0bSTom Erickson (void) strcpy(setpoint, 13492241e0bSTom Erickson ZPROP_SOURCE_VAL_RECVD); 13592241e0bSTom Erickson } 13692241e0bSTom Erickson } 13792241e0bSTom Erickson break; 13892241e0bSTom Erickson } 13992241e0bSTom Erickson } 14092241e0bSTom Erickson 14192241e0bSTom Erickson /* 14292241e0bSTom Erickson * If we found an explicit inheritance entry, err is zero even 14392241e0bSTom Erickson * though we haven't yet found the value, so reinitializing err 14492241e0bSTom Erickson * at the end of the loop (instead of at the beginning) ensures 14592241e0bSTom Erickson * that err has a valid post-loop value. 14692241e0bSTom Erickson */ 147*be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 148fa9e4066Sahrens } 14992241e0bSTom Erickson 150fa9e4066Sahrens if (err == ENOENT) 15192241e0bSTom Erickson err = dodefault(propname, intsz, numints, buf); 15292241e0bSTom Erickson 15392241e0bSTom Erickson strfree(inheritstr); 15492241e0bSTom Erickson strfree(recvdstr); 155fa9e4066Sahrens 156fa9e4066Sahrens return (err); 157fa9e4066Sahrens } 158fa9e4066Sahrens 159bb0ade09Sahrens int 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 boolean_t snapshot; 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)); 17092241e0bSTom Erickson snapshot = (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds)); 17192241e0bSTom Erickson zapobj = (ds->ds_phys == NULL ? 0 : ds->ds_phys->ds_props_obj); 17292241e0bSTom Erickson 17392241e0bSTom Erickson if (zapobj != 0) { 17492241e0bSTom Erickson objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 17592241e0bSTom Erickson int err; 176bb0ade09Sahrens 17792241e0bSTom Erickson ASSERT(snapshot); 17892241e0bSTom Erickson 17992241e0bSTom Erickson /* Check for a local value. */ 18092241e0bSTom Erickson err = zap_lookup(mos, zapobj, propname, intsz, numints, buf); 181bb0ade09Sahrens if (err != ENOENT) { 18292241e0bSTom Erickson if (setpoint != NULL && err == 0) 183bb0ade09Sahrens dsl_dataset_name(ds, setpoint); 184bb0ade09Sahrens return (err); 185bb0ade09Sahrens } 18692241e0bSTom Erickson 18792241e0bSTom Erickson /* 18892241e0bSTom Erickson * Skip the check for a received value if there is an explicit 18992241e0bSTom Erickson * inheritance entry. 19092241e0bSTom Erickson */ 19192241e0bSTom Erickson if (inheritable) { 19292241e0bSTom Erickson char *inheritstr = kmem_asprintf("%s%s", propname, 19392241e0bSTom Erickson ZPROP_INHERIT_SUFFIX); 19492241e0bSTom Erickson err = zap_contains(mos, zapobj, inheritstr); 19592241e0bSTom Erickson strfree(inheritstr); 19692241e0bSTom Erickson if (err != 0 && err != ENOENT) 19792241e0bSTom Erickson return (err); 19892241e0bSTom Erickson } 19992241e0bSTom Erickson 20092241e0bSTom Erickson if (err == ENOENT) { 20192241e0bSTom Erickson /* Check for a received value. */ 20292241e0bSTom Erickson char *recvdstr = kmem_asprintf("%s%s", propname, 20392241e0bSTom Erickson ZPROP_RECVD_SUFFIX); 20492241e0bSTom Erickson err = zap_lookup(mos, zapobj, recvdstr, 20592241e0bSTom Erickson intsz, numints, buf); 20692241e0bSTom Erickson strfree(recvdstr); 20792241e0bSTom Erickson if (err != ENOENT) { 20892241e0bSTom Erickson if (setpoint != NULL && err == 0) 20992241e0bSTom Erickson (void) strcpy(setpoint, 21092241e0bSTom Erickson ZPROP_SOURCE_VAL_RECVD); 21192241e0bSTom Erickson return (err); 21292241e0bSTom Erickson } 21392241e0bSTom Erickson } 214bb0ade09Sahrens } 215bb0ade09Sahrens 216bb0ade09Sahrens return (dsl_prop_get_dd(ds->ds_dir, propname, 21792241e0bSTom Erickson intsz, numints, buf, setpoint, snapshot)); 218bb0ade09Sahrens } 219bb0ade09Sahrens 220fa9e4066Sahrens /* 221fa9e4066Sahrens * Register interest in the named property. We'll call the callback 222fa9e4066Sahrens * once to notify it of the current property value, and again each time 223fa9e4066Sahrens * the property changes, until this callback is unregistered. 224fa9e4066Sahrens * 225fa9e4066Sahrens * Return 0 on success, errno if the prop is not an integer value. 226fa9e4066Sahrens */ 227fa9e4066Sahrens int 228fa9e4066Sahrens dsl_prop_register(dsl_dataset_t *ds, const char *propname, 229fa9e4066Sahrens dsl_prop_changed_cb_t *callback, void *cbarg) 230fa9e4066Sahrens { 23199653d4eSeschrock dsl_dir_t *dd = ds->ds_dir; 232bb0ade09Sahrens dsl_pool_t *dp = dd->dd_pool; 233fa9e4066Sahrens uint64_t value; 234fa9e4066Sahrens dsl_prop_cb_record_t *cbr; 235fa9e4066Sahrens int err; 236fa9e4066Sahrens 2373b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 238fa9e4066Sahrens 2393b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, propname, &value); 2403b2aab18SMatthew Ahrens if (err != 0) 241fa9e4066Sahrens return (err); 242fa9e4066Sahrens 243fa9e4066Sahrens cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP); 24499653d4eSeschrock cbr->cbr_ds = ds; 245fa9e4066Sahrens cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP); 246fa9e4066Sahrens (void) strcpy((char *)cbr->cbr_propname, propname); 247fa9e4066Sahrens cbr->cbr_func = callback; 248fa9e4066Sahrens cbr->cbr_arg = cbarg; 249fa9e4066Sahrens mutex_enter(&dd->dd_lock); 250fa9e4066Sahrens list_insert_head(&dd->dd_prop_cbs, cbr); 251fa9e4066Sahrens mutex_exit(&dd->dd_lock); 252fa9e4066Sahrens 253fa9e4066Sahrens cbr->cbr_func(cbr->cbr_arg, value); 254fa9e4066Sahrens return (0); 255fa9e4066Sahrens } 256fa9e4066Sahrens 257fa9e4066Sahrens int 258bb0ade09Sahrens dsl_prop_get(const char *dsname, const char *propname, 259fa9e4066Sahrens int intsz, int numints, void *buf, char *setpoint) 260fa9e4066Sahrens { 2613b2aab18SMatthew Ahrens objset_t *os; 2623b2aab18SMatthew Ahrens int error; 263fa9e4066Sahrens 2643b2aab18SMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os); 2653b2aab18SMatthew Ahrens if (error != 0) 2663b2aab18SMatthew Ahrens return (error); 267fa9e4066Sahrens 2683b2aab18SMatthew Ahrens error = dsl_prop_get_ds(dmu_objset_ds(os), propname, 2693b2aab18SMatthew Ahrens intsz, numints, buf, setpoint); 270fa9e4066Sahrens 2713b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 2723b2aab18SMatthew Ahrens return (error); 273fa9e4066Sahrens } 274fa9e4066Sahrens 275fa9e4066Sahrens /* 276fa9e4066Sahrens * Get the current property value. It may have changed by the time this 277fa9e4066Sahrens * function returns, so it is NOT safe to follow up with 278fa9e4066Sahrens * dsl_prop_register() and assume that the value has not changed in 279fa9e4066Sahrens * between. 280fa9e4066Sahrens * 281fa9e4066Sahrens * Return 0 on success, ENOENT if ddname is invalid. 282fa9e4066Sahrens */ 283fa9e4066Sahrens int 284fa9e4066Sahrens dsl_prop_get_integer(const char *ddname, const char *propname, 285fa9e4066Sahrens uint64_t *valuep, char *setpoint) 286fa9e4066Sahrens { 287fa9e4066Sahrens return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint)); 288fa9e4066Sahrens } 289fa9e4066Sahrens 2903b2aab18SMatthew Ahrens int 2913b2aab18SMatthew Ahrens dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname, 2923b2aab18SMatthew Ahrens uint64_t *valuep) 29392241e0bSTom Erickson { 2943b2aab18SMatthew Ahrens return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL)); 29592241e0bSTom Erickson } 29692241e0bSTom Erickson 29792241e0bSTom Erickson /* 29892241e0bSTom Erickson * Predict the effective value of the given special property if it were set with 29992241e0bSTom Erickson * the given value and source. This is not a general purpose function. It exists 30092241e0bSTom Erickson * only to handle the special requirements of the quota and reservation 30192241e0bSTom Erickson * properties. The fact that these properties are non-inheritable greatly 30292241e0bSTom Erickson * simplifies the prediction logic. 30392241e0bSTom Erickson * 30492241e0bSTom Erickson * Returns 0 on success, a positive error code on failure, or -1 if called with 30592241e0bSTom Erickson * a property not handled by this function. 30692241e0bSTom Erickson */ 30792241e0bSTom Erickson int 3083b2aab18SMatthew Ahrens dsl_prop_predict(dsl_dir_t *dd, const char *propname, 3093b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, uint64_t *newvalp) 31092241e0bSTom Erickson { 31192241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 31292241e0bSTom Erickson objset_t *mos; 31392241e0bSTom Erickson uint64_t zapobj; 31492241e0bSTom Erickson uint64_t version; 31592241e0bSTom Erickson char *recvdstr; 31692241e0bSTom Erickson int err = 0; 31792241e0bSTom Erickson 31892241e0bSTom Erickson switch (prop) { 31992241e0bSTom Erickson case ZFS_PROP_QUOTA: 32092241e0bSTom Erickson case ZFS_PROP_RESERVATION: 32192241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 32292241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 32392241e0bSTom Erickson break; 32492241e0bSTom Erickson default: 32592241e0bSTom Erickson return (-1); 32692241e0bSTom Erickson } 32792241e0bSTom Erickson 32892241e0bSTom Erickson mos = dd->dd_pool->dp_meta_objset; 32992241e0bSTom Erickson zapobj = dd->dd_phys->dd_props_zapobj; 33092241e0bSTom Erickson recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX); 33192241e0bSTom Erickson 33292241e0bSTom Erickson version = spa_version(dd->dd_pool->dp_spa); 33392241e0bSTom Erickson if (version < SPA_VERSION_RECVD_PROPS) { 33492241e0bSTom Erickson if (source & ZPROP_SRC_NONE) 33592241e0bSTom Erickson source = ZPROP_SRC_NONE; 33692241e0bSTom Erickson else if (source & ZPROP_SRC_RECEIVED) 33792241e0bSTom Erickson source = ZPROP_SRC_LOCAL; 33892241e0bSTom Erickson } 33992241e0bSTom Erickson 34092241e0bSTom Erickson switch (source) { 34192241e0bSTom Erickson case ZPROP_SRC_NONE: 34292241e0bSTom Erickson /* Revert to the received value, if any. */ 3433b2aab18SMatthew Ahrens err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp); 34492241e0bSTom Erickson if (err == ENOENT) 3453b2aab18SMatthew Ahrens *newvalp = 0; 34692241e0bSTom Erickson break; 34792241e0bSTom Erickson case ZPROP_SRC_LOCAL: 3483b2aab18SMatthew Ahrens *newvalp = value; 34992241e0bSTom Erickson break; 35092241e0bSTom Erickson case ZPROP_SRC_RECEIVED: 35192241e0bSTom Erickson /* 35292241e0bSTom Erickson * If there's no local setting, then the new received value will 35392241e0bSTom Erickson * be the effective value. 35492241e0bSTom Erickson */ 3553b2aab18SMatthew Ahrens err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp); 35692241e0bSTom Erickson if (err == ENOENT) 3573b2aab18SMatthew Ahrens *newvalp = value; 35892241e0bSTom Erickson break; 35992241e0bSTom Erickson case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED): 36092241e0bSTom Erickson /* 36192241e0bSTom Erickson * We're clearing the received value, so the local setting (if 36292241e0bSTom Erickson * it exists) remains the effective value. 36392241e0bSTom Erickson */ 3643b2aab18SMatthew Ahrens err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp); 36592241e0bSTom Erickson if (err == ENOENT) 3663b2aab18SMatthew Ahrens *newvalp = 0; 36792241e0bSTom Erickson break; 36892241e0bSTom Erickson default: 3693b2aab18SMatthew Ahrens panic("unexpected property source: %d", source); 37092241e0bSTom Erickson } 37192241e0bSTom Erickson 37292241e0bSTom Erickson strfree(recvdstr); 37392241e0bSTom Erickson 37492241e0bSTom Erickson if (err == ENOENT) 37592241e0bSTom Erickson return (0); 37692241e0bSTom Erickson 37792241e0bSTom Erickson return (err); 37892241e0bSTom Erickson } 37992241e0bSTom Erickson 380fa9e4066Sahrens /* 381fa9e4066Sahrens * Unregister this callback. Return 0 on success, ENOENT if ddname is 382fa9e4066Sahrens * invalid, ENOMSG if no matching callback registered. 383fa9e4066Sahrens */ 384fa9e4066Sahrens int 385fa9e4066Sahrens dsl_prop_unregister(dsl_dataset_t *ds, const char *propname, 386fa9e4066Sahrens dsl_prop_changed_cb_t *callback, void *cbarg) 387fa9e4066Sahrens { 38899653d4eSeschrock dsl_dir_t *dd = ds->ds_dir; 389fa9e4066Sahrens dsl_prop_cb_record_t *cbr; 390fa9e4066Sahrens 391fa9e4066Sahrens mutex_enter(&dd->dd_lock); 392fa9e4066Sahrens for (cbr = list_head(&dd->dd_prop_cbs); 393fa9e4066Sahrens cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) { 39499653d4eSeschrock if (cbr->cbr_ds == ds && 395fa9e4066Sahrens cbr->cbr_func == callback && 39699653d4eSeschrock cbr->cbr_arg == cbarg && 39799653d4eSeschrock strcmp(cbr->cbr_propname, propname) == 0) 398fa9e4066Sahrens break; 399fa9e4066Sahrens } 400fa9e4066Sahrens 401fa9e4066Sahrens if (cbr == NULL) { 402fa9e4066Sahrens mutex_exit(&dd->dd_lock); 403*be6fd75aSMatthew Ahrens return (SET_ERROR(ENOMSG)); 404fa9e4066Sahrens } 405fa9e4066Sahrens 406fa9e4066Sahrens list_remove(&dd->dd_prop_cbs, cbr); 407fa9e4066Sahrens mutex_exit(&dd->dd_lock); 408fa9e4066Sahrens kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1); 409fa9e4066Sahrens kmem_free(cbr, sizeof (dsl_prop_cb_record_t)); 410fa9e4066Sahrens 411fa9e4066Sahrens return (0); 412fa9e4066Sahrens } 413fa9e4066Sahrens 4143b2aab18SMatthew Ahrens boolean_t 4153b2aab18SMatthew Ahrens dsl_prop_hascb(dsl_dataset_t *ds) 41699653d4eSeschrock { 41799653d4eSeschrock dsl_dir_t *dd = ds->ds_dir; 4183b2aab18SMatthew Ahrens boolean_t rv = B_FALSE; 41999653d4eSeschrock dsl_prop_cb_record_t *cbr; 42099653d4eSeschrock 42199653d4eSeschrock mutex_enter(&dd->dd_lock); 4223b2aab18SMatthew Ahrens for (cbr = list_head(&dd->dd_prop_cbs); cbr; 4233b2aab18SMatthew Ahrens cbr = list_next(&dd->dd_prop_cbs, cbr)) { 4243b2aab18SMatthew Ahrens if (cbr->cbr_ds == ds) { 4253b2aab18SMatthew Ahrens rv = B_TRUE; 4263b2aab18SMatthew Ahrens break; 4273b2aab18SMatthew Ahrens } 42899653d4eSeschrock } 42999653d4eSeschrock mutex_exit(&dd->dd_lock); 4303b2aab18SMatthew Ahrens return (rv); 4313b2aab18SMatthew Ahrens } 43299653d4eSeschrock 4333b2aab18SMatthew Ahrens /* ARGSUSED */ 4343b2aab18SMatthew Ahrens static int 4353b2aab18SMatthew Ahrens dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 4363b2aab18SMatthew Ahrens { 4373b2aab18SMatthew Ahrens dsl_dir_t *dd = ds->ds_dir; 4383b2aab18SMatthew Ahrens dsl_prop_cb_record_t *cbr; 4393b2aab18SMatthew Ahrens 4403b2aab18SMatthew Ahrens mutex_enter(&dd->dd_lock); 4413b2aab18SMatthew Ahrens for (cbr = list_head(&dd->dd_prop_cbs); cbr; 4423b2aab18SMatthew Ahrens cbr = list_next(&dd->dd_prop_cbs, cbr)) { 4433b2aab18SMatthew Ahrens uint64_t value; 4443b2aab18SMatthew Ahrens 4453b2aab18SMatthew Ahrens if (dsl_prop_get_ds(cbr->cbr_ds, cbr->cbr_propname, 4463b2aab18SMatthew Ahrens sizeof (value), 1, &value, NULL) == 0) 4473b2aab18SMatthew Ahrens cbr->cbr_func(cbr->cbr_arg, value); 4483b2aab18SMatthew Ahrens } 4493b2aab18SMatthew Ahrens mutex_exit(&dd->dd_lock); 4503b2aab18SMatthew Ahrens 4513b2aab18SMatthew Ahrens return (0); 4523b2aab18SMatthew Ahrens } 4533b2aab18SMatthew Ahrens 4543b2aab18SMatthew Ahrens /* 4553b2aab18SMatthew Ahrens * Update all property values for ddobj & its descendants. This is used 4563b2aab18SMatthew Ahrens * when renaming the dir. 4573b2aab18SMatthew Ahrens */ 4583b2aab18SMatthew Ahrens void 4593b2aab18SMatthew Ahrens dsl_prop_notify_all(dsl_dir_t *dd) 4603b2aab18SMatthew Ahrens { 4613b2aab18SMatthew Ahrens dsl_pool_t *dp = dd->dd_pool; 4623b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 4633b2aab18SMatthew Ahrens (void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb, 4643b2aab18SMatthew Ahrens NULL, DS_FIND_CHILDREN); 46599653d4eSeschrock } 46699653d4eSeschrock 467fa9e4066Sahrens static void 468fa9e4066Sahrens dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj, 469fa9e4066Sahrens const char *propname, uint64_t value, int first) 470fa9e4066Sahrens { 471fa9e4066Sahrens dsl_dir_t *dd; 472fa9e4066Sahrens dsl_prop_cb_record_t *cbr; 473fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 4741d452cf5Sahrens zap_cursor_t zc; 475d8d77200Sahrens zap_attribute_t *za; 476fa9e4066Sahrens int err; 477fa9e4066Sahrens 4783b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 4793b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd); 480ea8dc4b6Seschrock if (err) 481ea8dc4b6Seschrock return; 482fa9e4066Sahrens 483fa9e4066Sahrens if (!first) { 484fa9e4066Sahrens /* 485fa9e4066Sahrens * If the prop is set here, then this change is not 486fa9e4066Sahrens * being inherited here or below; stop the recursion. 487fa9e4066Sahrens */ 48892241e0bSTom Erickson err = zap_contains(mos, dd->dd_phys->dd_props_zapobj, 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)) { 499bb0ade09Sahrens uint64_t propobj = cbr->cbr_ds->ds_phys->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, 5171d452cf5Sahrens dd->dd_phys->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 54692241e0bSTom Erickson if (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds)) { 54792241e0bSTom Erickson ASSERT(version >= SPA_VERSION_SNAP_PROPS); 548bb0ade09Sahrens if (ds->ds_phys->ds_props_obj == 0) { 549bb0ade09Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 550bb0ade09Sahrens ds->ds_phys->ds_props_obj = 551bb0ade09Sahrens zap_create(mos, 552bb0ade09Sahrens DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 553bb0ade09Sahrens } 554bb0ade09Sahrens zapobj = ds->ds_phys->ds_props_obj; 555bb0ade09Sahrens } else { 556bb0ade09Sahrens zapobj = ds->ds_dir->dd_phys->dd_props_zapobj; 557bb0ade09Sahrens } 558bb0ade09Sahrens 55992241e0bSTom Erickson if (version < SPA_VERSION_RECVD_PROPS) { 56092241e0bSTom Erickson zfs_prop_t prop = zfs_name_to_prop(propname); 56192241e0bSTom Erickson if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION) 56292241e0bSTom Erickson return; 56392241e0bSTom Erickson 56492241e0bSTom Erickson if (source & ZPROP_SRC_NONE) 56592241e0bSTom Erickson source = ZPROP_SRC_NONE; 56692241e0bSTom Erickson else if (source & ZPROP_SRC_RECEIVED) 56792241e0bSTom Erickson source = ZPROP_SRC_LOCAL; 56892241e0bSTom Erickson } 56992241e0bSTom Erickson 57092241e0bSTom Erickson inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX); 57192241e0bSTom Erickson recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX); 57292241e0bSTom Erickson 57392241e0bSTom Erickson switch (source) { 57492241e0bSTom Erickson case ZPROP_SRC_NONE: 57592241e0bSTom Erickson /* 57692241e0bSTom Erickson * revert to received value, if any (inherit -S) 57792241e0bSTom Erickson * - remove propname 57892241e0bSTom Erickson * - remove propname$inherit 57992241e0bSTom Erickson */ 58092241e0bSTom Erickson err = zap_remove(mos, zapobj, propname, tx); 58192241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 58292241e0bSTom Erickson err = zap_remove(mos, zapobj, inheritstr, tx); 58392241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 58492241e0bSTom Erickson break; 58592241e0bSTom Erickson case ZPROP_SRC_LOCAL: 58692241e0bSTom Erickson /* 58792241e0bSTom Erickson * remove propname$inherit 58892241e0bSTom Erickson * set propname -> value 58992241e0bSTom Erickson */ 59092241e0bSTom Erickson err = zap_remove(mos, zapobj, inheritstr, tx); 59192241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 5923b2aab18SMatthew Ahrens VERIFY0(zap_update(mos, zapobj, propname, 5933b2aab18SMatthew Ahrens intsz, numints, value, tx)); 59492241e0bSTom Erickson break; 59592241e0bSTom Erickson case ZPROP_SRC_INHERITED: 59692241e0bSTom Erickson /* 59792241e0bSTom Erickson * explicitly inherit 59892241e0bSTom Erickson * - remove propname 59992241e0bSTom Erickson * - set propname$inherit 60092241e0bSTom Erickson */ 60192241e0bSTom Erickson err = zap_remove(mos, zapobj, propname, tx); 6021d452cf5Sahrens ASSERT(err == 0 || err == ENOENT); 60392241e0bSTom Erickson if (version >= SPA_VERSION_RECVD_PROPS && 6043b2aab18SMatthew Ahrens dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) { 60592241e0bSTom Erickson dummy = 0; 6063b2aab18SMatthew Ahrens VERIFY0(zap_update(mos, zapobj, inheritstr, 6073b2aab18SMatthew Ahrens 8, 1, &dummy, tx)); 608fa9e4066Sahrens } 60992241e0bSTom Erickson break; 61092241e0bSTom Erickson case ZPROP_SRC_RECEIVED: 61192241e0bSTom Erickson /* 61292241e0bSTom Erickson * set propname$recvd -> value 61392241e0bSTom Erickson */ 61492241e0bSTom Erickson err = zap_update(mos, zapobj, recvdstr, 6153b2aab18SMatthew Ahrens intsz, numints, value, tx); 61692241e0bSTom Erickson ASSERT(err == 0); 61792241e0bSTom Erickson break; 61892241e0bSTom Erickson case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED): 61992241e0bSTom Erickson /* 62092241e0bSTom Erickson * clear local and received settings 62192241e0bSTom Erickson * - remove propname 62292241e0bSTom Erickson * - remove propname$inherit 62392241e0bSTom Erickson * - remove propname$recvd 62492241e0bSTom Erickson */ 62592241e0bSTom Erickson err = zap_remove(mos, zapobj, propname, tx); 62692241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 62792241e0bSTom Erickson err = zap_remove(mos, zapobj, inheritstr, tx); 62892241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 62992241e0bSTom Erickson /* FALLTHRU */ 63092241e0bSTom Erickson case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED): 63192241e0bSTom Erickson /* 63292241e0bSTom Erickson * remove propname$recvd 63392241e0bSTom Erickson */ 63492241e0bSTom Erickson err = zap_remove(mos, zapobj, recvdstr, tx); 63592241e0bSTom Erickson ASSERT(err == 0 || err == ENOENT); 63692241e0bSTom Erickson break; 63792241e0bSTom Erickson default: 63892241e0bSTom Erickson cmn_err(CE_PANIC, "unexpected property source: %d", source); 639fa9e4066Sahrens } 640fa9e4066Sahrens 64192241e0bSTom Erickson strfree(inheritstr); 64292241e0bSTom Erickson strfree(recvdstr); 64392241e0bSTom Erickson 6441d452cf5Sahrens if (isint) { 6453b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval)); 64692241e0bSTom Erickson 64792241e0bSTom Erickson if (ds->ds_phys != NULL && dsl_dataset_is_snapshot(ds)) { 648bb0ade09Sahrens dsl_prop_cb_record_t *cbr; 649bb0ade09Sahrens /* 650bb0ade09Sahrens * It's a snapshot; nothing can inherit this 651bb0ade09Sahrens * property, so just look for callbacks on this 652bb0ade09Sahrens * ds here. 653bb0ade09Sahrens */ 654bb0ade09Sahrens mutex_enter(&ds->ds_dir->dd_lock); 655bb0ade09Sahrens for (cbr = list_head(&ds->ds_dir->dd_prop_cbs); cbr; 656bb0ade09Sahrens cbr = list_next(&ds->ds_dir->dd_prop_cbs, cbr)) { 657bb0ade09Sahrens if (cbr->cbr_ds == ds && 65892241e0bSTom Erickson strcmp(cbr->cbr_propname, propname) == 0) 659bb0ade09Sahrens cbr->cbr_func(cbr->cbr_arg, intval); 660bb0ade09Sahrens } 661bb0ade09Sahrens mutex_exit(&ds->ds_dir->dd_lock); 662bb0ade09Sahrens } else { 663bb0ade09Sahrens dsl_prop_changed_notify(ds->ds_dir->dd_pool, 66492241e0bSTom Erickson ds->ds_dir->dd_object, propname, intval, TRUE); 665bb0ade09Sahrens } 66692241e0bSTom Erickson 667ecd6cf80Smarks (void) snprintf(valbuf, sizeof (valbuf), 668ecd6cf80Smarks "%lld", (longlong_t)intval); 669ecd6cf80Smarks valstr = valbuf; 670ecd6cf80Smarks } else { 67192241e0bSTom Erickson if (source == ZPROP_SRC_LOCAL) { 6723b2aab18SMatthew Ahrens valstr = value; 67392241e0bSTom Erickson } else { 67492241e0bSTom Erickson tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP); 67592241e0bSTom Erickson if (dsl_prop_get_ds(ds, propname, 1, 67692241e0bSTom Erickson ZAP_MAXVALUELEN, tbuf, NULL) == 0) 67792241e0bSTom Erickson valstr = tbuf; 67892241e0bSTom Erickson } 679ecd6cf80Smarks } 68092241e0bSTom Erickson 6814445fffbSMatthew Ahrens spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE || 6824445fffbSMatthew Ahrens source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx, 6834445fffbSMatthew Ahrens "%s=%s", propname, (valstr == NULL ? "" : valstr)); 68492241e0bSTom Erickson 68592241e0bSTom Erickson if (tbuf != NULL) 68692241e0bSTom Erickson kmem_free(tbuf, ZAP_MAXVALUELEN); 687fa9e4066Sahrens } 688fa9e4066Sahrens 6893b2aab18SMatthew Ahrens int 6903b2aab18SMatthew Ahrens dsl_prop_set_int(const char *dsname, const char *propname, 6913b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value) 6925c0b6a79SRich Morris { 6933b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc(); 6943b2aab18SMatthew Ahrens int error; 69592241e0bSTom Erickson 6963b2aab18SMatthew Ahrens fnvlist_add_uint64(nvl, propname, value); 6973b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl); 6983b2aab18SMatthew Ahrens fnvlist_free(nvl); 6993b2aab18SMatthew Ahrens return (error); 7005c0b6a79SRich Morris } 7015c0b6a79SRich Morris 702a2eea2e1Sahrens int 7033b2aab18SMatthew Ahrens dsl_prop_set_string(const char *dsname, const char *propname, 7043b2aab18SMatthew Ahrens zprop_source_t source, const char *value) 705fa9e4066Sahrens { 7063b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc(); 7073b2aab18SMatthew Ahrens int error; 708bb0ade09Sahrens 7093b2aab18SMatthew Ahrens fnvlist_add_string(nvl, propname, value); 7103b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl); 7113b2aab18SMatthew Ahrens fnvlist_free(nvl); 7123b2aab18SMatthew Ahrens return (error); 7133b2aab18SMatthew Ahrens } 71492241e0bSTom Erickson 7153b2aab18SMatthew Ahrens int 7163b2aab18SMatthew Ahrens dsl_prop_inherit(const char *dsname, const char *propname, 7173b2aab18SMatthew Ahrens zprop_source_t source) 7183b2aab18SMatthew Ahrens { 7193b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc(); 7203b2aab18SMatthew Ahrens int error; 721bb0ade09Sahrens 7223b2aab18SMatthew Ahrens fnvlist_add_boolean(nvl, propname); 7233b2aab18SMatthew Ahrens error = dsl_props_set(dsname, source, nvl); 7243b2aab18SMatthew Ahrens fnvlist_free(nvl); 7253b2aab18SMatthew Ahrens return (error); 726fa9e4066Sahrens } 7277f7322feSeschrock 7283b2aab18SMatthew Ahrens typedef struct dsl_props_set_arg { 7293b2aab18SMatthew Ahrens const char *dpsa_dsname; 7303b2aab18SMatthew Ahrens zprop_source_t dpsa_source; 7313b2aab18SMatthew Ahrens nvlist_t *dpsa_props; 7323b2aab18SMatthew Ahrens } dsl_props_set_arg_t; 7333b2aab18SMatthew Ahrens 7343b2aab18SMatthew Ahrens static int 7353b2aab18SMatthew Ahrens dsl_props_set_check(void *arg, dmu_tx_t *tx) 7365c0b6a79SRich Morris { 7373b2aab18SMatthew Ahrens dsl_props_set_arg_t *dpsa = arg; 7383b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 7395c0b6a79SRich Morris dsl_dataset_t *ds; 740478ed9adSEric Taylor uint64_t version; 7415c0b6a79SRich Morris nvpair_t *elem = NULL; 7425c0b6a79SRich Morris int err; 7435c0b6a79SRich Morris 7443b2aab18SMatthew Ahrens err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds); 7453b2aab18SMatthew Ahrens if (err != 0) 746478ed9adSEric Taylor return (err); 7473b2aab18SMatthew Ahrens 748478ed9adSEric Taylor version = spa_version(ds->ds_dir->dd_pool->dp_spa); 7493b2aab18SMatthew Ahrens while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) { 750478ed9adSEric Taylor if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 751478ed9adSEric Taylor dsl_dataset_rele(ds, FTAG); 752*be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 753478ed9adSEric Taylor } 7545c0b6a79SRich Morris if (nvpair_type(elem) == DATA_TYPE_STRING) { 7553b2aab18SMatthew Ahrens char *valstr = fnvpair_value_string(elem); 756478ed9adSEric Taylor if (strlen(valstr) >= (version < 757478ed9adSEric Taylor SPA_VERSION_STMF_PROP ? 758478ed9adSEric Taylor ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) { 759478ed9adSEric Taylor dsl_dataset_rele(ds, FTAG); 760ccba0801SRich Morris return (E2BIG); 761478ed9adSEric Taylor } 7625c0b6a79SRich Morris } 7635c0b6a79SRich Morris } 7645c0b6a79SRich Morris 7653b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds) && version < SPA_VERSION_SNAP_PROPS) { 7665c0b6a79SRich Morris dsl_dataset_rele(ds, FTAG); 767*be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 7685c0b6a79SRich Morris } 7693b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 7703b2aab18SMatthew Ahrens return (0); 7713b2aab18SMatthew Ahrens } 7725c0b6a79SRich Morris 7733b2aab18SMatthew Ahrens void 7743b2aab18SMatthew Ahrens dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source, 7753b2aab18SMatthew Ahrens nvlist_t *props, dmu_tx_t *tx) 7763b2aab18SMatthew Ahrens { 7773b2aab18SMatthew Ahrens nvpair_t *elem = NULL; 77892241e0bSTom Erickson 7793b2aab18SMatthew Ahrens while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 7803b2aab18SMatthew Ahrens nvpair_t *pair = elem; 7815c0b6a79SRich Morris 7823b2aab18SMatthew Ahrens if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 7833b2aab18SMatthew Ahrens /* 7843b2aab18SMatthew Ahrens * dsl_prop_get_all_impl() returns properties in this 7853b2aab18SMatthew Ahrens * format. 7863b2aab18SMatthew Ahrens */ 7873b2aab18SMatthew Ahrens nvlist_t *attrs = fnvpair_value_nvlist(pair); 7883b2aab18SMatthew Ahrens pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE); 7893b2aab18SMatthew Ahrens } 7903b2aab18SMatthew Ahrens 7913b2aab18SMatthew Ahrens if (nvpair_type(pair) == DATA_TYPE_STRING) { 7923b2aab18SMatthew Ahrens const char *value = fnvpair_value_string(pair); 7933b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair), 7943b2aab18SMatthew Ahrens source, 1, strlen(value) + 1, value, tx); 7953b2aab18SMatthew Ahrens } else if (nvpair_type(pair) == DATA_TYPE_UINT64) { 7963b2aab18SMatthew Ahrens uint64_t intval = fnvpair_value_uint64(pair); 7973b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair), 7983b2aab18SMatthew Ahrens source, sizeof (intval), 1, &intval, tx); 7993b2aab18SMatthew Ahrens } else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) { 8003b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, nvpair_name(pair), 8013b2aab18SMatthew Ahrens source, 0, 0, NULL, tx); 8023b2aab18SMatthew Ahrens } else { 8033b2aab18SMatthew Ahrens panic("invalid nvpair type"); 8043b2aab18SMatthew Ahrens } 8053b2aab18SMatthew Ahrens } 8063b2aab18SMatthew Ahrens } 8073b2aab18SMatthew Ahrens 8083b2aab18SMatthew Ahrens static void 8093b2aab18SMatthew Ahrens dsl_props_set_sync(void *arg, dmu_tx_t *tx) 8103b2aab18SMatthew Ahrens { 8113b2aab18SMatthew Ahrens dsl_props_set_arg_t *dpsa = arg; 8123b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 8133b2aab18SMatthew Ahrens dsl_dataset_t *ds; 8143b2aab18SMatthew Ahrens 8153b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds)); 8163b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx); 8175c0b6a79SRich Morris dsl_dataset_rele(ds, FTAG); 8183b2aab18SMatthew Ahrens } 8193b2aab18SMatthew Ahrens 8203b2aab18SMatthew Ahrens /* 8213b2aab18SMatthew Ahrens * All-or-nothing; if any prop can't be set, nothing will be modified. 8223b2aab18SMatthew Ahrens */ 8233b2aab18SMatthew Ahrens int 8243b2aab18SMatthew Ahrens dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props) 8253b2aab18SMatthew Ahrens { 8263b2aab18SMatthew Ahrens dsl_props_set_arg_t dpsa; 8273b2aab18SMatthew Ahrens int nblks = 0; 8283b2aab18SMatthew Ahrens 8293b2aab18SMatthew Ahrens dpsa.dpsa_dsname = dsname; 8303b2aab18SMatthew Ahrens dpsa.dpsa_source = source; 8313b2aab18SMatthew Ahrens dpsa.dpsa_props = props; 8323b2aab18SMatthew Ahrens 8333b2aab18SMatthew Ahrens /* 8343b2aab18SMatthew Ahrens * If the source includes NONE, then we will only be removing entries 8353b2aab18SMatthew Ahrens * from the ZAP object. In that case don't check for ENOSPC. 8363b2aab18SMatthew Ahrens */ 8373b2aab18SMatthew Ahrens if ((source & ZPROP_SRC_NONE) == 0) 8383b2aab18SMatthew Ahrens nblks = 2 * fnvlist_num_pairs(props); 8393b2aab18SMatthew Ahrens 8403b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync, 8413b2aab18SMatthew Ahrens &dpsa, nblks)); 8425c0b6a79SRich Morris } 8435c0b6a79SRich Morris 84492241e0bSTom Erickson typedef enum dsl_prop_getflags { 84592241e0bSTom Erickson DSL_PROP_GET_INHERITING = 0x1, /* searching parent of target ds */ 84692241e0bSTom Erickson DSL_PROP_GET_SNAPSHOT = 0x2, /* snapshot dataset */ 84792241e0bSTom Erickson DSL_PROP_GET_LOCAL = 0x4, /* local properties */ 84892241e0bSTom Erickson DSL_PROP_GET_RECEIVED = 0x8 /* received properties */ 84992241e0bSTom Erickson } dsl_prop_getflags_t; 85092241e0bSTom Erickson 85192241e0bSTom Erickson static int 85292241e0bSTom Erickson dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj, 85392241e0bSTom Erickson const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv) 85492241e0bSTom Erickson { 85592241e0bSTom Erickson zap_cursor_t zc; 85692241e0bSTom Erickson zap_attribute_t za; 85792241e0bSTom Erickson int err = 0; 85892241e0bSTom Erickson 85992241e0bSTom Erickson for (zap_cursor_init(&zc, mos, propobj); 86092241e0bSTom Erickson (err = zap_cursor_retrieve(&zc, &za)) == 0; 86192241e0bSTom Erickson zap_cursor_advance(&zc)) { 86292241e0bSTom Erickson nvlist_t *propval; 86392241e0bSTom Erickson zfs_prop_t prop; 86492241e0bSTom Erickson char buf[ZAP_MAXNAMELEN]; 86592241e0bSTom Erickson char *valstr; 86692241e0bSTom Erickson const char *suffix; 86792241e0bSTom Erickson const char *propname; 86892241e0bSTom Erickson const char *source; 86992241e0bSTom Erickson 87092241e0bSTom Erickson suffix = strchr(za.za_name, '$'); 87192241e0bSTom Erickson 87292241e0bSTom Erickson if (suffix == NULL) { 87392241e0bSTom Erickson /* 87492241e0bSTom Erickson * Skip local properties if we only want received 87592241e0bSTom Erickson * properties. 87692241e0bSTom Erickson */ 87792241e0bSTom Erickson if (flags & DSL_PROP_GET_RECEIVED) 87892241e0bSTom Erickson continue; 87992241e0bSTom Erickson 88092241e0bSTom Erickson propname = za.za_name; 88192241e0bSTom Erickson source = setpoint; 88292241e0bSTom Erickson } else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) { 88392241e0bSTom Erickson /* Skip explicitly inherited entries. */ 88492241e0bSTom Erickson continue; 88592241e0bSTom Erickson } else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) { 88692241e0bSTom Erickson if (flags & DSL_PROP_GET_LOCAL) 88792241e0bSTom Erickson continue; 88892241e0bSTom Erickson 88992241e0bSTom Erickson (void) strncpy(buf, za.za_name, (suffix - za.za_name)); 89092241e0bSTom Erickson buf[suffix - za.za_name] = '\0'; 89192241e0bSTom Erickson propname = buf; 89292241e0bSTom Erickson 89392241e0bSTom Erickson if (!(flags & DSL_PROP_GET_RECEIVED)) { 89492241e0bSTom Erickson /* Skip if locally overridden. */ 89592241e0bSTom Erickson err = zap_contains(mos, propobj, propname); 89692241e0bSTom Erickson if (err == 0) 89792241e0bSTom Erickson continue; 89892241e0bSTom Erickson if (err != ENOENT) 89992241e0bSTom Erickson break; 90092241e0bSTom Erickson 90192241e0bSTom Erickson /* Skip if explicitly inherited. */ 90292241e0bSTom Erickson valstr = kmem_asprintf("%s%s", propname, 90392241e0bSTom Erickson ZPROP_INHERIT_SUFFIX); 90492241e0bSTom Erickson err = zap_contains(mos, propobj, valstr); 90592241e0bSTom Erickson strfree(valstr); 90692241e0bSTom Erickson if (err == 0) 90792241e0bSTom Erickson continue; 90892241e0bSTom Erickson if (err != ENOENT) 90992241e0bSTom Erickson break; 91092241e0bSTom Erickson } 91192241e0bSTom Erickson 91292241e0bSTom Erickson source = ((flags & DSL_PROP_GET_INHERITING) ? 91392241e0bSTom Erickson setpoint : ZPROP_SOURCE_VAL_RECVD); 91492241e0bSTom Erickson } else { 91592241e0bSTom Erickson /* 91692241e0bSTom Erickson * For backward compatibility, skip suffixes we don't 91792241e0bSTom Erickson * recognize. 91892241e0bSTom Erickson */ 91992241e0bSTom Erickson continue; 92092241e0bSTom Erickson } 92192241e0bSTom Erickson 92292241e0bSTom Erickson prop = zfs_name_to_prop(propname); 92392241e0bSTom Erickson 92492241e0bSTom Erickson /* Skip non-inheritable properties. */ 92592241e0bSTom Erickson if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL && 92692241e0bSTom Erickson !zfs_prop_inheritable(prop)) 92792241e0bSTom Erickson continue; 92892241e0bSTom Erickson 92992241e0bSTom Erickson /* Skip properties not valid for this type. */ 93092241e0bSTom Erickson if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL && 93192241e0bSTom Erickson !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT)) 93292241e0bSTom Erickson continue; 93392241e0bSTom Erickson 93492241e0bSTom Erickson /* Skip properties already defined. */ 93592241e0bSTom Erickson if (nvlist_exists(nv, propname)) 93692241e0bSTom Erickson continue; 93792241e0bSTom Erickson 93892241e0bSTom Erickson VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 93992241e0bSTom Erickson if (za.za_integer_length == 1) { 94092241e0bSTom Erickson /* 94192241e0bSTom Erickson * String property 94292241e0bSTom Erickson */ 94392241e0bSTom Erickson char *tmp = kmem_alloc(za.za_num_integers, 94492241e0bSTom Erickson KM_SLEEP); 94592241e0bSTom Erickson err = zap_lookup(mos, propobj, 94692241e0bSTom Erickson za.za_name, 1, za.za_num_integers, tmp); 94792241e0bSTom Erickson if (err != 0) { 94892241e0bSTom Erickson kmem_free(tmp, za.za_num_integers); 94992241e0bSTom Erickson break; 95092241e0bSTom Erickson } 95192241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_VALUE, 95292241e0bSTom Erickson tmp) == 0); 95392241e0bSTom Erickson kmem_free(tmp, za.za_num_integers); 95492241e0bSTom Erickson } else { 95592241e0bSTom Erickson /* 95692241e0bSTom Erickson * Integer property 95792241e0bSTom Erickson */ 95892241e0bSTom Erickson ASSERT(za.za_integer_length == 8); 95992241e0bSTom Erickson (void) nvlist_add_uint64(propval, ZPROP_VALUE, 96092241e0bSTom Erickson za.za_first_integer); 96192241e0bSTom Erickson } 96292241e0bSTom Erickson 96392241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0); 96492241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 96592241e0bSTom Erickson nvlist_free(propval); 96692241e0bSTom Erickson } 96792241e0bSTom Erickson zap_cursor_fini(&zc); 96892241e0bSTom Erickson if (err == ENOENT) 96992241e0bSTom Erickson err = 0; 97092241e0bSTom Erickson return (err); 97192241e0bSTom Erickson } 97292241e0bSTom Erickson 9737f7322feSeschrock /* 9747f7322feSeschrock * Iterate over all properties for this dataset and return them in an nvlist. 9757f7322feSeschrock */ 97692241e0bSTom Erickson static int 97792241e0bSTom Erickson dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp, 97892241e0bSTom Erickson dsl_prop_getflags_t flags) 9797f7322feSeschrock { 98099653d4eSeschrock dsl_dir_t *dd = ds->ds_dir; 981bb0ade09Sahrens dsl_pool_t *dp = dd->dd_pool; 982bb0ade09Sahrens objset_t *mos = dp->dp_meta_objset; 98392241e0bSTom Erickson int err = 0; 98492241e0bSTom Erickson char setpoint[MAXNAMELEN]; 9857f7322feSeschrock 9867f7322feSeschrock VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 9877f7322feSeschrock 98892241e0bSTom Erickson if (dsl_dataset_is_snapshot(ds)) 98992241e0bSTom Erickson flags |= DSL_PROP_GET_SNAPSHOT; 9907f7322feSeschrock 9913b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 99292241e0bSTom Erickson 99392241e0bSTom Erickson if (ds->ds_phys->ds_props_obj != 0) { 99492241e0bSTom Erickson ASSERT(flags & DSL_PROP_GET_SNAPSHOT); 99592241e0bSTom Erickson dsl_dataset_name(ds, setpoint); 99692241e0bSTom Erickson err = dsl_prop_get_all_impl(mos, ds->ds_phys->ds_props_obj, 99792241e0bSTom Erickson setpoint, flags, *nvp); 99892241e0bSTom Erickson if (err) 99992241e0bSTom Erickson goto out; 100092241e0bSTom Erickson } 100192241e0bSTom Erickson 100292241e0bSTom Erickson for (; dd != NULL; dd = dd->dd_parent) { 100392241e0bSTom Erickson if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) { 100492241e0bSTom Erickson if (flags & (DSL_PROP_GET_LOCAL | 100592241e0bSTom Erickson DSL_PROP_GET_RECEIVED)) 100692241e0bSTom Erickson break; 100792241e0bSTom Erickson flags |= DSL_PROP_GET_INHERITING; 1008bb0ade09Sahrens } 100992241e0bSTom Erickson dsl_dir_name(dd, setpoint); 101092241e0bSTom Erickson err = dsl_prop_get_all_impl(mos, dd->dd_phys->dd_props_zapobj, 101192241e0bSTom Erickson setpoint, flags, *nvp); 101292241e0bSTom Erickson if (err) 101392241e0bSTom Erickson break; 101492241e0bSTom Erickson } 101592241e0bSTom Erickson out: 101692241e0bSTom Erickson return (err); 101792241e0bSTom Erickson } 1018a2eea2e1Sahrens 101992241e0bSTom Erickson boolean_t 10203b2aab18SMatthew Ahrens dsl_prop_get_hasrecvd(const char *dsname) 102192241e0bSTom Erickson { 102292241e0bSTom Erickson uint64_t dummy; 1023bb0ade09Sahrens 10243b2aab18SMatthew Ahrens return (0 == 10253b2aab18SMatthew Ahrens dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL)); 102692241e0bSTom Erickson } 1027e9dbad6fSeschrock 10283b2aab18SMatthew Ahrens static int 10293b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source) 103092241e0bSTom Erickson { 10313b2aab18SMatthew Ahrens uint64_t version; 10323b2aab18SMatthew Ahrens spa_t *spa; 10333b2aab18SMatthew Ahrens int error = 0; 10347f7322feSeschrock 10353b2aab18SMatthew Ahrens VERIFY0(spa_open(dsname, &spa, FTAG)); 10363b2aab18SMatthew Ahrens version = spa_version(spa); 10373b2aab18SMatthew Ahrens spa_close(spa, FTAG); 10387f7322feSeschrock 10393b2aab18SMatthew Ahrens if (version >= SPA_VERSION_RECVD_PROPS) 10403b2aab18SMatthew Ahrens error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0); 10413b2aab18SMatthew Ahrens return (error); 104292241e0bSTom Erickson } 10437f7322feSeschrock 104492241e0bSTom Erickson /* 104592241e0bSTom Erickson * Call after successfully receiving properties to ensure that only the first 104692241e0bSTom Erickson * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties. 104792241e0bSTom Erickson */ 10483b2aab18SMatthew Ahrens int 10493b2aab18SMatthew Ahrens dsl_prop_set_hasrecvd(const char *dsname) 105092241e0bSTom Erickson { 10513b2aab18SMatthew Ahrens int error = 0; 10523b2aab18SMatthew Ahrens if (!dsl_prop_get_hasrecvd(dsname)) 10533b2aab18SMatthew Ahrens error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL); 10543b2aab18SMatthew Ahrens return (error); 105592241e0bSTom Erickson } 10567f7322feSeschrock 105792241e0bSTom Erickson void 10583b2aab18SMatthew Ahrens dsl_prop_unset_hasrecvd(const char *dsname) 105992241e0bSTom Erickson { 10603b2aab18SMatthew Ahrens VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE)); 106192241e0bSTom Erickson } 106292241e0bSTom Erickson 106392241e0bSTom Erickson int 106492241e0bSTom Erickson dsl_prop_get_all(objset_t *os, nvlist_t **nvp) 106592241e0bSTom Erickson { 106692241e0bSTom Erickson return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0)); 106792241e0bSTom Erickson } 106892241e0bSTom Erickson 106992241e0bSTom Erickson int 10703b2aab18SMatthew Ahrens dsl_prop_get_received(const char *dsname, nvlist_t **nvp) 107192241e0bSTom Erickson { 10723b2aab18SMatthew Ahrens objset_t *os; 10733b2aab18SMatthew Ahrens int error; 10743b2aab18SMatthew Ahrens 107592241e0bSTom Erickson /* 107692241e0bSTom Erickson * Received properties are not distinguishable from local properties 107792241e0bSTom Erickson * until the dataset has received properties on or after 107892241e0bSTom Erickson * SPA_VERSION_RECVD_PROPS. 107992241e0bSTom Erickson */ 10803b2aab18SMatthew Ahrens dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ? 108192241e0bSTom Erickson DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL); 10823b2aab18SMatthew Ahrens 10833b2aab18SMatthew Ahrens error = dmu_objset_hold(dsname, FTAG, &os); 10843b2aab18SMatthew Ahrens if (error != 0) 10853b2aab18SMatthew Ahrens return (error); 10863b2aab18SMatthew Ahrens error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags); 10873b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 10883b2aab18SMatthew Ahrens return (error); 10897f7322feSeschrock } 1090a2eea2e1Sahrens 1091a2eea2e1Sahrens void 1092a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value) 1093a2eea2e1Sahrens { 1094a2eea2e1Sahrens nvlist_t *propval; 109592241e0bSTom Erickson const char *propname = zfs_prop_to_name(prop); 109692241e0bSTom Erickson uint64_t default_value; 109792241e0bSTom Erickson 109892241e0bSTom Erickson if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) { 109992241e0bSTom Erickson VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0); 110092241e0bSTom Erickson return; 110192241e0bSTom Erickson } 1102a2eea2e1Sahrens 1103a2eea2e1Sahrens VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1104990b4856Slling VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0); 110592241e0bSTom Erickson /* Indicate the default source if we can. */ 110692241e0bSTom Erickson if (dodefault(propname, 8, 1, &default_value) == 0 && 110792241e0bSTom Erickson value == default_value) { 110892241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0); 110992241e0bSTom Erickson } 111092241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 1111a2eea2e1Sahrens nvlist_free(propval); 1112a2eea2e1Sahrens } 1113a2eea2e1Sahrens 1114a2eea2e1Sahrens void 1115a2eea2e1Sahrens dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value) 1116a2eea2e1Sahrens { 1117a2eea2e1Sahrens nvlist_t *propval; 111892241e0bSTom Erickson const char *propname = zfs_prop_to_name(prop); 111992241e0bSTom Erickson 112092241e0bSTom Erickson if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) { 112192241e0bSTom Erickson VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0); 112292241e0bSTom Erickson return; 112392241e0bSTom Erickson } 1124a2eea2e1Sahrens 1125a2eea2e1Sahrens VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1126990b4856Slling VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0); 112792241e0bSTom Erickson VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0); 1128a2eea2e1Sahrens nvlist_free(propval); 1129a2eea2e1Sahrens } 1130