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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <sys/dmu_objset.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_synctask.h>
34 #include <sys/zio_checksum.h> /* for the default checksum value */
36 #include <sys/fs/zfs.h>
41 dodefault(const char *propname, int intsz, int numint, void *buf)
46 * The setonce properties are read-only, BUT they still
47 * have a default value that can be used as the initial
50 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL ||
51 (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
54 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
57 (void) strncpy(buf, zfs_prop_default_string(prop),
60 if (intsz != 8 || numint < 1)
63 *(uint64_t *)buf = zfs_prop_default_numeric(prop);
70 dsl_prop_get_dd(dsl_dir_t *dd, const char *propname,
71 int intsz, int numint, void *buf, char *setpoint)
74 objset_t *mos = dd->dd_pool->dp_meta_objset;
77 ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
82 prop = zfs_name_to_prop(propname);
85 * Note: dd may be NULL, therefore we shouldn't dereference it
88 for (; dd != NULL; dd = dd->dd_parent) {
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 dsl_prop_get_ds(dsl_dataset_t *ds, const char *propname,
112 int intsz, int numint, void *buf, char *setpoint)
114 ASSERT(RW_LOCK_HELD(&ds->ds_dir->dd_pool->dp_config_rwlock));
116 if (ds->ds_phys->ds_props_obj) {
117 int err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
118 ds->ds_phys->ds_props_obj, propname, intsz, numint, buf);
121 dsl_dataset_name(ds, setpoint);
126 return (dsl_prop_get_dd(ds->ds_dir, propname,
127 intsz, numint, buf, setpoint));
131 * Register interest in the named property. We'll call the callback
132 * once to notify it of the current property value, and again each time
133 * the property changes, until this callback is unregistered.
135 * Return 0 on success, errno if the prop is not an integer value.
138 dsl_prop_register(dsl_dataset_t *ds, const char *propname,
139 dsl_prop_changed_cb_t *callback, void *cbarg)
141 dsl_dir_t *dd = ds->ds_dir;
142 dsl_pool_t *dp = dd->dd_pool;
144 dsl_prop_cb_record_t *cbr;
148 need_rwlock = !RW_WRITE_HELD(&dp->dp_config_rwlock);
150 rw_enter(&dp->dp_config_rwlock, RW_READER);
152 err = dsl_prop_get_ds(ds, propname, 8, 1, &value, NULL);
155 rw_exit(&dp->dp_config_rwlock);
159 cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
161 cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
162 (void) strcpy((char *)cbr->cbr_propname, propname);
163 cbr->cbr_func = callback;
164 cbr->cbr_arg = cbarg;
165 mutex_enter(&dd->dd_lock);
166 list_insert_head(&dd->dd_prop_cbs, cbr);
167 mutex_exit(&dd->dd_lock);
169 cbr->cbr_func(cbr->cbr_arg, value);
171 VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
174 rw_exit(&dp->dp_config_rwlock);
175 /* Leave dir open until this callback is unregistered */
180 dsl_prop_get(const char *dsname, const char *propname,
181 int intsz, int numints, void *buf, char *setpoint)
186 err = dsl_dataset_hold(dsname, FTAG, &ds);
190 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
191 err = dsl_prop_get_ds(ds, propname, intsz, numints, buf, setpoint);
192 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
194 dsl_dataset_rele(ds, FTAG);
199 * Get the current property value. It may have changed by the time this
200 * function returns, so it is NOT safe to follow up with
201 * dsl_prop_register() and assume that the value has not changed in
204 * Return 0 on success, ENOENT if ddname is invalid.
207 dsl_prop_get_integer(const char *ddname, const char *propname,
208 uint64_t *valuep, char *setpoint)
210 return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
214 * Unregister this callback. Return 0 on success, ENOENT if ddname is
215 * invalid, ENOMSG if no matching callback registered.
218 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
219 dsl_prop_changed_cb_t *callback, void *cbarg)
221 dsl_dir_t *dd = ds->ds_dir;
222 dsl_prop_cb_record_t *cbr;
224 mutex_enter(&dd->dd_lock);
225 for (cbr = list_head(&dd->dd_prop_cbs);
226 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
227 if (cbr->cbr_ds == ds &&
228 cbr->cbr_func == callback &&
229 cbr->cbr_arg == cbarg &&
230 strcmp(cbr->cbr_propname, propname) == 0)
235 mutex_exit(&dd->dd_lock);
239 list_remove(&dd->dd_prop_cbs, cbr);
240 mutex_exit(&dd->dd_lock);
241 kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
242 kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
244 /* Clean up from dsl_prop_register */
245 dsl_dir_close(dd, cbr);
250 * Return the number of callbacks that are registered for this dataset.
253 dsl_prop_numcb(dsl_dataset_t *ds)
255 dsl_dir_t *dd = ds->ds_dir;
256 dsl_prop_cb_record_t *cbr;
259 mutex_enter(&dd->dd_lock);
260 for (cbr = list_head(&dd->dd_prop_cbs);
261 cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
262 if (cbr->cbr_ds == ds)
265 mutex_exit(&dd->dd_lock);
271 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
272 const char *propname, uint64_t value, int first)
275 dsl_prop_cb_record_t *cbr;
276 objset_t *mos = dp->dp_meta_objset;
282 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
283 err = dsl_dir_open_obj(dp, ddobj, NULL, FTAG, &dd);
289 * If the prop is set here, then this change is not
290 * being inherited here or below; stop the recursion.
292 err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname,
295 dsl_dir_close(dd, FTAG);
298 ASSERT3U(err, ==, ENOENT);
301 mutex_enter(&dd->dd_lock);
302 for (cbr = list_head(&dd->dd_prop_cbs); cbr;
303 cbr = list_next(&dd->dd_prop_cbs, cbr)) {
304 uint64_t propobj = cbr->cbr_ds->ds_phys->ds_props_obj;
306 if (strcmp(cbr->cbr_propname, propname) != 0)
310 * If the property is set on this ds, then it is not
311 * inherited here; don't call the callback.
313 if (propobj && 0 == zap_lookup(mos, propobj, propname,
317 cbr->cbr_func(cbr->cbr_arg, value);
319 mutex_exit(&dd->dd_lock);
321 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
322 for (zap_cursor_init(&zc, mos,
323 dd->dd_phys->dd_child_dir_zapobj);
324 zap_cursor_retrieve(&zc, za) == 0;
325 zap_cursor_advance(&zc)) {
326 dsl_prop_changed_notify(dp, za->za_first_integer,
327 propname, value, FALSE);
329 kmem_free(za, sizeof (zap_attribute_t));
330 zap_cursor_fini(&zc);
331 dsl_dir_close(dd, FTAG);
334 struct prop_set_arg {
343 dsl_prop_set_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
345 dsl_dataset_t *ds = arg1;
346 struct prop_set_arg *psa = arg2;
347 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
348 uint64_t zapobj, intval;
353 isint = (dodefault(psa->name, 8, 1, &intval) == 0);
355 if (dsl_dataset_is_snapshot(ds)) {
356 ASSERT(spa_version(ds->ds_dir->dd_pool->dp_spa) >=
357 SPA_VERSION_SNAP_PROPS);
358 if (ds->ds_phys->ds_props_obj == 0) {
359 dmu_buf_will_dirty(ds->ds_dbuf, tx);
360 ds->ds_phys->ds_props_obj =
362 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
364 zapobj = ds->ds_phys->ds_props_obj;
366 zapobj = ds->ds_dir->dd_phys->dd_props_zapobj;
369 if (psa->numints == 0) {
370 int err = zap_remove(mos, zapobj, psa->name, tx);
371 ASSERT(err == 0 || err == ENOENT);
373 VERIFY(0 == dsl_prop_get_ds(ds,
374 psa->name, 8, 1, &intval, NULL));
377 VERIFY(0 == zap_update(mos, zapobj, psa->name,
378 psa->intsz, psa->numints, psa->buf, tx));
380 intval = *(uint64_t *)psa->buf;
384 if (dsl_dataset_is_snapshot(ds)) {
385 dsl_prop_cb_record_t *cbr;
387 * It's a snapshot; nothing can inherit this
388 * property, so just look for callbacks on this
391 mutex_enter(&ds->ds_dir->dd_lock);
392 for (cbr = list_head(&ds->ds_dir->dd_prop_cbs); cbr;
393 cbr = list_next(&ds->ds_dir->dd_prop_cbs, cbr)) {
394 if (cbr->cbr_ds == ds &&
395 strcmp(cbr->cbr_propname, psa->name) == 0)
396 cbr->cbr_func(cbr->cbr_arg, intval);
398 mutex_exit(&ds->ds_dir->dd_lock);
400 dsl_prop_changed_notify(ds->ds_dir->dd_pool,
401 ds->ds_dir->dd_object, psa->name, intval, TRUE);
405 (void) snprintf(valbuf, sizeof (valbuf),
406 "%lld", (longlong_t)intval);
409 valstr = (char *)psa->buf;
411 spa_history_internal_log((psa->numints == 0) ? LOG_DS_INHERIT :
412 LOG_DS_PROPSET, ds->ds_dir->dd_pool->dp_spa, tx, cr,
413 "%s=%s dataset = %llu", psa->name, valstr, ds->ds_object);
417 dsl_props_set_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
419 dsl_dataset_t *ds = arg1;
420 nvlist_t *nvl = arg2;
421 nvpair_t *elem = NULL;
423 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
424 struct prop_set_arg psa;
426 psa.name = nvpair_name(elem);
428 if (nvpair_type(elem) == DATA_TYPE_STRING) {
429 VERIFY(nvpair_value_string(elem,
430 (char **)&psa.buf) == 0);
432 psa.numints = strlen(psa.buf) + 1;
435 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
436 psa.intsz = sizeof (intval);
440 dsl_prop_set_sync(ds, &psa, cr, tx);
445 dsl_prop_set_uint64_sync(dsl_dir_t *dd, const char *name, uint64_t val,
446 cred_t *cr, dmu_tx_t *tx)
448 objset_t *mos = dd->dd_pool->dp_meta_objset;
449 uint64_t zapobj = dd->dd_phys->dd_props_zapobj;
451 ASSERT(dmu_tx_is_syncing(tx));
453 VERIFY(0 == zap_update(mos, zapobj, name, sizeof (val), 1, &val, tx));
455 dsl_prop_changed_notify(dd->dd_pool, dd->dd_object, name, val, TRUE);
457 spa_history_internal_log(LOG_DS_PROPSET, dd->dd_pool->dp_spa, tx, cr,
458 "%s=%llu dataset = %llu", name, (u_longlong_t)val,
459 dd->dd_phys->dd_head_dataset_obj);
463 dsl_prop_set(const char *dsname, const char *propname,
464 int intsz, int numints, const void *buf)
468 struct prop_set_arg psa;
471 * We must do these checks before we get to the syncfunc, since
474 if (strlen(propname) >= ZAP_MAXNAMELEN)
475 return (ENAMETOOLONG);
476 if (intsz * numints >= ZAP_MAXVALUELEN)
479 err = dsl_dataset_hold(dsname, FTAG, &ds);
483 if (dsl_dataset_is_snapshot(ds) &&
484 spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_SNAP_PROPS) {
485 dsl_dataset_rele(ds, FTAG);
491 psa.numints = numints;
493 err = dsl_sync_task_do(ds->ds_dir->dd_pool,
494 NULL, dsl_prop_set_sync, ds, &psa, 2);
496 dsl_dataset_rele(ds, FTAG);
501 dsl_props_set(const char *dsname, nvlist_t *nvl)
504 nvpair_t *elem = NULL;
508 * Do these checks before the syncfunc, since it can't fail.
510 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
511 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN)
512 return (ENAMETOOLONG);
513 if (nvpair_type(elem) == DATA_TYPE_STRING) {
515 VERIFY(nvpair_value_string(elem, &valstr) == 0);
516 if (strlen(valstr) >= ZAP_MAXVALUELEN)
521 if (err = dsl_dataset_hold(dsname, FTAG, &ds))
524 if (dsl_dataset_is_snapshot(ds) &&
525 spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_SNAP_PROPS) {
526 dsl_dataset_rele(ds, FTAG);
530 err = dsl_sync_task_do(ds->ds_dir->dd_pool,
531 NULL, dsl_props_set_sync, ds, nvl, 2);
533 dsl_dataset_rele(ds, FTAG);
538 * Iterate over all properties for this dataset and return them in an nvlist.
541 dsl_prop_get_all(objset_t *os, nvlist_t **nvp, boolean_t local)
543 dsl_dataset_t *ds = os->os->os_dsl_dataset;
544 dsl_dir_t *dd = ds->ds_dir;
545 boolean_t snapshot = dsl_dataset_is_snapshot(ds);
547 dsl_pool_t *dp = dd->dd_pool;
548 objset_t *mos = dp->dp_meta_objset;
549 uint64_t propobj = ds->ds_phys->ds_props_obj;
551 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
553 if (local && snapshot && !propobj)
556 rw_enter(&dp->dp_config_rwlock, RW_READER);
558 char setpoint[MAXNAMELEN];
564 dsl_dataset_name(ds, setpoint);
567 dsl_dir_name(dd, setpoint);
568 propobj = dd->dd_phys->dd_props_zapobj;
569 dd_next = dd->dd_parent;
572 for (zap_cursor_init(&zc, mos, propobj);
573 (err = zap_cursor_retrieve(&zc, &za)) == 0;
574 zap_cursor_advance(&zc)) {
576 zfs_prop_t prop = zfs_name_to_prop(za.za_name);
578 /* Skip non-inheritable properties. */
579 if (prop != ZPROP_INVAL &&
580 !zfs_prop_inheritable(prop) &&
581 (dd != ds->ds_dir || (snapshot && dd != dd_next)))
584 /* Skip properties not valid for this type. */
585 if (snapshot && prop != ZPROP_INVAL &&
586 !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
589 /* Skip properties already defined */
590 if (nvlist_lookup_nvlist(*nvp, za.za_name,
594 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME,
596 if (za.za_integer_length == 1) {
600 char *tmp = kmem_alloc(za.za_num_integers,
602 err = zap_lookup(mos, propobj,
603 za.za_name, 1, za.za_num_integers, tmp);
605 kmem_free(tmp, za.za_num_integers);
608 VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
610 kmem_free(tmp, za.za_num_integers);
615 ASSERT(za.za_integer_length == 8);
616 (void) nvlist_add_uint64(propval, ZPROP_VALUE,
617 za.za_first_integer);
620 VERIFY(nvlist_add_string(propval, ZPROP_SOURCE,
622 VERIFY(nvlist_add_nvlist(*nvp, za.za_name,
624 nvlist_free(propval);
626 zap_cursor_fini(&zc);
632 * If we are just after the props that have been set
633 * locally, then we are done after the first iteration.
640 rw_exit(&dp->dp_config_rwlock);
646 dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
650 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
651 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
652 VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
653 nvlist_free(propval);
657 dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
661 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
662 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
663 VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
664 nvlist_free(propval);