xref: /titanic_51/usr/src/uts/common/fs/zfs/dsl_prop.c (revision ac19272f7eb4a433cfccf2fdccc769cca5528169)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
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/zio_checksum.h> /* for the default checksum value */
37 #include <sys/zap.h>
38 #include <sys/fs/zfs.h>
39 
40 #include "zfs_prop.h"
41 
42 static int
43 dodefault(const char *propname, int intsz, int numint, void *buf)
44 {
45 	zfs_prop_t prop;
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 = zfs_name_to_prop(propname)) == ZPROP_INVAL ||
53 	    (zfs_prop_readonly(prop) && !zfs_prop_setonce(prop)))
54 		return (ENOENT);
55 
56 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
57 		if (intsz != 1)
58 			return (EOVERFLOW);
59 		(void) strncpy(buf, zfs_prop_default_string(prop),
60 		    numint);
61 	} else {
62 		if (intsz != 8 || numint < 1)
63 			return (EOVERFLOW);
64 
65 		*(uint64_t *)buf = zfs_prop_default_numeric(prop);
66 	}
67 
68 	return (0);
69 }
70 
71 static int
72 dsl_prop_get_impl(dsl_dir_t *dd, const char *propname,
73     int intsz, int numint, void *buf, char *setpoint)
74 {
75 	int err = ENOENT;
76 	zfs_prop_t prop;
77 
78 	if (setpoint)
79 		setpoint[0] = '\0';
80 
81 	prop = zfs_name_to_prop(propname);
82 
83 	/*
84 	 * Note: dd may be NULL, therefore we shouldn't dereference it
85 	 * ouside this loop.
86 	 */
87 	for (; dd != NULL; dd = dd->dd_parent) {
88 		objset_t *mos = dd->dd_pool->dp_meta_objset;
89 		ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
90 		err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj,
91 		    propname, intsz, numint, buf);
92 		if (err != ENOENT) {
93 			if (setpoint)
94 				dsl_dir_name(dd, setpoint);
95 			break;
96 		}
97 
98 		/*
99 		 * Break out of this loop for non-inheritable properties.
100 		 */
101 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
102 			break;
103 	}
104 	if (err == ENOENT)
105 		err = dodefault(propname, intsz, numint, buf);
106 
107 	return (err);
108 }
109 
110 /*
111  * Register interest in the named property.  We'll call the callback
112  * once to notify it of the current property value, and again each time
113  * the property changes, until this callback is unregistered.
114  *
115  * Return 0 on success, errno if the prop is not an integer value.
116  */
117 int
118 dsl_prop_register(dsl_dataset_t *ds, const char *propname,
119     dsl_prop_changed_cb_t *callback, void *cbarg)
120 {
121 	dsl_dir_t *dd = ds->ds_dir;
122 	uint64_t value;
123 	dsl_prop_cb_record_t *cbr;
124 	int err;
125 	int need_rwlock;
126 
127 	need_rwlock = !RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock);
128 	if (need_rwlock)
129 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
130 
131 	err = dsl_prop_get_impl(dd, propname, 8, 1, &value, NULL);
132 	if (err != 0) {
133 		rw_exit(&dd->dd_pool->dp_config_rwlock);
134 		return (err);
135 	}
136 
137 	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
138 	cbr->cbr_ds = ds;
139 	cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
140 	(void) strcpy((char *)cbr->cbr_propname, propname);
141 	cbr->cbr_func = callback;
142 	cbr->cbr_arg = cbarg;
143 	mutex_enter(&dd->dd_lock);
144 	list_insert_head(&dd->dd_prop_cbs, cbr);
145 	mutex_exit(&dd->dd_lock);
146 
147 	cbr->cbr_func(cbr->cbr_arg, value);
148 
149 	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, dd->dd_object,
150 	    NULL, cbr, &dd));
151 	if (need_rwlock)
152 		rw_exit(&dd->dd_pool->dp_config_rwlock);
153 	/* Leave dataset open until this callback is unregistered */
154 	return (0);
155 }
156 
157 int
158 dsl_prop_get_ds(dsl_dir_t *dd, const char *propname,
159     int intsz, int numints, void *buf, char *setpoint)
160 {
161 	int err;
162 
163 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
164 	err = dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint);
165 	rw_exit(&dd->dd_pool->dp_config_rwlock);
166 
167 	return (err);
168 }
169 
170 /*
171  * Get property when config lock is already held.
172  */
173 int dsl_prop_get_ds_locked(dsl_dir_t *dd, const char *propname,
174     int intsz, int numints, void *buf, char *setpoint)
175 {
176 	ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
177 	return (dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint));
178 }
179 
180 int
181 dsl_prop_get(const char *ddname, const char *propname,
182     int intsz, int numints, void *buf, char *setpoint)
183 {
184 	dsl_dir_t *dd;
185 	const char *tail;
186 	int err;
187 
188 	err = dsl_dir_open(ddname, FTAG, &dd, &tail);
189 	if (err)
190 		return (err);
191 	if (tail && tail[0] != '@') {
192 		dsl_dir_close(dd, FTAG);
193 		return (ENOENT);
194 	}
195 
196 	err = dsl_prop_get_ds(dd, propname, intsz, numints, buf, setpoint);
197 
198 	dsl_dir_close(dd, FTAG);
199 	return (err);
200 }
201 
202 /*
203  * Get the current property value.  It may have changed by the time this
204  * function returns, so it is NOT safe to follow up with
205  * dsl_prop_register() and assume that the value has not changed in
206  * between.
207  *
208  * Return 0 on success, ENOENT if ddname is invalid.
209  */
210 int
211 dsl_prop_get_integer(const char *ddname, const char *propname,
212     uint64_t *valuep, char *setpoint)
213 {
214 	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
215 }
216 
217 /*
218  * Unregister this callback.  Return 0 on success, ENOENT if ddname is
219  * invalid, ENOMSG if no matching callback registered.
220  */
221 int
222 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
223     dsl_prop_changed_cb_t *callback, void *cbarg)
224 {
225 	dsl_dir_t *dd = ds->ds_dir;
226 	dsl_prop_cb_record_t *cbr;
227 
228 	mutex_enter(&dd->dd_lock);
229 	for (cbr = list_head(&dd->dd_prop_cbs);
230 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
231 		if (cbr->cbr_ds == ds &&
232 		    cbr->cbr_func == callback &&
233 		    cbr->cbr_arg == cbarg &&
234 		    strcmp(cbr->cbr_propname, propname) == 0)
235 			break;
236 	}
237 
238 	if (cbr == NULL) {
239 		mutex_exit(&dd->dd_lock);
240 		return (ENOMSG);
241 	}
242 
243 	list_remove(&dd->dd_prop_cbs, cbr);
244 	mutex_exit(&dd->dd_lock);
245 	kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
246 	kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
247 
248 	/* Clean up from dsl_prop_register */
249 	dsl_dir_close(dd, cbr);
250 	return (0);
251 }
252 
253 /*
254  * Return the number of callbacks that are registered for this dataset.
255  */
256 int
257 dsl_prop_numcb(dsl_dataset_t *ds)
258 {
259 	dsl_dir_t *dd = ds->ds_dir;
260 	dsl_prop_cb_record_t *cbr;
261 	int num = 0;
262 
263 	mutex_enter(&dd->dd_lock);
264 	for (cbr = list_head(&dd->dd_prop_cbs);
265 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
266 		if (cbr->cbr_ds == ds)
267 			num++;
268 	}
269 	mutex_exit(&dd->dd_lock);
270 
271 	return (num);
272 }
273 
274 static void
275 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
276     const char *propname, uint64_t value, int first)
277 {
278 	dsl_dir_t *dd;
279 	dsl_prop_cb_record_t *cbr;
280 	objset_t *mos = dp->dp_meta_objset;
281 	zap_cursor_t zc;
282 	zap_attribute_t za;
283 	int err;
284 
285 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
286 	err = dsl_dir_open_obj(dp, ddobj, NULL, FTAG, &dd);
287 	if (err)
288 		return;
289 
290 	if (!first) {
291 		/*
292 		 * If the prop is set here, then this change is not
293 		 * being inherited here or below; stop the recursion.
294 		 */
295 		err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname,
296 		    8, 1, &value);
297 		if (err == 0) {
298 			dsl_dir_close(dd, FTAG);
299 			return;
300 		}
301 		ASSERT3U(err, ==, ENOENT);
302 	}
303 
304 	mutex_enter(&dd->dd_lock);
305 	for (cbr = list_head(&dd->dd_prop_cbs);
306 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
307 		if (strcmp(cbr->cbr_propname, propname) == 0) {
308 			cbr->cbr_func(cbr->cbr_arg, value);
309 		}
310 	}
311 	mutex_exit(&dd->dd_lock);
312 
313 	for (zap_cursor_init(&zc, mos,
314 	    dd->dd_phys->dd_child_dir_zapobj);
315 	    zap_cursor_retrieve(&zc, &za) == 0;
316 	    zap_cursor_advance(&zc)) {
317 		/* XXX recursion could blow stack; esp. za! */
318 		dsl_prop_changed_notify(dp, za.za_first_integer,
319 		    propname, value, FALSE);
320 	}
321 	zap_cursor_fini(&zc);
322 	dsl_dir_close(dd, FTAG);
323 }
324 
325 struct prop_set_arg {
326 	const char *name;
327 	int intsz;
328 	int numints;
329 	const void *buf;
330 };
331 
332 
333 static void
334 dsl_prop_set_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
335 {
336 	dsl_dir_t *dd = arg1;
337 	struct prop_set_arg *psa = arg2;
338 	objset_t *mos = dd->dd_pool->dp_meta_objset;
339 	uint64_t zapobj = dd->dd_phys->dd_props_zapobj;
340 	uint64_t intval;
341 	int isint;
342 	char valbuf[32];
343 	char *valstr;
344 
345 	isint = (dodefault(psa->name, 8, 1, &intval) == 0);
346 
347 	if (psa->numints == 0) {
348 		int err = zap_remove(mos, zapobj, psa->name, tx);
349 		ASSERT(err == 0 || err == ENOENT);
350 		if (isint) {
351 			VERIFY(0 == dsl_prop_get_impl(dd->dd_parent,
352 			    psa->name, 8, 1, &intval, NULL));
353 		}
354 	} else {
355 		VERIFY(0 == zap_update(mos, zapobj, psa->name,
356 		    psa->intsz, psa->numints, psa->buf, tx));
357 		if (isint)
358 			intval = *(uint64_t *)psa->buf;
359 	}
360 
361 	if (isint) {
362 		dsl_prop_changed_notify(dd->dd_pool,
363 		    dd->dd_object, psa->name, intval, TRUE);
364 	}
365 	if (isint) {
366 		(void) snprintf(valbuf, sizeof (valbuf),
367 		    "%lld", (longlong_t)intval);
368 		valstr = valbuf;
369 	} else {
370 		valstr = (char *)psa->buf;
371 	}
372 	spa_history_internal_log((psa->numints == 0) ? LOG_DS_INHERIT :
373 	    LOG_DS_PROPSET, dd->dd_pool->dp_spa, tx, cr,
374 	    "%s=%s dataset = %llu", psa->name, valstr,
375 	    dd->dd_phys->dd_head_dataset_obj);
376 }
377 
378 void
379 dsl_prop_set_uint64_sync(dsl_dir_t *dd, const char *name, uint64_t val,
380     cred_t *cr, dmu_tx_t *tx)
381 {
382 	objset_t *mos = dd->dd_pool->dp_meta_objset;
383 	uint64_t zapobj = dd->dd_phys->dd_props_zapobj;
384 
385 	ASSERT(dmu_tx_is_syncing(tx));
386 
387 	VERIFY(0 == zap_update(mos, zapobj, name, sizeof (val), 1, &val, tx));
388 
389 	dsl_prop_changed_notify(dd->dd_pool, dd->dd_object, name, val, TRUE);
390 
391 	spa_history_internal_log(LOG_DS_PROPSET, dd->dd_pool->dp_spa, tx, cr,
392 	    "%s=%llu dataset = %llu", name, (u_longlong_t)val,
393 	    dd->dd_phys->dd_head_dataset_obj);
394 }
395 
396 int
397 dsl_prop_set_dd(dsl_dir_t *dd, const char *propname,
398     int intsz, int numints, const void *buf)
399 {
400 	struct prop_set_arg psa;
401 
402 	psa.name = propname;
403 	psa.intsz = intsz;
404 	psa.numints = numints;
405 	psa.buf = buf;
406 
407 	return (dsl_sync_task_do(dd->dd_pool,
408 	    NULL, dsl_prop_set_sync, dd, &psa, 2));
409 }
410 
411 int
412 dsl_prop_set(const char *ddname, const char *propname,
413     int intsz, int numints, const void *buf)
414 {
415 	dsl_dir_t *dd;
416 	int err;
417 
418 	/*
419 	 * We must do these checks before we get to the syncfunc, since
420 	 * it can't fail.
421 	 */
422 	if (strlen(propname) >= ZAP_MAXNAMELEN)
423 		return (ENAMETOOLONG);
424 	if (intsz * numints >= ZAP_MAXVALUELEN)
425 		return (E2BIG);
426 
427 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
428 	if (err)
429 		return (err);
430 	err = dsl_prop_set_dd(dd, propname, intsz, numints, buf);
431 	dsl_dir_close(dd, FTAG);
432 	return (err);
433 }
434 
435 /*
436  * Iterate over all properties for this dataset and return them in an nvlist.
437  */
438 int
439 dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
440 {
441 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
442 	dsl_dir_t *dd = ds->ds_dir;
443 	boolean_t snapshot;
444 	int err = 0;
445 	dsl_pool_t *dp;
446 	objset_t *mos;
447 
448 	snapshot = dsl_dataset_is_snapshot(ds);
449 
450 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
451 
452 	dp = dd->dd_pool;
453 	mos = dp->dp_meta_objset;
454 
455 	rw_enter(&dp->dp_config_rwlock, RW_READER);
456 	for (; dd != NULL; dd = dd->dd_parent) {
457 		char setpoint[MAXNAMELEN];
458 		zap_cursor_t zc;
459 		zap_attribute_t za;
460 
461 		dsl_dir_name(dd, setpoint);
462 
463 		for (zap_cursor_init(&zc, mos, dd->dd_phys->dd_props_zapobj);
464 		    (err = zap_cursor_retrieve(&zc, &za)) == 0;
465 		    zap_cursor_advance(&zc)) {
466 			nvlist_t *propval;
467 			zfs_prop_t prop;
468 			/*
469 			 * Skip non-inheritable properties.
470 			 */
471 			if ((prop = zfs_name_to_prop(za.za_name)) !=
472 			    ZPROP_INVAL && !zfs_prop_inheritable(prop) &&
473 			    dd != ds->ds_dir)
474 				continue;
475 
476 			if (snapshot &&
477 			    !zfs_prop_valid_for_type(prop, ZFS_TYPE_SNAPSHOT))
478 				continue;
479 
480 			if (nvlist_lookup_nvlist(*nvp, za.za_name,
481 			    &propval) == 0)
482 				continue;
483 
484 			VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME,
485 			    KM_SLEEP) == 0);
486 			if (za.za_integer_length == 1) {
487 				/*
488 				 * String property
489 				 */
490 				char *tmp = kmem_alloc(za.za_num_integers,
491 				    KM_SLEEP);
492 				err = zap_lookup(mos,
493 				    dd->dd_phys->dd_props_zapobj,
494 				    za.za_name, 1, za.za_num_integers,
495 				    tmp);
496 				if (err != 0) {
497 					kmem_free(tmp, za.za_num_integers);
498 					break;
499 				}
500 				VERIFY(nvlist_add_string(propval, ZPROP_VALUE,
501 				    tmp) == 0);
502 				kmem_free(tmp, za.za_num_integers);
503 			} else {
504 				/*
505 				 * Integer property
506 				 */
507 				ASSERT(za.za_integer_length == 8);
508 				(void) nvlist_add_uint64(propval, ZPROP_VALUE,
509 				    za.za_first_integer);
510 			}
511 
512 			VERIFY(nvlist_add_string(propval, ZPROP_SOURCE,
513 			    setpoint) == 0);
514 			VERIFY(nvlist_add_nvlist(*nvp, za.za_name,
515 			    propval) == 0);
516 			nvlist_free(propval);
517 		}
518 		zap_cursor_fini(&zc);
519 
520 		if (err != ENOENT)
521 			break;
522 		err = 0;
523 	}
524 	rw_exit(&dp->dp_config_rwlock);
525 
526 	return (err);
527 }
528 
529 void
530 dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
531 {
532 	nvlist_t *propval;
533 
534 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
535 	VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, value) == 0);
536 	VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
537 	nvlist_free(propval);
538 }
539 
540 void
541 dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
542 {
543 	nvlist_t *propval;
544 
545 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
546 	VERIFY(nvlist_add_string(propval, ZPROP_VALUE, value) == 0);
547 	VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
548 	nvlist_free(propval);
549 }
550