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