xref: /titanic_51/usr/src/uts/common/fs/zfs/dsl_prop.c (revision a6a74e0e62d62ff750cd4b790be5eacc99c3bb8c)
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, 2014 by Delphix. All rights reserved.
24  * Copyright (c) 2013 Martin Matuska. All rights reserved.
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 
44 static int
45 dodefault(const char *propname, int intsz, int numints, void *buf)
46 {
47 	zfs_prop_t prop;
48 
49 	/*
50 	 * The setonce properties are read-only, BUT they still
51 	 * have a default value that can be used as the initial
52 	 * value.
53 	 */
54 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL ||
55 	    (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
56 		return (SET_ERROR(ENOENT));
57 
58 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
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 
73 int
74 dsl_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(propname, intsz, numints, buf);
153 
154 	strfree(inheritstr);
155 	strfree(recvdstr);
156 
157 	return (err);
158 }
159 
160 int
161 dsl_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 
219 /*
220  * Register interest in the named property.  We'll call the callback
221  * once to notify it of the current property value, and again each time
222  * the property changes, until this callback is unregistered.
223  *
224  * Return 0 on success, errno if the prop is not an integer value.
225  */
226 int
227 dsl_prop_register(dsl_dataset_t *ds, const char *propname,
228     dsl_prop_changed_cb_t *callback, void *cbarg)
229 {
230 	dsl_dir_t *dd = ds->ds_dir;
231 	dsl_pool_t *dp = dd->dd_pool;
232 	uint64_t value;
233 	dsl_prop_cb_record_t *cbr;
234 	int err;
235 
236 	ASSERT(dsl_pool_config_held(dp));
237 
238 	err = dsl_prop_get_int_ds(ds, propname, &value);
239 	if (err != 0)
240 		return (err);
241 
242 	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
243 	cbr->cbr_ds = ds;
244 	cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
245 	(void) strcpy((char *)cbr->cbr_propname, propname);
246 	cbr->cbr_func = callback;
247 	cbr->cbr_arg = cbarg;
248 	mutex_enter(&dd->dd_lock);
249 	list_insert_head(&dd->dd_prop_cbs, cbr);
250 	mutex_exit(&dd->dd_lock);
251 
252 	cbr->cbr_func(cbr->cbr_arg, value);
253 	return (0);
254 }
255 
256 int
257 dsl_prop_get(const char *dsname, const char *propname,
258     int intsz, int numints, void *buf, char *setpoint)
259 {
260 	objset_t *os;
261 	int error;
262 
263 	error = dmu_objset_hold(dsname, FTAG, &os);
264 	if (error != 0)
265 		return (error);
266 
267 	error = dsl_prop_get_ds(dmu_objset_ds(os), propname,
268 	    intsz, numints, buf, setpoint);
269 
270 	dmu_objset_rele(os, FTAG);
271 	return (error);
272 }
273 
274 /*
275  * Get the current property value.  It may have changed by the time this
276  * function returns, so it is NOT safe to follow up with
277  * dsl_prop_register() and assume that the value has not changed in
278  * between.
279  *
280  * Return 0 on success, ENOENT if ddname is invalid.
281  */
282 int
283 dsl_prop_get_integer(const char *ddname, const char *propname,
284     uint64_t *valuep, char *setpoint)
285 {
286 	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
287 }
288 
289 int
290 dsl_prop_get_int_ds(dsl_dataset_t *ds, const char *propname,
291     uint64_t *valuep)
292 {
293 	return (dsl_prop_get_ds(ds, propname, 8, 1, valuep, NULL));
294 }
295 
296 /*
297  * Predict the effective value of the given special property if it were set with
298  * the given value and source. This is not a general purpose function. It exists
299  * only to handle the special requirements of the quota and reservation
300  * properties. The fact that these properties are non-inheritable greatly
301  * simplifies the prediction logic.
302  *
303  * Returns 0 on success, a positive error code on failure, or -1 if called with
304  * a property not handled by this function.
305  */
306 int
307 dsl_prop_predict(dsl_dir_t *dd, const char *propname,
308     zprop_source_t source, uint64_t value, uint64_t *newvalp)
309 {
310 	zfs_prop_t prop = zfs_name_to_prop(propname);
311 	objset_t *mos;
312 	uint64_t zapobj;
313 	uint64_t version;
314 	char *recvdstr;
315 	int err = 0;
316 
317 	switch (prop) {
318 	case ZFS_PROP_QUOTA:
319 	case ZFS_PROP_RESERVATION:
320 	case ZFS_PROP_REFQUOTA:
321 	case ZFS_PROP_REFRESERVATION:
322 		break;
323 	default:
324 		return (-1);
325 	}
326 
327 	mos = dd->dd_pool->dp_meta_objset;
328 	zapobj = dsl_dir_phys(dd)->dd_props_zapobj;
329 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
330 
331 	version = spa_version(dd->dd_pool->dp_spa);
332 	if (version < SPA_VERSION_RECVD_PROPS) {
333 		if (source & ZPROP_SRC_NONE)
334 			source = ZPROP_SRC_NONE;
335 		else if (source & ZPROP_SRC_RECEIVED)
336 			source = ZPROP_SRC_LOCAL;
337 	}
338 
339 	switch (source) {
340 	case ZPROP_SRC_NONE:
341 		/* Revert to the received value, if any. */
342 		err = zap_lookup(mos, zapobj, recvdstr, 8, 1, newvalp);
343 		if (err == ENOENT)
344 			*newvalp = 0;
345 		break;
346 	case ZPROP_SRC_LOCAL:
347 		*newvalp = value;
348 		break;
349 	case ZPROP_SRC_RECEIVED:
350 		/*
351 		 * If there's no local setting, then the new received value will
352 		 * be the effective value.
353 		 */
354 		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
355 		if (err == ENOENT)
356 			*newvalp = value;
357 		break;
358 	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
359 		/*
360 		 * We're clearing the received value, so the local setting (if
361 		 * it exists) remains the effective value.
362 		 */
363 		err = zap_lookup(mos, zapobj, propname, 8, 1, newvalp);
364 		if (err == ENOENT)
365 			*newvalp = 0;
366 		break;
367 	default:
368 		panic("unexpected property source: %d", source);
369 	}
370 
371 	strfree(recvdstr);
372 
373 	if (err == ENOENT)
374 		return (0);
375 
376 	return (err);
377 }
378 
379 /*
380  * Unregister this callback.  Return 0 on success, ENOENT if ddname is
381  * invalid, or ENOMSG if no matching callback registered.
382  */
383 int
384 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
385     dsl_prop_changed_cb_t *callback, void *cbarg)
386 {
387 	dsl_dir_t *dd = ds->ds_dir;
388 	dsl_prop_cb_record_t *cbr;
389 
390 	mutex_enter(&dd->dd_lock);
391 	for (cbr = list_head(&dd->dd_prop_cbs);
392 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
393 		if (cbr->cbr_ds == ds &&
394 		    cbr->cbr_func == callback &&
395 		    cbr->cbr_arg == cbarg &&
396 		    strcmp(cbr->cbr_propname, propname) == 0)
397 			break;
398 	}
399 
400 	if (cbr == NULL) {
401 		mutex_exit(&dd->dd_lock);
402 		return (SET_ERROR(ENOMSG));
403 	}
404 
405 	list_remove(&dd->dd_prop_cbs, cbr);
406 	mutex_exit(&dd->dd_lock);
407 	kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
408 	kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
409 
410 	return (0);
411 }
412 
413 boolean_t
414 dsl_prop_hascb(dsl_dataset_t *ds)
415 {
416 	dsl_dir_t *dd = ds->ds_dir;
417 	boolean_t rv = B_FALSE;
418 	dsl_prop_cb_record_t *cbr;
419 
420 	mutex_enter(&dd->dd_lock);
421 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
422 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
423 		if (cbr->cbr_ds == ds) {
424 			rv = B_TRUE;
425 			break;
426 		}
427 	}
428 	mutex_exit(&dd->dd_lock);
429 	return (rv);
430 }
431 
432 /* ARGSUSED */
433 static int
434 dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
435 {
436 	dsl_dir_t *dd = ds->ds_dir;
437 	dsl_prop_cb_record_t *cbr;
438 
439 	mutex_enter(&dd->dd_lock);
440 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
441 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
442 		uint64_t value;
443 
444 		if (dsl_prop_get_ds(cbr->cbr_ds, cbr->cbr_propname,
445 		    sizeof (value), 1, &value, NULL) == 0)
446 			cbr->cbr_func(cbr->cbr_arg, value);
447 	}
448 	mutex_exit(&dd->dd_lock);
449 
450 	return (0);
451 }
452 
453 /*
454  * Update all property values for ddobj & its descendants.  This is used
455  * when renaming the dir.
456  */
457 void
458 dsl_prop_notify_all(dsl_dir_t *dd)
459 {
460 	dsl_pool_t *dp = dd->dd_pool;
461 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
462 	(void) dmu_objset_find_dp(dp, dd->dd_object, dsl_prop_notify_all_cb,
463 	    NULL, DS_FIND_CHILDREN);
464 }
465 
466 static void
467 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
468     const char *propname, uint64_t value, int first)
469 {
470 	dsl_dir_t *dd;
471 	dsl_prop_cb_record_t *cbr;
472 	objset_t *mos = dp->dp_meta_objset;
473 	zap_cursor_t zc;
474 	zap_attribute_t *za;
475 	int err;
476 
477 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
478 	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
479 	if (err)
480 		return;
481 
482 	if (!first) {
483 		/*
484 		 * If the prop is set here, then this change is not
485 		 * being inherited here or below; stop the recursion.
486 		 */
487 		err = zap_contains(mos, dsl_dir_phys(dd)->dd_props_zapobj,
488 		    propname);
489 		if (err == 0) {
490 			dsl_dir_rele(dd, FTAG);
491 			return;
492 		}
493 		ASSERT3U(err, ==, ENOENT);
494 	}
495 
496 	mutex_enter(&dd->dd_lock);
497 	for (cbr = list_head(&dd->dd_prop_cbs); cbr;
498 	    cbr = list_next(&dd->dd_prop_cbs, cbr)) {
499 		uint64_t propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
500 
501 		if (strcmp(cbr->cbr_propname, propname) != 0)
502 			continue;
503 
504 		/*
505 		 * If the property is set on this ds, then it is not
506 		 * inherited here; don't call the callback.
507 		 */
508 		if (propobj && 0 == zap_contains(mos, propobj, propname))
509 			continue;
510 
511 		cbr->cbr_func(cbr->cbr_arg, value);
512 	}
513 	mutex_exit(&dd->dd_lock);
514 
515 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
516 	for (zap_cursor_init(&zc, mos,
517 	    dsl_dir_phys(dd)->dd_child_dir_zapobj);
518 	    zap_cursor_retrieve(&zc, za) == 0;
519 	    zap_cursor_advance(&zc)) {
520 		dsl_prop_changed_notify(dp, za->za_first_integer,
521 		    propname, value, FALSE);
522 	}
523 	kmem_free(za, sizeof (zap_attribute_t));
524 	zap_cursor_fini(&zc);
525 	dsl_dir_rele(dd, FTAG);
526 }
527 
528 void
529 dsl_prop_set_sync_impl(dsl_dataset_t *ds, const char *propname,
530     zprop_source_t source, int intsz, int numints, const void *value,
531     dmu_tx_t *tx)
532 {
533 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
534 	uint64_t zapobj, intval, dummy;
535 	int isint;
536 	char valbuf[32];
537 	const char *valstr = NULL;
538 	char *inheritstr;
539 	char *recvdstr;
540 	char *tbuf = NULL;
541 	int err;
542 	uint64_t version = spa_version(ds->ds_dir->dd_pool->dp_spa);
543 
544 	isint = (dodefault(propname, 8, 1, &intval) == 0);
545 
546 	if (ds->ds_is_snapshot) {
547 		ASSERT(version >= SPA_VERSION_SNAP_PROPS);
548 		if (dsl_dataset_phys(ds)->ds_props_obj == 0) {
549 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
550 			dsl_dataset_phys(ds)->ds_props_obj =
551 			    zap_create(mos,
552 			    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
553 		}
554 		zapobj = dsl_dataset_phys(ds)->ds_props_obj;
555 	} else {
556 		zapobj = dsl_dir_phys(ds->ds_dir)->dd_props_zapobj;
557 	}
558 
559 	if (version < SPA_VERSION_RECVD_PROPS) {
560 		if (source & ZPROP_SRC_NONE)
561 			source = ZPROP_SRC_NONE;
562 		else if (source & ZPROP_SRC_RECEIVED)
563 			source = ZPROP_SRC_LOCAL;
564 	}
565 
566 	inheritstr = kmem_asprintf("%s%s", propname, ZPROP_INHERIT_SUFFIX);
567 	recvdstr = kmem_asprintf("%s%s", propname, ZPROP_RECVD_SUFFIX);
568 
569 	switch (source) {
570 	case ZPROP_SRC_NONE:
571 		/*
572 		 * revert to received value, if any (inherit -S)
573 		 * - remove propname
574 		 * - remove propname$inherit
575 		 */
576 		err = zap_remove(mos, zapobj, propname, tx);
577 		ASSERT(err == 0 || err == ENOENT);
578 		err = zap_remove(mos, zapobj, inheritstr, tx);
579 		ASSERT(err == 0 || err == ENOENT);
580 		break;
581 	case ZPROP_SRC_LOCAL:
582 		/*
583 		 * remove propname$inherit
584 		 * set propname -> value
585 		 */
586 		err = zap_remove(mos, zapobj, inheritstr, tx);
587 		ASSERT(err == 0 || err == ENOENT);
588 		VERIFY0(zap_update(mos, zapobj, propname,
589 		    intsz, numints, value, tx));
590 		break;
591 	case ZPROP_SRC_INHERITED:
592 		/*
593 		 * explicitly inherit
594 		 * - remove propname
595 		 * - set propname$inherit
596 		 */
597 		err = zap_remove(mos, zapobj, propname, tx);
598 		ASSERT(err == 0 || err == ENOENT);
599 		if (version >= SPA_VERSION_RECVD_PROPS &&
600 		    dsl_prop_get_int_ds(ds, ZPROP_HAS_RECVD, &dummy) == 0) {
601 			dummy = 0;
602 			VERIFY0(zap_update(mos, zapobj, inheritstr,
603 			    8, 1, &dummy, tx));
604 		}
605 		break;
606 	case ZPROP_SRC_RECEIVED:
607 		/*
608 		 * set propname$recvd -> value
609 		 */
610 		err = zap_update(mos, zapobj, recvdstr,
611 		    intsz, numints, value, tx);
612 		ASSERT(err == 0);
613 		break;
614 	case (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED):
615 		/*
616 		 * clear local and received settings
617 		 * - remove propname
618 		 * - remove propname$inherit
619 		 * - remove propname$recvd
620 		 */
621 		err = zap_remove(mos, zapobj, propname, tx);
622 		ASSERT(err == 0 || err == ENOENT);
623 		err = zap_remove(mos, zapobj, inheritstr, tx);
624 		ASSERT(err == 0 || err == ENOENT);
625 		/* FALLTHRU */
626 	case (ZPROP_SRC_NONE | ZPROP_SRC_RECEIVED):
627 		/*
628 		 * remove propname$recvd
629 		 */
630 		err = zap_remove(mos, zapobj, recvdstr, tx);
631 		ASSERT(err == 0 || err == ENOENT);
632 		break;
633 	default:
634 		cmn_err(CE_PANIC, "unexpected property source: %d", source);
635 	}
636 
637 	strfree(inheritstr);
638 	strfree(recvdstr);
639 
640 	if (isint) {
641 		VERIFY0(dsl_prop_get_int_ds(ds, propname, &intval));
642 
643 		if (ds->ds_is_snapshot) {
644 			dsl_prop_cb_record_t *cbr;
645 			/*
646 			 * It's a snapshot; nothing can inherit this
647 			 * property, so just look for callbacks on this
648 			 * ds here.
649 			 */
650 			mutex_enter(&ds->ds_dir->dd_lock);
651 			for (cbr = list_head(&ds->ds_dir->dd_prop_cbs); cbr;
652 			    cbr = list_next(&ds->ds_dir->dd_prop_cbs, cbr)) {
653 				if (cbr->cbr_ds == ds &&
654 				    strcmp(cbr->cbr_propname, propname) == 0)
655 					cbr->cbr_func(cbr->cbr_arg, intval);
656 			}
657 			mutex_exit(&ds->ds_dir->dd_lock);
658 		} else {
659 			dsl_prop_changed_notify(ds->ds_dir->dd_pool,
660 			    ds->ds_dir->dd_object, propname, intval, TRUE);
661 		}
662 
663 		(void) snprintf(valbuf, sizeof (valbuf),
664 		    "%lld", (longlong_t)intval);
665 		valstr = valbuf;
666 	} else {
667 		if (source == ZPROP_SRC_LOCAL) {
668 			valstr = value;
669 		} else {
670 			tbuf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
671 			if (dsl_prop_get_ds(ds, propname, 1,
672 			    ZAP_MAXVALUELEN, tbuf, NULL) == 0)
673 				valstr = tbuf;
674 		}
675 	}
676 
677 	spa_history_log_internal_ds(ds, (source == ZPROP_SRC_NONE ||
678 	    source == ZPROP_SRC_INHERITED) ? "inherit" : "set", tx,
679 	    "%s=%s", propname, (valstr == NULL ? "" : valstr));
680 
681 	if (tbuf != NULL)
682 		kmem_free(tbuf, ZAP_MAXVALUELEN);
683 }
684 
685 int
686 dsl_prop_set_int(const char *dsname, const char *propname,
687     zprop_source_t source, uint64_t value)
688 {
689 	nvlist_t *nvl = fnvlist_alloc();
690 	int error;
691 
692 	fnvlist_add_uint64(nvl, propname, value);
693 	error = dsl_props_set(dsname, source, nvl);
694 	fnvlist_free(nvl);
695 	return (error);
696 }
697 
698 int
699 dsl_prop_set_string(const char *dsname, const char *propname,
700     zprop_source_t source, const char *value)
701 {
702 	nvlist_t *nvl = fnvlist_alloc();
703 	int error;
704 
705 	fnvlist_add_string(nvl, propname, value);
706 	error = dsl_props_set(dsname, source, nvl);
707 	fnvlist_free(nvl);
708 	return (error);
709 }
710 
711 int
712 dsl_prop_inherit(const char *dsname, const char *propname,
713     zprop_source_t source)
714 {
715 	nvlist_t *nvl = fnvlist_alloc();
716 	int error;
717 
718 	fnvlist_add_boolean(nvl, propname);
719 	error = dsl_props_set(dsname, source, nvl);
720 	fnvlist_free(nvl);
721 	return (error);
722 }
723 
724 typedef struct dsl_props_set_arg {
725 	const char *dpsa_dsname;
726 	zprop_source_t dpsa_source;
727 	nvlist_t *dpsa_props;
728 } dsl_props_set_arg_t;
729 
730 static int
731 dsl_props_set_check(void *arg, dmu_tx_t *tx)
732 {
733 	dsl_props_set_arg_t *dpsa = arg;
734 	dsl_pool_t *dp = dmu_tx_pool(tx);
735 	dsl_dataset_t *ds;
736 	uint64_t version;
737 	nvpair_t *elem = NULL;
738 	int err;
739 
740 	err = dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds);
741 	if (err != 0)
742 		return (err);
743 
744 	version = spa_version(ds->ds_dir->dd_pool->dp_spa);
745 	while ((elem = nvlist_next_nvpair(dpsa->dpsa_props, elem)) != NULL) {
746 		if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
747 			dsl_dataset_rele(ds, FTAG);
748 			return (SET_ERROR(ENAMETOOLONG));
749 		}
750 		if (nvpair_type(elem) == DATA_TYPE_STRING) {
751 			char *valstr = fnvpair_value_string(elem);
752 			if (strlen(valstr) >= (version <
753 			    SPA_VERSION_STMF_PROP ?
754 			    ZAP_OLDMAXVALUELEN : ZAP_MAXVALUELEN)) {
755 				dsl_dataset_rele(ds, FTAG);
756 				return (E2BIG);
757 			}
758 		}
759 	}
760 
761 	if (ds->ds_is_snapshot && version < SPA_VERSION_SNAP_PROPS) {
762 		dsl_dataset_rele(ds, FTAG);
763 		return (SET_ERROR(ENOTSUP));
764 	}
765 	dsl_dataset_rele(ds, FTAG);
766 	return (0);
767 }
768 
769 void
770 dsl_props_set_sync_impl(dsl_dataset_t *ds, zprop_source_t source,
771     nvlist_t *props, dmu_tx_t *tx)
772 {
773 	nvpair_t *elem = NULL;
774 
775 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
776 		nvpair_t *pair = elem;
777 
778 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
779 			/*
780 			 * dsl_prop_get_all_impl() returns properties in this
781 			 * format.
782 			 */
783 			nvlist_t *attrs = fnvpair_value_nvlist(pair);
784 			pair = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
785 		}
786 
787 		if (nvpair_type(pair) == DATA_TYPE_STRING) {
788 			const char *value = fnvpair_value_string(pair);
789 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
790 			    source, 1, strlen(value) + 1, value, tx);
791 		} else if (nvpair_type(pair) == DATA_TYPE_UINT64) {
792 			uint64_t intval = fnvpair_value_uint64(pair);
793 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
794 			    source, sizeof (intval), 1, &intval, tx);
795 		} else if (nvpair_type(pair) == DATA_TYPE_BOOLEAN) {
796 			dsl_prop_set_sync_impl(ds, nvpair_name(pair),
797 			    source, 0, 0, NULL, tx);
798 		} else {
799 			panic("invalid nvpair type");
800 		}
801 	}
802 }
803 
804 static void
805 dsl_props_set_sync(void *arg, dmu_tx_t *tx)
806 {
807 	dsl_props_set_arg_t *dpsa = arg;
808 	dsl_pool_t *dp = dmu_tx_pool(tx);
809 	dsl_dataset_t *ds;
810 
811 	VERIFY0(dsl_dataset_hold(dp, dpsa->dpsa_dsname, FTAG, &ds));
812 	dsl_props_set_sync_impl(ds, dpsa->dpsa_source, dpsa->dpsa_props, tx);
813 	dsl_dataset_rele(ds, FTAG);
814 }
815 
816 /*
817  * All-or-nothing; if any prop can't be set, nothing will be modified.
818  */
819 int
820 dsl_props_set(const char *dsname, zprop_source_t source, nvlist_t *props)
821 {
822 	dsl_props_set_arg_t dpsa;
823 	int nblks = 0;
824 
825 	dpsa.dpsa_dsname = dsname;
826 	dpsa.dpsa_source = source;
827 	dpsa.dpsa_props = props;
828 
829 	/*
830 	 * If the source includes NONE, then we will only be removing entries
831 	 * from the ZAP object.  In that case don't check for ENOSPC.
832 	 */
833 	if ((source & ZPROP_SRC_NONE) == 0)
834 		nblks = 2 * fnvlist_num_pairs(props);
835 
836 	return (dsl_sync_task(dsname, dsl_props_set_check, dsl_props_set_sync,
837 	    &dpsa, nblks, ZFS_SPACE_CHECK_RESERVED));
838 }
839 
840 typedef enum dsl_prop_getflags {
841 	DSL_PROP_GET_INHERITING = 0x1,	/* searching parent of target ds */
842 	DSL_PROP_GET_SNAPSHOT = 0x2,	/* snapshot dataset */
843 	DSL_PROP_GET_LOCAL = 0x4,	/* local properties */
844 	DSL_PROP_GET_RECEIVED = 0x8	/* received properties */
845 } dsl_prop_getflags_t;
846 
847 static int
848 dsl_prop_get_all_impl(objset_t *mos, uint64_t propobj,
849     const char *setpoint, dsl_prop_getflags_t flags, nvlist_t *nv)
850 {
851 	zap_cursor_t zc;
852 	zap_attribute_t za;
853 	int err = 0;
854 
855 	for (zap_cursor_init(&zc, mos, propobj);
856 	    (err = zap_cursor_retrieve(&zc, &za)) == 0;
857 	    zap_cursor_advance(&zc)) {
858 		nvlist_t *propval;
859 		zfs_prop_t prop;
860 		char buf[ZAP_MAXNAMELEN];
861 		char *valstr;
862 		const char *suffix;
863 		const char *propname;
864 		const char *source;
865 
866 		suffix = strchr(za.za_name, '$');
867 
868 		if (suffix == NULL) {
869 			/*
870 			 * Skip local properties if we only want received
871 			 * properties.
872 			 */
873 			if (flags & DSL_PROP_GET_RECEIVED)
874 				continue;
875 
876 			propname = za.za_name;
877 			source = setpoint;
878 		} else if (strcmp(suffix, ZPROP_INHERIT_SUFFIX) == 0) {
879 			/* Skip explicitly inherited entries. */
880 			continue;
881 		} else if (strcmp(suffix, ZPROP_RECVD_SUFFIX) == 0) {
882 			if (flags & DSL_PROP_GET_LOCAL)
883 				continue;
884 
885 			(void) strncpy(buf, za.za_name, (suffix - za.za_name));
886 			buf[suffix - za.za_name] = '\0';
887 			propname = buf;
888 
889 			if (!(flags & DSL_PROP_GET_RECEIVED)) {
890 				/* Skip if locally overridden. */
891 				err = zap_contains(mos, propobj, propname);
892 				if (err == 0)
893 					continue;
894 				if (err != ENOENT)
895 					break;
896 
897 				/* Skip if explicitly inherited. */
898 				valstr = kmem_asprintf("%s%s", propname,
899 				    ZPROP_INHERIT_SUFFIX);
900 				err = zap_contains(mos, propobj, valstr);
901 				strfree(valstr);
902 				if (err == 0)
903 					continue;
904 				if (err != ENOENT)
905 					break;
906 			}
907 
908 			source = ((flags & DSL_PROP_GET_INHERITING) ?
909 			    setpoint : ZPROP_SOURCE_VAL_RECVD);
910 		} else {
911 			/*
912 			 * For backward compatibility, skip suffixes we don't
913 			 * recognize.
914 			 */
915 			continue;
916 		}
917 
918 		prop = zfs_name_to_prop(propname);
919 
920 		/* Skip non-inheritable properties. */
921 		if ((flags & DSL_PROP_GET_INHERITING) && prop != ZPROP_INVAL &&
922 		    !zfs_prop_inheritable(prop))
923 			continue;
924 
925 		/* Skip properties not valid for this type. */
926 		if ((flags & DSL_PROP_GET_SNAPSHOT) && prop != ZPROP_INVAL &&
927 		    !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
928 			continue;
929 
930 		/* Skip properties already defined. */
931 		if (nvlist_exists(nv, propname))
932 			continue;
933 
934 		VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
935 		if (za.za_integer_length == 1) {
936 			/*
937 			 * String property
938 			 */
939 			char *tmp = kmem_alloc(za.za_num_integers,
940 			    KM_SLEEP);
941 			err = zap_lookup(mos, propobj,
942 			    za.za_name, 1, za.za_num_integers, tmp);
943 			if (err != 0) {
944 				kmem_free(tmp, za.za_num_integers);
945 				break;
946 			}
947 			VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
948 			    tmp) == 0);
949 			kmem_free(tmp, za.za_num_integers);
950 		} else {
951 			/*
952 			 * Integer property
953 			 */
954 			ASSERT(za.za_integer_length == 8);
955 			(void) nvlist_add_uint64(propval, ZPROP_VALUE,
956 			    za.za_first_integer);
957 		}
958 
959 		VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, source) == 0);
960 		VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
961 		nvlist_free(propval);
962 	}
963 	zap_cursor_fini(&zc);
964 	if (err == ENOENT)
965 		err = 0;
966 	return (err);
967 }
968 
969 /*
970  * Iterate over all properties for this dataset and return them in an nvlist.
971  */
972 static int
973 dsl_prop_get_all_ds(dsl_dataset_t *ds, nvlist_t **nvp,
974     dsl_prop_getflags_t flags)
975 {
976 	dsl_dir_t *dd = ds->ds_dir;
977 	dsl_pool_t *dp = dd->dd_pool;
978 	objset_t *mos = dp->dp_meta_objset;
979 	int err = 0;
980 	char setpoint[MAXNAMELEN];
981 
982 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
983 
984 	if (ds->ds_is_snapshot)
985 		flags |= DSL_PROP_GET_SNAPSHOT;
986 
987 	ASSERT(dsl_pool_config_held(dp));
988 
989 	if (dsl_dataset_phys(ds)->ds_props_obj != 0) {
990 		ASSERT(flags & DSL_PROP_GET_SNAPSHOT);
991 		dsl_dataset_name(ds, setpoint);
992 		err = dsl_prop_get_all_impl(mos,
993 		    dsl_dataset_phys(ds)->ds_props_obj, setpoint, flags, *nvp);
994 		if (err)
995 			goto out;
996 	}
997 
998 	for (; dd != NULL; dd = dd->dd_parent) {
999 		if (dd != ds->ds_dir || (flags & DSL_PROP_GET_SNAPSHOT)) {
1000 			if (flags & (DSL_PROP_GET_LOCAL |
1001 			    DSL_PROP_GET_RECEIVED))
1002 				break;
1003 			flags |= DSL_PROP_GET_INHERITING;
1004 		}
1005 		dsl_dir_name(dd, setpoint);
1006 		err = dsl_prop_get_all_impl(mos,
1007 		    dsl_dir_phys(dd)->dd_props_zapobj, setpoint, flags, *nvp);
1008 		if (err)
1009 			break;
1010 	}
1011 out:
1012 	return (err);
1013 }
1014 
1015 boolean_t
1016 dsl_prop_get_hasrecvd(const char *dsname)
1017 {
1018 	uint64_t dummy;
1019 
1020 	return (0 ==
1021 	    dsl_prop_get_integer(dsname, ZPROP_HAS_RECVD, &dummy, NULL));
1022 }
1023 
1024 static int
1025 dsl_prop_set_hasrecvd_impl(const char *dsname, zprop_source_t source)
1026 {
1027 	uint64_t version;
1028 	spa_t *spa;
1029 	int error = 0;
1030 
1031 	VERIFY0(spa_open(dsname, &spa, FTAG));
1032 	version = spa_version(spa);
1033 	spa_close(spa, FTAG);
1034 
1035 	if (version >= SPA_VERSION_RECVD_PROPS)
1036 		error = dsl_prop_set_int(dsname, ZPROP_HAS_RECVD, source, 0);
1037 	return (error);
1038 }
1039 
1040 /*
1041  * Call after successfully receiving properties to ensure that only the first
1042  * receive on or after SPA_VERSION_RECVD_PROPS blows away local properties.
1043  */
1044 int
1045 dsl_prop_set_hasrecvd(const char *dsname)
1046 {
1047 	int error = 0;
1048 	if (!dsl_prop_get_hasrecvd(dsname))
1049 		error = dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_LOCAL);
1050 	return (error);
1051 }
1052 
1053 void
1054 dsl_prop_unset_hasrecvd(const char *dsname)
1055 {
1056 	VERIFY0(dsl_prop_set_hasrecvd_impl(dsname, ZPROP_SRC_NONE));
1057 }
1058 
1059 int
1060 dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
1061 {
1062 	return (dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, 0));
1063 }
1064 
1065 int
1066 dsl_prop_get_received(const char *dsname, nvlist_t **nvp)
1067 {
1068 	objset_t *os;
1069 	int error;
1070 
1071 	/*
1072 	 * Received properties are not distinguishable from local properties
1073 	 * until the dataset has received properties on or after
1074 	 * SPA_VERSION_RECVD_PROPS.
1075 	 */
1076 	dsl_prop_getflags_t flags = (dsl_prop_get_hasrecvd(dsname) ?
1077 	    DSL_PROP_GET_RECEIVED : DSL_PROP_GET_LOCAL);
1078 
1079 	error = dmu_objset_hold(dsname, FTAG, &os);
1080 	if (error != 0)
1081 		return (error);
1082 	error = dsl_prop_get_all_ds(os->os_dsl_dataset, nvp, flags);
1083 	dmu_objset_rele(os, FTAG);
1084 	return (error);
1085 }
1086 
1087 void
1088 dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
1089 {
1090 	nvlist_t *propval;
1091 	const char *propname = zfs_prop_to_name(prop);
1092 	uint64_t default_value;
1093 
1094 	if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
1095 		VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
1096 		return;
1097 	}
1098 
1099 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1100 	VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
1101 	/* Indicate the default source if we can. */
1102 	if (dodefault(propname, 8, 1, &default_value) == 0 &&
1103 	    value == default_value) {
1104 		VERIFY(nvlist_add_string(propval, ZPROP_SOURCE, "") == 0);
1105 	}
1106 	VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1107 	nvlist_free(propval);
1108 }
1109 
1110 void
1111 dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
1112 {
1113 	nvlist_t *propval;
1114 	const char *propname = zfs_prop_to_name(prop);
1115 
1116 	if (nvlist_lookup_nvlist(nv, propname, &propval) == 0) {
1117 		VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
1118 		return;
1119 	}
1120 
1121 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1122 	VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
1123 	VERIFY(nvlist_add_nvlist(nv, propname, propval) == 0);
1124 	nvlist_free(propval);
1125 }
1126