4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)dsl_prop.c 1.16 08/02/20 SMI"
29 #include <sys/dmu_objset.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dsl_prop.h>
34 #include <sys/dsl_synctask.h>
36 #include <sys/zio_checksum.h> /* for the default checksum value */
38 #include <sys/fs/zfs.h>
43 dodefault(const char *propname, int intsz, int numint, void *buf)
48 * The setonce properties are read-only, BUT they still
49 * have a default value that can be used as the initial
52 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL ||
53 (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
56 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
59 (void) strncpy(buf, zfs_prop_default_string(prop),
62 if (intsz != 8 || numint < 1)
65 *(uint64_t *)buf = zfs_prop_default_numeric(prop);
72 dsl_prop_get_impl(dsl_dir_t *dd, const char *propname,
73 int intsz, int numint, void *buf, char *setpoint)
81 prop = zfs_name_to_prop(propname);
84 * Note: dd may be NULL, therefore we shouldn't dereference it
87 for (; dd != NULL; dd = dd->dd_parent) {
88 objset_t *mos = dd->dd_pool->dp_meta_objset;
89 ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
90 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj,
91 propname, intsz, numint, buf);
94 dsl_dir_name(dd, setpoint);
99 * Break out of this loop for non-inheritable properties.
101 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
105 err = dodefault(propname, intsz, numint, buf);
111 * Register interest in the named property. We'll call the callback
112 * once to notify it of the current property value, and again each time
113 * the property changes, until this callback is unregistered.
115 * Return 0 on success, errno if the prop is not an integer value.
118 dsl_prop_register(dsl_dataset_t *ds, const char *propname,
119 dsl_prop_changed_cb_t *callback, void *cbarg)
121 dsl_dir_t *dd = ds->ds_dir;
123 dsl_prop_cb_record_t *cbr;
127 need_rwlock = !RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock);
129 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
131 err = dsl_prop_get_impl(dd, propname, 8, 1, &value, NULL);
134 rw_exit(&dd->dd_pool->dp_config_rwlock);
138 cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
140 cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
141 (void) strcpy((char *)cbr->cbr_propname, propname);
142 cbr->cbr_func = callback;
143 cbr->cbr_arg = cbarg;
144 mutex_enter(&dd->dd_lock);
145 list_insert_head(&dd->dd_prop_cbs, cbr);
146 mutex_exit(&dd->dd_lock);
148 cbr->cbr_func(cbr->cbr_arg, value);
150 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, dd->dd_object,
153 rw_exit(&dd->dd_pool->dp_config_rwlock);
154 /* Leave dataset open until this callback is unregistered */
159 dsl_prop_get_ds(dsl_dir_t *dd, const char *propname,
160 int intsz, int numints, void *buf, char *setpoint)
164 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
165 err = dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint);
166 rw_exit(&dd->dd_pool->dp_config_rwlock);
172 * Get property when config lock is already held.
174 int dsl_prop_get_ds_locked(dsl_dir_t *dd, const char *propname,
175 int intsz, int numints, void *buf, char *setpoint)
177 ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
178 return (dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint));
182 dsl_prop_get(const char *ddname, const char *propname,
183 int intsz, int numints, void *buf, char *setpoint)
189 err = dsl_dir_open(ddname, FTAG, &dd, &tail);
192 if (tail && tail[0] != '@') {
193 dsl_dir_close(dd, FTAG);
197 err = dsl_prop_get_ds(dd, propname, intsz, numints, buf, setpoint);
199 dsl_dir_close(dd, FTAG);
204 * Get the current property value. It may have changed by the time this
205 * function returns, so it is NOT safe to follow up with
206 * dsl_prop_register() and assume that the value has not changed in
209 * Return 0 on success, ENOENT if ddname is invalid.
212 dsl_prop_get_integer(const char *ddname, const char *propname,
213 uint64_t *valuep, char *setpoint)
215 return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
219 * Unregister this callback. Return 0 on success, ENOENT if ddname is
220 * invalid, ENOMSG if no matching callback registered.
223 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
224 dsl_prop_changed_cb_t *callback, void *cbarg)
226 dsl_dir_t *dd = ds->ds_dir;
227 dsl_prop_cb_record_t *cbr;
229 mutex_enter(&dd->dd_lock);
230 for (cbr = list_head(&dd->dd_prop_cbs);
231 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
232 if (cbr->cbr_ds == ds &&
233 cbr->cbr_func == callback &&
234 cbr->cbr_arg == cbarg &&
235 strcmp(cbr->cbr_propname, propname) == 0)
240 mutex_exit(&dd->dd_lock);
244 list_remove(&dd->dd_prop_cbs, cbr);
245 mutex_exit(&dd->dd_lock);
246 kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
247 kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
249 /* Clean up from dsl_prop_register */
250 dsl_dir_close(dd, cbr);
255 * Return the number of callbacks that are registered for this dataset.
258 dsl_prop_numcb(dsl_dataset_t *ds)
260 dsl_dir_t *dd = ds->ds_dir;
261 dsl_prop_cb_record_t *cbr;
264 mutex_enter(&dd->dd_lock);
265 for (cbr = list_head(&dd->dd_prop_cbs);
266 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
267 if (cbr->cbr_ds == ds)
270 mutex_exit(&dd->dd_lock);
276 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
277 const char *propname, uint64_t value, int first)
280 dsl_prop_cb_record_t *cbr;
281 objset_t *mos = dp->dp_meta_objset;
286 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
287 err = dsl_dir_open_obj(dp, ddobj, NULL, FTAG, &dd);
293 * If the prop is set here, then this change is not
294 * being inherited here or below; stop the recursion.
296 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname,
299 dsl_dir_close(dd, FTAG);
302 ASSERT3U(err, ==, ENOENT);
305 mutex_enter(&dd->dd_lock);
306 for (cbr = list_head(&dd->dd_prop_cbs);
307 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
308 if (strcmp(cbr->cbr_propname, propname) == 0) {
309 cbr->cbr_func(cbr->cbr_arg, value);
312 mutex_exit(&dd->dd_lock);
314 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
315 for (zap_cursor_init(&zc, mos,
316 dd->dd_phys->dd_child_dir_zapobj);
317 zap_cursor_retrieve(&zc, za) == 0;
318 zap_cursor_advance(&zc)) {
319 dsl_prop_changed_notify(dp, za->za_first_integer,
320 propname, value, FALSE);
322 kmem_free(za, sizeof (zap_attribute_t));
323 zap_cursor_fini(&zc);
324 dsl_dir_close(dd, FTAG);
327 struct prop_set_arg {
336 dsl_prop_set_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
338 dsl_dir_t *dd = arg1;
339 struct prop_set_arg *psa = arg2;
340 objset_t *mos = dd->dd_pool->dp_meta_objset;
341 uint64_t zapobj = dd->dd_phys->dd_props_zapobj;
347 isint = (dodefault(psa->name, 8, 1, &intval) == 0);
349 if (psa->numints == 0) {
350 int err = zap_remove(mos, zapobj, psa->name, tx);
351 ASSERT(err == 0 || err == ENOENT);
353 VERIFY(0 == dsl_prop_get_impl(dd->dd_parent,
354 psa->name, 8, 1, &intval, NULL));
357 VERIFY(0 == zap_update(mos, zapobj, psa->name,
358 psa->intsz, psa->numints, psa->buf, tx));
360 intval = *(uint64_t *)psa->buf;
364 dsl_prop_changed_notify(dd->dd_pool,
365 dd->dd_object, psa->name, intval, TRUE);
368 (void) snprintf(valbuf, sizeof (valbuf),
369 "%lld", (longlong_t)intval);
372 valstr = (char *)psa->buf;
374 spa_history_internal_log((psa->numints == 0) ? LOG_DS_INHERIT :
375 LOG_DS_PROPSET, dd->dd_pool->dp_spa, tx, cr,
376 "%s=%s dataset = %llu", psa->name, valstr,
377 dd->dd_phys->dd_head_dataset_obj);
381 dsl_prop_set_uint64_sync(dsl_dir_t *dd, const char *name, uint64_t val,
382 cred_t *cr, dmu_tx_t *tx)
384 objset_t *mos = dd->dd_pool->dp_meta_objset;
385 uint64_t zapobj = dd->dd_phys->dd_props_zapobj;
387 ASSERT(dmu_tx_is_syncing(tx));
389 VERIFY(0 == zap_update(mos, zapobj, name, sizeof (val), 1, &val, tx));
391 dsl_prop_changed_notify(dd->dd_pool, dd->dd_object, name, val, TRUE);
393 spa_history_internal_log(LOG_DS_PROPSET, dd->dd_pool->dp_spa, tx, cr,
394 "%s=%llu dataset = %llu", name, (u_longlong_t)val,
395 dd->dd_phys->dd_head_dataset_obj);
399 dsl_prop_set_dd(dsl_dir_t *dd, const char *propname,
400 int intsz, int numints, const void *buf)
402 struct prop_set_arg psa;
406 psa.numints = numints;
409 return (dsl_sync_task_do(dd->dd_pool,
410 NULL, dsl_prop_set_sync, dd, &psa, 2));
414 dsl_prop_set(const char *ddname, const char *propname,
415 int intsz, int numints, const void *buf)
421 * We must do these checks before we get to the syncfunc, since
424 if (strlen(propname) >= ZAP_MAXNAMELEN)
425 return (ENAMETOOLONG);
426 if (intsz * numints >= ZAP_MAXVALUELEN)
429 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
432 err = dsl_prop_set_dd(dd, propname, intsz, numints, buf);
433 dsl_dir_close(dd, FTAG);
438 * Iterate over all properties for this dataset and return them in an nvlist.
441 dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
443 dsl_dataset_t *ds = os->os->os_dsl_dataset;
444 dsl_dir_t *dd = ds->ds_dir;
450 snapshot = dsl_dataset_is_snapshot(ds);
452 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
455 mos = dp->dp_meta_objset;
457 rw_enter(&dp->dp_config_rwlock, RW_READER);
458 for (; dd != NULL; dd = dd->dd_parent) {
459 char setpoint[MAXNAMELEN];
463 dsl_dir_name(dd, setpoint);
465 for (zap_cursor_init(&zc, mos, dd->dd_phys->dd_props_zapobj);
466 (err = zap_cursor_retrieve(&zc, &za)) == 0;
467 zap_cursor_advance(&zc)) {
471 * Skip non-inheritable properties.
473 if ((prop = zfs_name_to_prop(za.za_name)) !=
474 ZPROP_INVAL && !zfs_prop_inheritable(prop) &&
479 !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
482 if (nvlist_lookup_nvlist(*nvp, za.za_name,
486 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME,
488 if (za.za_integer_length == 1) {
492 char *tmp = kmem_alloc(za.za_num_integers,
494 err = zap_lookup(mos,
495 dd->dd_phys->dd_props_zapobj,
496 za.za_name, 1, za.za_num_integers,
499 kmem_free(tmp, za.za_num_integers);
502 VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
504 kmem_free(tmp, za.za_num_integers);
509 ASSERT(za.za_integer_length == 8);
510 (void) nvlist_add_uint64(propval, ZPROP_VALUE,
511 za.za_first_integer);
514 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE,
516 VERIFY(nvlist_add_nvlist(*nvp, za.za_name,
518 nvlist_free(propval);
520 zap_cursor_fini(&zc);
526 rw_exit(&dp->dp_config_rwlock);
532 dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
536 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
537 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
538 VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
539 nvlist_free(propval);
543 dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
547 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
548 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
549 VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
550 nvlist_free(propval);