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 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% 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)
47 if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL ||
48 zfs_prop_readonly(prop))
51 if (zfs_prop_get_type(prop) == prop_type_string) {
54 (void) strncpy(buf, zfs_prop_default_string(prop), numint);
56 if (intsz != 8 || numint < 1)
59 *(uint64_t *)buf = zfs_prop_default_numeric(prop);
66 dsl_prop_get_impl(dsl_dir_t *dd, const char *propname,
67 int intsz, int numint, void *buf, char *setpoint)
75 prop = zfs_name_to_prop(propname);
78 * Note: dd may be NULL, therefore we shouldn't dereference it
81 for (; dd != NULL; dd = dd->dd_parent) {
82 objset_t *mos = dd->dd_pool->dp_meta_objset;
83 ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
84 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj,
85 propname, intsz, numint, buf);
88 dsl_dir_name(dd, setpoint);
93 * Break out of this loop for non-inheritable properties.
95 if (prop != ZFS_PROP_INVAL &&
96 !zfs_prop_inheritable(prop))
100 err = dodefault(propname, intsz, numint, buf);
106 * Register interest in the named property. We'll call the callback
107 * once to notify it of the current property value, and again each time
108 * the property changes, until this callback is unregistered.
110 * Return 0 on success, errno if the prop is not an integer value.
113 dsl_prop_register(dsl_dataset_t *ds, const char *propname,
114 dsl_prop_changed_cb_t *callback, void *cbarg)
116 dsl_dir_t *dd = ds->ds_dir;
118 dsl_prop_cb_record_t *cbr;
122 need_rwlock = !RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock);
124 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
126 err = dsl_prop_get_impl(dd, propname, 8, 1, &value, NULL);
128 rw_exit(&dd->dd_pool->dp_config_rwlock);
132 cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
134 cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
135 (void) strcpy((char *)cbr->cbr_propname, propname);
136 cbr->cbr_func = callback;
137 cbr->cbr_arg = cbarg;
138 mutex_enter(&dd->dd_lock);
139 list_insert_head(&dd->dd_prop_cbs, cbr);
140 mutex_exit(&dd->dd_lock);
142 cbr->cbr_func(cbr->cbr_arg, value);
144 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, dd->dd_object,
147 rw_exit(&dd->dd_pool->dp_config_rwlock);
148 /* Leave dataset open until this callback is unregistered */
153 dsl_prop_get_ds(dsl_dir_t *dd, const char *propname,
154 int intsz, int numints, void *buf, char *setpoint)
158 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
159 err = dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint);
160 rw_exit(&dd->dd_pool->dp_config_rwlock);
166 dsl_prop_get(const char *ddname, const char *propname,
167 int intsz, int numints, void *buf, char *setpoint)
173 err = dsl_dir_open(ddname, FTAG, &dd, &tail);
176 if (tail && tail[0] != '@') {
177 dsl_dir_close(dd, FTAG);
181 err = dsl_prop_get_ds(dd, propname, intsz, numints, buf, setpoint);
183 dsl_dir_close(dd, FTAG);
188 * Get the current property value. It may have changed by the time this
189 * function returns, so it is NOT safe to follow up with
190 * dsl_prop_register() and assume that the value has not changed in
193 * Return 0 on success, ENOENT if ddname is invalid.
196 dsl_prop_get_integer(const char *ddname, const char *propname,
197 uint64_t *valuep, char *setpoint)
199 return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
203 * Unregister this callback. Return 0 on success, ENOENT if ddname is
204 * invalid, ENOMSG if no matching callback registered.
207 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
208 dsl_prop_changed_cb_t *callback, void *cbarg)
210 dsl_dir_t *dd = ds->ds_dir;
211 dsl_prop_cb_record_t *cbr;
213 mutex_enter(&dd->dd_lock);
214 for (cbr = list_head(&dd->dd_prop_cbs);
215 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
216 if (cbr->cbr_ds == ds &&
217 cbr->cbr_func == callback &&
218 cbr->cbr_arg == cbarg &&
219 strcmp(cbr->cbr_propname, propname) == 0)
224 mutex_exit(&dd->dd_lock);
228 list_remove(&dd->dd_prop_cbs, cbr);
229 mutex_exit(&dd->dd_lock);
230 kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
231 kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
233 /* Clean up from dsl_prop_register */
234 dsl_dir_close(dd, cbr);
239 * Return the number of callbacks that are registered for this dataset.
242 dsl_prop_numcb(dsl_dataset_t *ds)
244 dsl_dir_t *dd = ds->ds_dir;
245 dsl_prop_cb_record_t *cbr;
248 mutex_enter(&dd->dd_lock);
249 for (cbr = list_head(&dd->dd_prop_cbs);
250 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
251 if (cbr->cbr_ds == ds)
254 mutex_exit(&dd->dd_lock);
260 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
261 const char *propname, uint64_t value, int first)
264 dsl_prop_cb_record_t *cbr;
265 objset_t *mos = dp->dp_meta_objset;
270 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
271 err = dsl_dir_open_obj(dp, ddobj, NULL, FTAG, &dd);
277 * If the prop is set here, then this change is not
278 * being inherited here or below; stop the recursion.
280 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname,
283 dsl_dir_close(dd, FTAG);
286 ASSERT3U(err, ==, ENOENT);
289 mutex_enter(&dd->dd_lock);
290 for (cbr = list_head(&dd->dd_prop_cbs);
291 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
292 if (strcmp(cbr->cbr_propname, propname) == 0) {
293 cbr->cbr_func(cbr->cbr_arg, value);
296 mutex_exit(&dd->dd_lock);
298 for (zap_cursor_init(&zc, mos,
299 dd->dd_phys->dd_child_dir_zapobj);
300 zap_cursor_retrieve(&zc, &za) == 0;
301 zap_cursor_advance(&zc)) {
302 /* XXX recursion could blow stack; esp. za! */
303 dsl_prop_changed_notify(dp, za.za_first_integer,
304 propname, value, FALSE);
306 zap_cursor_fini(&zc);
307 dsl_dir_close(dd, FTAG);
310 struct prop_set_arg {
319 dsl_prop_set_sync(void *arg1, void *arg2, dmu_tx_t *tx)
321 dsl_dir_t *dd = arg1;
322 struct prop_set_arg *psa = arg2;
323 objset_t *mos = dd->dd_pool->dp_meta_objset;
324 uint64_t zapobj = dd->dd_phys->dd_props_zapobj;
328 isint = (dodefault(psa->name, 8, 1, &intval) == 0);
330 if (psa->numints == 0) {
331 int err = zap_remove(mos, zapobj, psa->name, tx);
332 ASSERT(err == 0 || err == ENOENT);
334 VERIFY(0 == dsl_prop_get_impl(dd->dd_parent,
335 psa->name, 8, 1, &intval, NULL));
338 VERIFY(0 == zap_update(mos, zapobj, psa->name,
339 psa->intsz, psa->numints, psa->buf, tx));
341 intval = *(uint64_t *)psa->buf;
345 dsl_prop_changed_notify(dd->dd_pool,
346 dd->dd_object, psa->name, intval, TRUE);
351 dsl_prop_set_dd(dsl_dir_t *dd, const char *propname,
352 int intsz, int numints, const void *buf)
354 struct prop_set_arg psa;
358 psa.numints = numints;
361 return (dsl_sync_task_do(dd->dd_pool,
362 NULL, dsl_prop_set_sync, dd, &psa, 2));
366 dsl_prop_set(const char *ddname, const char *propname,
367 int intsz, int numints, const void *buf)
373 * We must do these checks before we get to the syncfunc, since
376 if (strlen(propname) >= ZAP_MAXNAMELEN)
377 return (ENAMETOOLONG);
378 if (intsz * numints >= ZAP_MAXVALUELEN)
381 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
384 err = dsl_prop_set_dd(dd, propname, intsz, numints, buf);
385 dsl_dir_close(dd, FTAG);
390 * Iterate over all properties for this dataset and return them in an nvlist.
393 dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
395 dsl_dataset_t *ds = os->os->os_dsl_dataset;
396 dsl_dir_t *dd = ds->ds_dir;
401 if (dsl_dataset_is_snapshot(ds)) {
402 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
406 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
409 mos = dp->dp_meta_objset;
411 rw_enter(&dp->dp_config_rwlock, RW_READER);
412 for (; dd != NULL; dd = dd->dd_parent) {
413 char setpoint[MAXNAMELEN];
417 dsl_dir_name(dd, setpoint);
419 for (zap_cursor_init(&zc, mos, dd->dd_phys->dd_props_zapobj);
420 (err = zap_cursor_retrieve(&zc, &za)) == 0;
421 zap_cursor_advance(&zc)) {
425 * Skip non-inheritable properties.
427 if ((prop = zfs_name_to_prop(za.za_name)) !=
428 ZFS_PROP_INVAL && !zfs_prop_inheritable(prop) &&
432 if (nvlist_lookup_nvlist(*nvp, za.za_name,
436 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME,
438 if (za.za_integer_length == 1) {
442 char *tmp = kmem_alloc(za.za_num_integers,
444 err = zap_lookup(mos,
445 dd->dd_phys->dd_props_zapobj,
446 za.za_name, 1, za.za_num_integers,
449 kmem_free(tmp, za.za_num_integers);
452 VERIFY(nvlist_add_string(propval,
453 ZFS_PROP_VALUE, tmp) == 0);
454 kmem_free(tmp, za.za_num_integers);
459 ASSERT(za.za_integer_length == 8);
460 (void) nvlist_add_uint64(propval,
461 ZFS_PROP_VALUE, za.za_first_integer);
464 VERIFY(nvlist_add_string(propval,
465 ZFS_PROP_SOURCE, setpoint) == 0);
466 VERIFY(nvlist_add_nvlist(*nvp, za.za_name,
468 nvlist_free(propval);
470 zap_cursor_fini(&zc);
476 rw_exit(&dp->dp_config_rwlock);
482 dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
486 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
487 VERIFY(nvlist_add_uint64(propval, ZFS_PROP_VALUE, value) == 0);
488 VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
489 nvlist_free(propval);
493 dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
497 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
498 VERIFY(nvlist_add_string(propval, ZFS_PROP_VALUE, value) == 0);
499 VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
500 nvlist_free(propval);