1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2015, Joyent, Inc.
25 */
26
27#include <sys/zfs_context.h>
28#include <sys/dmu.h>
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>
35#include <sys/spa.h>
36#include <sys/zap.h>
37#include <sys/fs/zfs.h>
38
39#include "zfs_prop.h"
40
41#define	ZPROP_INHERIT_SUFFIX "$inherit"
42#define	ZPROP_RECVD_SUFFIX "$recvd"
43
44static int
45dodefault(zfs_prop_t prop, int intsz, int numints, void *buf)
46{
47	/*
48	 * The setonce properties are read-only, BUT they still
49	 * have a default value that can be used as the initial
50	 * value.
51	 */
52	if (prop == ZPROP_INVAL ||
53	    (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
54		return (SET_ERROR(ENOENT));
55
56	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
57		if (zfs_prop_default_string(prop) == NULL)
58			return (SET_ERROR(ENOENT));
59		if (intsz != 1)
60			return (SET_ERROR(EOVERFLOW));
61		(void) strncpy(buf, zfs_prop_default_string(prop),
62		    numints);
63	} else {
64		if (intsz != 8 || numints < 1)
65			return (SET_ERROR(EOVERFLOW));
66
67		*(uint64_t *)buf = zfs_prop_default_numeric(prop);
68	}
69
70	return (0);
71}
72
73int
74dsl_prop_get_dd(dsl_dir_t *dd, const char *propname,
75    int intsz, int numints, void *buf, char *setpoint, boolean_t snapshot)
76{
77	int err = ENOENT;
78	dsl_dir_t *target = dd;
79	objset_t *mos = dd->dd_pool->dp_meta_objset;
80	zfs_prop_t prop;
81	boolean_t inheritable;
82	boolean_t inheriting = B_FALSE;
83	char *inheritstr;
84	char *recvdstr;
85
86	ASSERT(dsl_pool_config_held(dd->dd_pool));
87
88	if (setpoint)
89		setpoint[0] = '\0';
90
91	prop = zfs_name_to_prop(propname);
92	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
93	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
94	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
95
96	/*
97	 * Note: dd may become NULL, therefore we shouldn't dereference it
98	 * after this loop.
99	 */
100	for (; dd != NULL; dd = dd->dd_parent) {
101		if (dd != target || snapshot) {
102			if (!inheritable)
103				break;
104			inheriting = B_TRUE;
105		}
106
107		/* Check for a local value. */
108		err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
109		    propname, intsz, numints, buf);
110		if (err != ENOENT) {
111			if (setpoint != NULL && err == 0)
112				dsl_dir_name(dd, setpoint);
113			break;
114		}
115
116		/*
117		 * Skip the check for a received value if there is an explicit
118		 * inheritance entry.
119		 */
120		err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
121		    inheritstr);
122		if (err != 0 && err != ENOENT)
123			break;
124
125		if (err == ENOENT) {
126			/* Check for a received value. */
127			err = zap_lookup(mos, dsl_dir_phys(dd)->dd_props_zapobj,
128			    recvdstr, intsz, numints, buf);
129			if (err != ENOENT) {
130				if (setpoint != NULL && err == 0) {
131					if (inheriting) {
132						dsl_dir_name(dd, setpoint);
133					} else {
134						(void) strcpy(setpoint,
135						    ZPROP_SOURCE_VAL_RECVD);
136					}
137				}
138				break;
139			}
140		}
141
142		/*
143		 * If we found an explicit inheritance entry, err is zero even
144		 * though we haven't yet found the value, so reinitializing err
145		 * at the end of the loop (instead of at the beginning) ensures
146		 * that err has a valid post-loop value.
147		 */
148		err = SET_ERROR(ENOENT);
149	}
150
151	if (err == ENOENT)
152		err = dodefault(prop, intsz, numints, buf);
153
154	strfree(inheritstr);
155	strfree(recvdstr);
156
157	return (err);
158}
159
160int
161dsl_prop_get_ds(dsl_dataset_t *ds, const char *propname,
162    int intsz, int numints, void *buf, char *setpoint)
163{
164	zfs_prop_t prop = zfs_name_to_prop(propname);
165	boolean_t inheritable;
166	uint64_t zapobj;
167
168	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
169	inheritable = (prop == ZPROP_INVAL || zfs_prop_inheritable(prop));
170	zapobj = dsl_dataset_phys(ds)->ds_props_obj;
171
172	if (zapobj != 0) {
173		objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
174		int err;
175
176		ASSERT(ds->ds_is_snapshot);
177
178		/* Check for a local value. */
179		err = zap_lookup(mos, zapobj, propname, intsz, numints, buf);
180		if (err != ENOENT) {
181			if (setpoint != NULL && err == 0)
182				dsl_dataset_name(ds, setpoint);
183			return (err);
184		}
185
186		/*
187		 * Skip the check for a received value if there is an explicit
188		 * inheritance entry.
189		 */
190		if (inheritable) {
191			char *inheritstr = kmem_asprintf("%s%s", propname,
192			    ZPROP_INHERIT_SUFFIX);
193			err = zap_contains(mos, zapobj, inheritstr);
194			strfree(inheritstr);
195			if (err != 0 && err != ENOENT)
196				return (err);
197		}
198
199		if (err == ENOENT) {
200			/* Check for a received value. */
201			char *recvdstr = kmem_asprintf("%s%s", propname,
202			    ZPROP_RECVD_SUFFIX);
203			err = zap_lookup(mos, zapobj, recvdstr,
204			    intsz, numints, buf);
205			strfree(recvdstr);
206			if (err != ENOENT) {
207				if (setpoint != NULL && err == 0)
208					(void) strcpy(setpoint,
209					    ZPROP_SOURCE_VAL_RECVD);
210				return (err);
211			}
212		}
213	}
214
215	return (dsl_prop_get_dd(ds->ds_dir, propname,
216	    intsz, numints, buf, setpoint, ds->ds_is_snapshot));
217}
218
219static dsl_prop_record_t *
220dsl_prop_record_find(dsl_dir_t *dd, const char *propname)
221{
222	dsl_prop_record_t *pr = NULL;
223
224	ASSERT(MUTEX_HELD(&dd->dd_lock));
225
226	for (pr = list_head(&dd->dd_props);
227	    pr != NULL; pr = list_next(&dd->dd_props, pr)) {
228		if (strcmp(pr->pr_propname, propname) == 0)
229			break;
230	}
231
232	return (pr);
233}
234
235static dsl_prop_record_t *
236dsl_prop_record_create(dsl_dir_t *dd, const char *propname)
237{
238	dsl_prop_record_t *pr;
239
240	ASSERT(MUTEX_HELD(&dd->dd_lock));
241
242	pr = kmem_alloc(sizeof (dsl_prop_record_t), KM_SLEEP);
243	pr->pr_propname = spa_strdup(propname);
244	list_create(&pr->pr_cbs, sizeof (dsl_prop_cb_record_t),
245	    offsetof(dsl_prop_cb_record_t, cbr_pr_node));
246	list_insert_head(&dd->dd_props, pr);
247
248	return (pr);
249}
250
251void
252dsl_prop_init(dsl_dir_t *dd)
253{
254	list_create(&dd->dd_props, sizeof (dsl_prop_record_t),
255	    offsetof(dsl_prop_record_t, pr_node));
256}
257
258void
259dsl_prop_fini(dsl_dir_t *dd)
260{
261	dsl_prop_record_t *pr;
262
263	while ((pr = list_remove_head(&dd->dd_props)) != NULL) {
264		list_destroy(&pr->pr_cbs);
265		strfree((char *)pr->pr_propname);
266		kmem_free(pr, sizeof (dsl_prop_record_t));
267	}
268	list_destroy(&dd->dd_props);
269}
270
271/*
272 * Register interest in the named property.  We'll call the callback
273 * once to notify it of the current property value, and again each time
274 * the property changes, until this callback is unregistered.
275 *
276 * Return 0 on success, errno if the prop is not an integer value.
277 */
278int
279dsl_prop_register(dsl_dataset_t *ds, const char *propname,
280    dsl_prop_changed_cb_t *callback, void *cbarg)
281{
282	dsl_dir_t *dd = ds->ds_dir;
283	dsl_pool_t *dp = dd->dd_pool;
284	uint64_t value;
285	dsl_prop_record_t *pr;
286	dsl_prop_cb_record_t *cbr;
287	int err;
288
289	ASSERT(dsl_pool_config_held(dp));
290
291	err = dsl_prop_get_int_ds(ds, propname, &value);
292	if (err != 0)
293		return (err);
294
295	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
296	cbr->cbr_ds = ds;
297	cbr->cbr_func = callback;
298	cbr->cbr_arg = cbarg;
299
300	mutex_enter(&dd->dd_lock);
301	pr = dsl_prop_record_find(dd, propname);
302	if (pr == NULL)
303		pr = dsl_prop_record_create(dd, propname);
304	cbr->cbr_pr = pr;
305	list_insert_head(&pr->pr_cbs, cbr);
306	list_insert_head(&ds->ds_prop_cbs, cbr);
307	mutex_exit(&dd->dd_lock);
308
309	cbr->cbr_func(cbr->cbr_arg, value);
310	return (0);
311}
312
313int
314dsl_prop_get(const char *dsname, const char *propname,
315    int intsz, int numints, void *buf, char *setpoint)
316{
317	objset_t *os;
318	int error;
319
320	error = dmu_objset_hold(dsname, FTAG, &os);
321	if (error != 0)
322		return (error);
323
324	error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
325	    intsz, numints, buf, setpoint);
326
327	dmu_objset_rele(os, FTAG);
328	return (error);
329}
330
331/*
332 * Get the current property value.  It may have changed by the time this
333 * function returns, so it is NOT safe to follow up with
334 * dsl_prop_register() and assume that the value has not changed in
335 * between.
336 *
337 * Return 0 on success, ENOENT if ddname is invalid.
338 */
339int
340dsl_prop_get_integer(const char *ddname, const char *propname,
341    uint64_t *valuep, char *setpoint)
342{
343	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
344}
345
346int
347dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname,
348    uint64_t *valuep)
349{
350	return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL));
351}
352
353/*
354 * Predict the effective value of the given special property if it were set with
355 * the given value and source. This is not a general purpose function. It exists
356 * only to handle the special requirements of the quota and reservation
357 * properties. The fact that these properties are non-inheritable greatly
358 * simplifies the prediction logic.
359 *
360 * Returns 0 on success, a positive error code on failure, or -1 if called with
361 * a property not handled by this function.
362 */
363int
364dsl_prop_predict(dsl_dir_t *dd, const char *propname,
365    zprop_source_t source, uint64_t value, uint64_t *newvalp)
366{
367	zfs_prop_t prop = zfs_name_to_prop(propname);
368	objset_t *mos;
369	uint64_t zapobj;
370	uint64_t version;
371	char *recvdstr;
372	int err = 0;
373
374	switch (prop) {
375	case ZFS_PROP_QUOTA:
376	case ZFS_PROP_RESERVATION:
377	case ZFS_PROP_REFQUOTA:
378	case ZFS_PROP_REFRESERVATION:
379		break;
380	default:
381		return (-1);
382	}
383
384	mos = dd->dd_pool->dp_meta_objset;
385	zapobj = dsl_dir_phys(dd)->dd_props_zapobj;
386	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
387
388	version = spa_version(dd->dd_pool->dp_spa);
389	if (version < SPA_VERSION_RECVD_PROPS) {
390		if (source & ZPROP_SRC_NONE)
391			source = ZPROP_SRC_NONE;
392		else if (source & ZPROP_SRC_RECEIVED)
393			source = ZPROP_SRC_LOCAL;
394	}
395
396	switch (source) {
397	case ZPROP_SRC_NONE:
398		/* Revert to the received value, if any. */
399		err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp);
400		if (err == ENOENT)
401			*newvalp = 0;
402		break;
403	case ZPROP_SRC_LOCAL:
404		*newvalp = value;
405		break;
406	case ZPROP_SRC_RECEIVED:
407		/*
408		 * If there's no local setting, then the new received value will
409		 * be the effective value.
410		 */
411		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
412		if (err == ENOENT)
413			*newvalp = value;
414		break;
415	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
416		/*
417		 * We're clearing the received value, so the local setting (if
418		 * it exists) remains the effective value.
419		 */
420		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
421		if (err == ENOENT)
422			*newvalp = 0;
423		break;
424	default:
425		panic("unexpected property source: %d", source);
426	}
427
428	strfree(recvdstr);
429
430	if (err == ENOENT)
431		return (0);
432
433	return (err);
434}
435
436/*
437 * Unregister all callbacks that are registered with the
438 * given callback argument.
439 */
440void
441dsl_prop_unregister_all(dsl_dataset_t *ds, void *cbarg)
442{
443	dsl_prop_cb_record_t *cbr, *next_cbr;
444
445	dsl_dir_t *dd = ds->ds_dir;
446
447	mutex_enter(&dd->dd_lock);
448	next_cbr = list_head(&ds->ds_prop_cbs);
449	while (next_cbr != NULL) {
450		cbr = next_cbr;
451		next_cbr = list_next(&ds->ds_prop_cbs, cbr);
452		if (cbr->cbr_arg == cbarg) {
453			list_remove(&ds->ds_prop_cbs, cbr);
454			list_remove(&cbr->cbr_pr->pr_cbs, cbr);
455			kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
456		}
457	}
458	mutex_exit(&dd->dd_lock);
459}
460
461boolean_t
462dsl_prop_hascb(dsl_dataset_t *ds)
463{
464	return (!list_is_empty(&ds->ds_prop_cbs));
465}
466
467/* ARGSUSED */
468static int
469dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
470{
471	dsl_dir_t *dd = ds->ds_dir;
472	dsl_prop_record_t *pr;
473	dsl_prop_cb_record_t *cbr;
474
475	mutex_enter(&dd->dd_lock);
476	for (pr = list_head(&dd->dd_props);
477	    pr; pr = list_next(&dd->dd_props, pr)) {
478		for (cbr = list_head(&pr->pr_cbs); cbr;
479		    cbr = list_next(&pr->pr_cbs, cbr)) {
480			uint64_t value;
481
482			/*
483			 * Callback entries do not have holds on their
484			 * datasets so that datasets with registered
485			 * callbacks are still eligible for eviction.
486			 * Unlike operations to update properties on a
487			 * single dataset, we are performing a recursive
488			 * descent of related head datasets.  The caller
489			 * of this function only has a dataset hold on
490			 * the passed in head dataset, not the snapshots
491			 * associated with this dataset.  Without a hold,
492			 * the dataset pointer within callback records
493			 * for snapshots can be invalidated by eviction
494			 * at any time.
495			 *
496			 * Use dsl_dataset_try_add_ref() to verify
497			 * that the dataset for a snapshot has not
498			 * begun eviction processing and to prevent
499			 * eviction from occurring for the duration of
500			 * the callback.  If the hold attempt fails,
501			 * this object is already being evicted and the
502			 * callback can be safely ignored.
503			 */
504			if (ds != cbr->cbr_ds &&
505			    !dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
506				continue;
507
508			if (dsl_prop_get_ds(cbr->cbr_ds,
509			    cbr->cbr_pr->pr_propname, sizeof (value), 1,
510			    &value, NULL) == 0)
511				cbr->cbr_func(cbr->cbr_arg, value);
512
513			if (ds != cbr->cbr_ds)
514				dsl_dataset_rele(cbr->cbr_ds, FTAG);
515		}
516	}
517	mutex_exit(&dd->dd_lock);
518
519	return (0);
520}
521
522/*
523 * Update all property values for ddobj & its descendants.  This is used
524 * when renaming the dir.
525 */
526void
527dsl_prop_notify_all(dsl_dir_t *dd)
528{
529	dsl_pool_t *dp = dd->dd_pool;
530	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
531	(void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb,
532	    NULL, DS_FIND_CHILDREN);
533}
534
535static void
536dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
537    const char *propname, uint64_t value, int first)
538{
539	dsl_dir_t *dd;
540	dsl_prop_record_t *pr;
541	dsl_prop_cb_record_t *cbr;
542	objset_t *mos = dp->dp_meta_objset;
543	zap_cursor_t zc;
544	zap_attribute_t *za;
545	int err;
546
547	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
548	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
549	if (err)
550		return;
551
552	if (!first) {
553		/*
554		 * If the prop is set here, then this change is not
555		 * being inherited here or below; stop the recursion.
556		 */
557		err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
558		    propname);
559		if (err == 0) {
560			dsl_dir_rele(dd, FTAG);
561			return;
562		}
563		ASSERT3U(err, ==, ENOENT);
564	}
565
566	mutex_enter(&dd->dd_lock);
567	pr = dsl_prop_record_find(dd, propname);
568	if (pr != NULL) {
569		for (cbr = list_head(&pr->pr_cbs); cbr;
570		    cbr = list_next(&pr->pr_cbs, cbr)) {
571			uint64_t propobj;
572
573			/*
574			 * cbr->cbr_ds may be invalidated due to eviction,
575			 * requiring the use of dsl_dataset_try_add_ref().
576			 * See comment block in dsl_prop_notify_all_cb()
577			 * for details.
578			 */
579			if (!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
580				continue;
581
582			propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
583
584			/*
585			 * If the property is not set on this ds, then it is
586			 * inherited here; call the callback.
587			 */
588			if (propobj == 0 ||
589			    zap_contains(mos, propobj, propname) != 0)
590				cbr->cbr_func(cbr->cbr_arg, value);
591
592			dsl_dataset_rele(cbr->cbr_ds, FTAG);
593		}
594	}
595	mutex_exit(&dd->dd_lock);
596
597	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
598	for (zap_cursor_init(&zc, mos,
599	    dsl_dir_phys(dd)->dd_child_dir_zapobj);
600	    zap_cursor_retrieve(&zc, za) == 0;
601	    zap_cursor_advance(&zc)) {
602		dsl_prop_changed_notify(dp, za->za_first_integer,
603		    propname, value, FALSE);
604	}
605	kmem_free(za, sizeof (zap_attribute_t));
606	zap_cursor_fini(&zc);
607	dsl_dir_rele(dd, FTAG);
608}
609
610void
611dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname,
612    zprop_source_t source, int intsz, int numints, const void *value,
613    dmu_tx_t *tx)
614{
615	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
616	uint64_t zapobj, intval, dummy;
617	int isint;
618	char valbuf[32];
619	const char *valstr = NULL;
620	char *inheritstr;
621	char *recvdstr;
622	char *tbuf = NULL;
623	int err;
624	uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa);
625
626	isint = (dodefault(zfs_name_to_prop(propname), 8, 1, &intval) == 0);
627
628	if (ds->ds_is_snapshot) {
629		ASSERT(version >= SPA_VERSION_SNAP_PROPS);
630		if (dsl_dataset_phys(ds)->ds_props_obj == 0) {
631			dmu_buf_will_dirty(ds->ds_dbuf, tx);
632			dsl_dataset_phys(ds)->ds_props_obj =
633			    zap_create(mos,
634			    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
635		}
636		zapobj = dsl_dataset_phys(ds)->ds_props_obj;
637	} else {
638		zapobj = dsl_dir_phys(ds->ds_dir)->dd_props_zapobj;
639	}
640
641	if (version < SPA_VERSION_RECVD_PROPS) {
642		if (source & ZPROP_SRC_NONE)
643			source = ZPROP_SRC_NONE;
644		else if (source & ZPROP_SRC_RECEIVED)
645			source = ZPROP_SRC_LOCAL;
646	}
647
648	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
649	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
650
651	switch (source) {
652	case ZPROP_SRC_NONE:
653		/*
654		 * revert to received value, if any (inherit -S)
655		 * - remove propname
656		 * - remove propname$inherit
657		 */
658		err = zap_remove(mos, zapobj, propname, tx);
659		ASSERT(err == 0 || err == ENOENT);
660		err = zap_remove(mos, zapobj, inheritstr, tx);
661		ASSERT(err == 0 || err == ENOENT);
662		break;
663	case ZPROP_SRC_LOCAL:
664		/*
665		 * remove propname$inherit
666		 * set propname -> value
667		 */
668		err = zap_remove(mos, zapobj, inheritstr, tx);
669		ASSERT(err == 0 || err == ENOENT);
670		VERIFY0(zap_update(mos, zapobj, propname,
671		    intsz, numints, value, tx));
672		break;
673	case ZPROP_SRC_INHERITED:
674		/*
675		 * explicitly inherit
676		 * - remove propname
677		 * - set propname$inherit
678		 */
679		err = zap_remove(mos, zapobj, propname, tx);
680		ASSERT(err == 0 || err == ENOENT);
681		if (version >= SPA_VERSION_RECVD_PROPS &&
682		    dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) {
683			dummy = 0;
684			VERIFY0(zap_update(mos, zapobj, inheritstr,
685			    8, 1, &dummy, tx));
686		}
687		break;
688	case ZPROP_SRC_RECEIVED:
689		/*
690		 * set propname$recvd -> value
691		 */
692		err = zap_update(mos, zapobj, recvdstr,
693		    intsz, numints, value, tx);
694		ASSERT(err == 0);
695		break;
696	case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED):
697		/*
698		 * clear local and received settings
699		 * - remove propname
700		 * - remove propname$inherit
701		 * - remove propname$recvd
702		 */
703		err = zap_remove(mos, zapobj, propname, tx);
704		ASSERT(err == 0 || err == ENOENT);
705		err = zap_remove(mos, zapobj, inheritstr, tx);
706		ASSERT(err == 0 || err == ENOENT);
707		/* FALLTHRU */
708	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
709		/*
710		 * remove propname$recvd
711		 */
712		err = zap_remove(mos, zapobj, recvdstr, tx);
713		ASSERT(err == 0 || err == ENOENT);
714		break;
715	default:
716		cmn_err(CE_PANIC, "unexpected property source: %d", source);
717	}
718
719	strfree(inheritstr);
720	strfree(recvdstr);
721
722	if (isint) {
723		VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval));
724
725		if (ds->ds_is_snapshot) {
726			dsl_prop_cb_record_t *cbr;
727			/*
728			 * It's a snapshot; nothing can inherit this
729			 * property, so just look for callbacks on this
730			 * ds here.
731			 */
732			mutex_enter(&ds->ds_dir->dd_lock);
733			for (cbr = list_head(&ds->ds_prop_cbs); cbr;
734			    cbr = list_next(&ds->ds_prop_cbs, cbr)) {
735				if (strcmp(cbr->cbr_pr->pr_propname,
736				    propname) == 0)
737					cbr->cbr_func(cbr->cbr_arg, intval);
738			}
739			mutex_exit(&ds->ds_dir->dd_lock);
740		} else {
741			dsl_prop_changed_notify(ds->ds_dir->dd_pool,
742			    ds->ds_dir->dd_object, propname, intval, TRUE);
743		}
744
745		(void) snprintf(valbuf, sizeof (valbuf),
746		    "%lld", (longlong_t)intval);
747		valstr = valbuf;
748	} else {
749		if (source == ZPROP_SRC_LOCAL) {
750			valstr = value;
751		} else {
752			tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
753			if (dsl_prop_get_ds(ds, propname, 1,
754			    ZAP_MAXVALUELEN, tbuf, NULL) == 0)
755				valstr = tbuf;
756		}
757	}
758
759	spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE ||
760	    source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx,
761	    "%s=%s", propname, (valstr == NULL ? "" : valstr));
762
763	if (tbuf != NULL)
764		kmem_free(tbuf, ZAP_MAXVALUELEN);
765}
766
767int
768dsl_prop_set_int(const char *dsname, const char *propname,
769    zprop_source_t source, uint64_t value)
770{
771	nvlist_t *nvl = fnvlist_alloc();
772	int error;
773
774	fnvlist_add_uint64(nvl, propname, value);
775	error = dsl_props_set(dsname, source, nvl);
776	fnvlist_free(nvl);
777	return (error);
778}
779
780int
781dsl_prop_set_string(const char *dsname, const char *propname,
782    zprop_source_t source, const char *value)
783{
784	nvlist_t *nvl = fnvlist_alloc();
785	int error;
786
787	fnvlist_add_string(nvl, propname, value);
788	error = dsl_props_set(dsname, source, nvl);
789	fnvlist_free(nvl);
790	return (error);
791}
792
793int
794dsl_prop_inherit(const char *dsname, const char *propname,
795    zprop_source_t source)
796{
797	nvlist_t *nvl = fnvlist_alloc();
798	int error;
799
800	fnvlist_add_boolean(nvl, propname);
801	error = dsl_props_set(dsname, source, nvl);
802	fnvlist_free(nvl);
803	return (error);
804}
805
806typedef struct dsl_props_set_arg {
807	const char *dpsa_dsname;
808	zprop_source_t dpsa_source;
809	nvlist_t *dpsa_props;
810} dsl_props_set_arg_t;
811
812static int
813dsl_props_set_check(void *arg, dmu_tx_t *tx)
814{
815	dsl_props_set_arg_t *dpsa = arg;
816	dsl_pool_t *dp = dmu_tx_pool(tx);
817	dsl_dataset_t *ds;
818	uint64_t version;
819	nvpair_t *elem = NULL;
820	int err;
821
822	err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds);
823	if (err != 0)
824		return (err);
825
826	version = spa_version(ds->ds_dir->dd_pool->dp_spa);
827	while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) {
828		if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
829			dsl_dataset_rele(ds, FTAG);
830			return (SET_ERROR(ENAMETOOLONG));
831		}
832		if (nvpair_type(elem) == DATA_TYPE_STRING) {
833			char *valstr = fnvpair_value_string(elem);
834			if (strlen(valstr) >= (version <
835			    SPA_VERSION_STMF_PROP ?
836			    ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) {
837				dsl_dataset_rele(ds, FTAG);
838				return (E2BIG);
839			}
840		}
841	}
842
843	if (ds->ds_is_snapshot && version < SPA_VERSION_SNAP_PROPS) {
844		dsl_dataset_rele(ds, FTAG);
845		return (SET_ERROR(ENOTSUP));
846	}
847	dsl_dataset_rele(ds, FTAG);
848	return (0);
849}
850
851void
852dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source,
853    nvlist_t *props, dmu_tx_t *tx)
854{
855	nvpair_t *elem = NULL;
856
857	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
858		nvpair_t *pair = elem;
859		const char *name = nvpair_name(pair);
860
861		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
862			/*
863			 * This usually happens when we reuse the nvlist_t data
864			 * returned by the counterpart dsl_prop_get_all_impl().
865			 * For instance we do this to restore the original
866			 * received properties when an error occurs in the
867			 * zfs_ioc_recv() codepath.
868			 */
869			nvlist_t *attrs = fnvpair_value_nvlist(pair);
870			pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
871		}
872
873		if (nvpair_type(pair) == DATA_TYPE_STRING) {
874			const char *value = fnvpair_value_string(pair);
875			dsl_prop_set_sync_impl(ds, name,
876			    source, 1, strlen(value) + 1, value, tx);
877		} else if (nvpair_type(pair) == DATA_TYPE_UINT64) {
878			uint64_t intval = fnvpair_value_uint64(pair);
879			dsl_prop_set_sync_impl(ds, name,
880			    source, sizeof (intval), 1, &intval, tx);
881		} else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) {
882			dsl_prop_set_sync_impl(ds, name,
883			    source, 0, 0, NULL, tx);
884		} else {
885			panic("invalid nvpair type");
886		}
887	}
888}
889
890static void
891dsl_props_set_sync(void *arg, dmu_tx_t *tx)
892{
893	dsl_props_set_arg_t *dpsa = arg;
894	dsl_pool_t *dp = dmu_tx_pool(tx);
895	dsl_dataset_t *ds;
896
897	VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds));
898	dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx);
899	dsl_dataset_rele(ds, FTAG);
900}
901
902/*
903 * All-or-nothing; if any prop can't be set, nothing will be modified.
904 */
905int
906dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props)
907{
908	dsl_props_set_arg_t dpsa;
909	int nblks = 0;
910
911	dpsa.dpsa_dsname = dsname;
912	dpsa.dpsa_source = source;
913	dpsa.dpsa_props = props;
914
915	/*
916	 * If the source includes NONE, then we will only be removing entries
917	 * from the ZAP object.  In that case don't check for ENOSPC.
918	 */
919	if ((source & ZPROP_SRC_NONE) == 0)
920		nblks = 2 * fnvlist_num_pairs(props);
921
922	return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync,
923	    &dpsa, nblks, ZFS_SPACE_CHECK_RESERVED));
924}
925
926typedef enum dsl_prop_getflags {
927	DSL_PROP_GET_INHERITING = 0x1,	/* searching parent of target ds */
928	DSL_PROP_GET_SNAPSHOT = 0x2,	/* snapshot dataset */
929	DSL_PROP_GET_LOCAL = 0x4,	/* local properties */
930	DSL_PROP_GET_RECEIVED = 0x8	/* received properties */
931} dsl_prop_getflags_t;
932
933static int
934dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj,
935    const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv)
936{
937	zap_cursor_t zc;
938	zap_attribute_t za;
939	int err = 0;
940
941	for (zap_cursor_init(&zc, mos, propobj);
942	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
943	    zap_cursor_advance(&zc)) {
944		nvlist_t *propval;
945		zfs_prop_t prop;
946		char buf[ZAP_MAXNAMELEN];
947		char *valstr;
948		const char *suffix;
949		const char *propname;
950		const char *source;
951
952		suffix = strchr(za.za_name, '$');
953
954		if (suffix == NULL) {
955			/*
956			 * Skip local properties if we only want received
957			 * properties.
958			 */
959			if (flags & DSL_PROP_GET_RECEIVED)
960				continue;
961
962			propname = za.za_name;
963			source = setpoint;
964		} else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) {
965			/* Skip explicitly inherited entries. */
966			continue;
967		} else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) {
968			if (flags & DSL_PROP_GET_LOCAL)
969				continue;
970
971			(void) strncpy(buf, za.za_name, (suffix - za.za_name));
972			buf[suffix - za.za_name] = '\0';
973			propname = buf;
974
975			if (!(flags & DSL_PROP_GET_RECEIVED)) {
976				/* Skip if locally overridden. */
977				err = zap_contains(mos, propobj, propname);
978				if (err == 0)
979					continue;
980				if (err != ENOENT)
981					break;
982
983				/* Skip if explicitly inherited. */
984				valstr = kmem_asprintf("%s%s", propname,
985				    ZPROP_INHERIT_SUFFIX);
986				err = zap_contains(mos, propobj, valstr);
987				strfree(valstr);
988				if (err == 0)
989					continue;
990				if (err != ENOENT)
991					break;
992			}
993
994			source = ((flags & DSL_PROP_GET_INHERITING) ?
995			    setpoint : ZPROP_SOURCE_VAL_RECVD);
996		} else {
997			/*
998			 * For backward compatibility, skip suffixes we don't
999			 * recognize.
1000			 */
1001			continue;
1002		}
1003
1004		prop = zfs_name_to_prop(propname);
1005
1006		/* Skip non-inheritable properties. */
1007		if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL &&
1008		    !zfs_prop_inheritable(prop))
1009			continue;
1010
1011		/* Skip properties not valid for this type. */
1012		if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL &&
1013		    !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
1014			continue;
1015
1016		/* Skip properties already defined. */
1017		if (nvlist_exists(nv, propname))
1018			continue;
1019
1020		VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1021		if (za.za_integer_length == 1) {
1022			/*
1023			 * String property
1024			 */
1025			char *tmp = kmem_alloc(za.za_num_integers,
1026			    KM_SLEEP);
1027			err = zap_lookup(mos, propobj,
1028			    za.za_name, 1, za.za_num_integers, tmp);
1029			if (err != 0) {
1030				kmem_free(tmp, za.za_num_integers);
1031				break;
1032			}
1033			VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
1034			    tmp) == 0);
1035			kmem_free(tmp, za.za_num_integers);
1036		} else {
1037			/*
1038			 * Integer property
1039			 */
1040			ASSERT(za.za_integer_length == 8);
1041			(void) nvlist_add_uint64(propval, ZPROP_VALUE,
1042			    za.za_first_integer);
1043		}
1044
1045		VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0);
1046		VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1047		nvlist_free(propval);
1048	}
1049	zap_cursor_fini(&zc);
1050	if (err == ENOENT)
1051		err = 0;
1052	return (err);
1053}
1054
1055/*
1056 * Iterate over all properties for this dataset and return them in an nvlist.
1057 */
1058static int
1059dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp,
1060    dsl_prop_getflags_t flags)
1061{
1062	dsl_dir_t *dd = ds->ds_dir;
1063	dsl_pool_t *dp = dd->dd_pool;
1064	objset_t *mos = dp->dp_meta_objset;
1065	int err = 0;
1066	char setpoint[ZFS_MAX_DATASET_NAME_LEN];
1067
1068	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1069
1070	if (ds->ds_is_snapshot)
1071		flags |= DSL_PROP_GET_SNAPSHOT;
1072
1073	ASSERT(dsl_pool_config_held(dp));
1074
1075	if (dsl_dataset_phys(ds)->ds_props_obj != 0) {
1076		ASSERT(flags & DSL_PROP_GET_SNAPSHOT);
1077		dsl_dataset_name(ds, setpoint);
1078		err = dsl_prop_get_all_impl(mos,
1079		    dsl_dataset_phys(ds)->ds_props_obj, setpoint, flags, *nvp);
1080		if (err)
1081			goto out;
1082	}
1083
1084	for (; dd != NULL; dd = dd->dd_parent) {
1085		if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) {
1086			if (flags & (DSL_PROP_GET_LOCAL |
1087			    DSL_PROP_GET_RECEIVED))
1088				break;
1089			flags |= DSL_PROP_GET_INHERITING;
1090		}
1091		dsl_dir_name(dd, setpoint);
1092		err = dsl_prop_get_all_impl(mos,
1093		    dsl_dir_phys(dd)->dd_props_zapobj, setpoint, flags, *nvp);
1094		if (err)
1095			break;
1096	}
1097out:
1098	return (err);
1099}
1100
1101boolean_t
1102dsl_prop_get_hasrecvd(const char *dsname)
1103{
1104	uint64_t dummy;
1105
1106	return (0 ==
1107	    dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL));
1108}
1109
1110static int
1111dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source)
1112{
1113	uint64_t version;
1114	spa_t *spa;
1115	int error = 0;
1116
1117	VERIFY0(spa_open(dsname, &spa, FTAG));
1118	version = spa_version(spa);
1119	spa_close(spa, FTAG);
1120
1121	if (version >= SPA_VERSION_RECVD_PROPS)
1122		error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0);
1123	return (error);
1124}
1125
1126/*
1127 * Call after successfully receiving properties to ensure that only the first
1128 * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
1129 */
1130int
1131dsl_prop_set_hasrecvd(const char *dsname)
1132{
1133	int error = 0;
1134	if (!dsl_prop_get_hasrecvd(dsname))
1135		error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL);
1136	return (error);
1137}
1138
1139void
1140dsl_prop_unset_hasrecvd(const char *dsname)
1141{
1142	VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE));
1143}
1144
1145int
1146dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
1147{
1148	return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0));
1149}
1150
1151int
1152dsl_prop_get_received(const char *dsname, nvlist_t **nvp)
1153{
1154	objset_t *os;
1155	int error;
1156
1157	/*
1158	 * Received properties are not distinguishable from local properties
1159	 * until the dataset has received properties on or after
1160	 * SPA_VERSION_RECVD_PROPS.
1161	 */
1162	dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ?
1163	    DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL);
1164
1165	error = dmu_objset_hold(dsname, FTAG, &os);
1166	if (error != 0)
1167		return (error);
1168	error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags);
1169	dmu_objset_rele(os, FTAG);
1170	return (error);
1171}
1172
1173void
1174dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
1175{
1176	nvlist_t *propval;
1177	const char *propname = zfs_prop_to_name(prop);
1178	uint64_t default_value;
1179
1180	if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
1181		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
1182		return;
1183	}
1184
1185	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1186	VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
1187	/* Indicate the default source if we can. */
1188	if (dodefault(prop, 8, 1, &default_value) == 0 &&
1189	    value == default_value) {
1190		VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0);
1191	}
1192	VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1193	nvlist_free(propval);
1194}
1195
1196void
1197dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
1198{
1199	nvlist_t *propval;
1200	const char *propname = zfs_prop_to_name(prop);
1201
1202	if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
1203		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
1204		return;
1205	}
1206
1207	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1208	VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
1209	VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1210	nvlist_free(propval);
1211}
1212