xref: /titanic_51/usr/src/uts/common/fs/zfs/dsl_prop.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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 2006 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 	if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL ||
48 	    zfs_prop_readonly(prop))
49 		return (ENOENT);
50 
51 	if (zfs_prop_get_type(prop) == prop_type_string) {
52 		if (intsz != 1)
53 			return (EOVERFLOW);
54 		(void) strncpy(buf, zfs_prop_default_string(prop), numint);
55 	} else {
56 		if (intsz != 8 || numint < 1)
57 			return (EOVERFLOW);
58 
59 		*(uint64_t *)buf = zfs_prop_default_numeric(prop);
60 	}
61 
62 	return (0);
63 }
64 
65 static int
66 dsl_prop_get_impl(dsl_dir_t *dd, const char *propname,
67     int intsz, int numint, void *buf, char *setpoint)
68 {
69 	int err = ENOENT;
70 	zfs_prop_t prop;
71 
72 	if (setpoint)
73 		setpoint[0] = '\0';
74 
75 	prop = zfs_name_to_prop(propname);
76 
77 	/*
78 	 * Note: dd may be NULL, therefore we shouldn't dereference it
79 	 * ouside this loop.
80 	 */
81 	for (; dd != NULL; dd = dd->dd_parent) {
82 		objset_t *mos = dd->dd_pool->dp_meta_objset;
83 		ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
84 		err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj,
85 		    propname, intsz, numint, buf);
86 		if (err != ENOENT) {
87 			if (setpoint)
88 				dsl_dir_name(dd, setpoint);
89 			break;
90 		}
91 
92 		/*
93 		 * Break out of this loop for non-inheritable properties.
94 		 */
95 		if (prop != ZFS_PROP_INVAL &&
96 		    !zfs_prop_inheritable(prop))
97 			break;
98 	}
99 	if (err == ENOENT)
100 		err = dodefault(propname, intsz, numint, buf);
101 
102 	return (err);
103 }
104 
105 /*
106  * Register interest in the named property.  We'll call the callback
107  * once to notify it of the current property value, and again each time
108  * the property changes, until this callback is unregistered.
109  *
110  * Return 0 on success, errno if the prop is not an integer value.
111  */
112 int
113 dsl_prop_register(dsl_dataset_t *ds, const char *propname,
114     dsl_prop_changed_cb_t *callback, void *cbarg)
115 {
116 	dsl_dir_t *dd = ds->ds_dir;
117 	uint64_t value;
118 	dsl_prop_cb_record_t *cbr;
119 	int err;
120 	int need_rwlock;
121 
122 	need_rwlock = !RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock);
123 	if (need_rwlock)
124 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
125 
126 	err = dsl_prop_get_impl(dd, propname, 8, 1, &value, NULL);
127 	if (err != 0) {
128 		rw_exit(&dd->dd_pool->dp_config_rwlock);
129 		return (err);
130 	}
131 
132 	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
133 	cbr->cbr_ds = ds;
134 	cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
135 	(void) strcpy((char *)cbr->cbr_propname, propname);
136 	cbr->cbr_func = callback;
137 	cbr->cbr_arg = cbarg;
138 	mutex_enter(&dd->dd_lock);
139 	list_insert_head(&dd->dd_prop_cbs, cbr);
140 	mutex_exit(&dd->dd_lock);
141 
142 	cbr->cbr_func(cbr->cbr_arg, value);
143 
144 	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, dd->dd_object,
145 	    NULL, cbr, &dd));
146 	if (need_rwlock)
147 		rw_exit(&dd->dd_pool->dp_config_rwlock);
148 	/* Leave dataset open until this callback is unregistered */
149 	return (0);
150 }
151 
152 int
153 dsl_prop_get_ds(dsl_dir_t *dd, const char *propname,
154     int intsz, int numints, void *buf, char *setpoint)
155 {
156 	int err;
157 
158 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
159 	err = dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint);
160 	rw_exit(&dd->dd_pool->dp_config_rwlock);
161 
162 	return (err);
163 }
164 
165 int
166 dsl_prop_get(const char *ddname, const char *propname,
167     int intsz, int numints, void *buf, char *setpoint)
168 {
169 	dsl_dir_t *dd;
170 	const char *tail;
171 	int err;
172 
173 	err = dsl_dir_open(ddname, FTAG, &dd, &tail);
174 	if (err)
175 		return (err);
176 	if (tail && tail[0] != '@') {
177 		dsl_dir_close(dd, FTAG);
178 		return (ENOENT);
179 	}
180 
181 	err = dsl_prop_get_ds(dd, propname, intsz, numints, buf, setpoint);
182 
183 	dsl_dir_close(dd, FTAG);
184 	return (err);
185 }
186 
187 /*
188  * Return 0 on success, ENOENT if ddname is invalid, EOVERFLOW if
189  * valuelen not big enough.
190  */
191 int
192 dsl_prop_get_string(const char *ddname, const char *propname,
193     char *value, int valuelen, char *setpoint)
194 {
195 	return (dsl_prop_get(ddname, propname, 1, valuelen, value, setpoint));
196 }
197 
198 /*
199  * Get the current property value.  It may have changed by the time this
200  * function returns, so it is NOT safe to follow up with
201  * dsl_prop_register() and assume that the value has not changed in
202  * between.
203  *
204  * Return 0 on success, ENOENT if ddname is invalid.
205  */
206 int
207 dsl_prop_get_integer(const char *ddname, const char *propname,
208     uint64_t *valuep, char *setpoint)
209 {
210 	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
211 }
212 
213 int
214 dsl_prop_get_ds_integer(dsl_dir_t *dd, const char *propname,
215     uint64_t *valuep, char *setpoint)
216 {
217 	return (dsl_prop_get_ds(dd, propname, 8, 1, valuep, setpoint));
218 }
219 
220 /*
221  * Unregister this callback.  Return 0 on success, ENOENT if ddname is
222  * invalid, ENOMSG if no matching callback registered.
223  */
224 int
225 dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
226     dsl_prop_changed_cb_t *callback, void *cbarg)
227 {
228 	dsl_dir_t *dd = ds->ds_dir;
229 	dsl_prop_cb_record_t *cbr;
230 
231 	mutex_enter(&dd->dd_lock);
232 	for (cbr = list_head(&dd->dd_prop_cbs);
233 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
234 		if (cbr->cbr_ds == ds &&
235 		    cbr->cbr_func == callback &&
236 		    cbr->cbr_arg == cbarg &&
237 		    strcmp(cbr->cbr_propname, propname) == 0)
238 			break;
239 	}
240 
241 	if (cbr == NULL) {
242 		mutex_exit(&dd->dd_lock);
243 		return (ENOMSG);
244 	}
245 
246 	list_remove(&dd->dd_prop_cbs, cbr);
247 	mutex_exit(&dd->dd_lock);
248 	kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
249 	kmem_free(cbr, sizeof (dsl_prop_cb_record_t));
250 
251 	/* Clean up from dsl_prop_register */
252 	dsl_dir_close(dd, cbr);
253 	return (0);
254 }
255 
256 /*
257  * Return the number of callbacks that are registered for this dataset.
258  */
259 int
260 dsl_prop_numcb(dsl_dataset_t *ds)
261 {
262 	dsl_dir_t *dd = ds->ds_dir;
263 	dsl_prop_cb_record_t *cbr;
264 	int num = 0;
265 
266 	mutex_enter(&dd->dd_lock);
267 	for (cbr = list_head(&dd->dd_prop_cbs);
268 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
269 		if (cbr->cbr_ds == ds)
270 			num++;
271 	}
272 	mutex_exit(&dd->dd_lock);
273 
274 	return (num);
275 }
276 
277 static void
278 dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
279     const char *propname, uint64_t value, int first)
280 {
281 	dsl_dir_t *dd;
282 	dsl_prop_cb_record_t *cbr;
283 	objset_t *mos = dp->dp_meta_objset;
284 	zap_cursor_t zc;
285 	zap_attribute_t za;
286 	int err;
287 
288 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
289 	err = dsl_dir_open_obj(dp, ddobj, NULL, FTAG, &dd);
290 	if (err)
291 		return;
292 
293 	if (!first) {
294 		/*
295 		 * If the prop is set here, then this change is not
296 		 * being inherited here or below; stop the recursion.
297 		 */
298 		err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname,
299 		    8, 1, &value);
300 		if (err == 0) {
301 			dsl_dir_close(dd, FTAG);
302 			return;
303 		}
304 		ASSERT3U(err, ==, ENOENT);
305 	}
306 
307 	mutex_enter(&dd->dd_lock);
308 	for (cbr = list_head(&dd->dd_prop_cbs);
309 	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
310 		if (strcmp(cbr->cbr_propname, propname) == 0) {
311 			cbr->cbr_func(cbr->cbr_arg, value);
312 		}
313 	}
314 	mutex_exit(&dd->dd_lock);
315 
316 	for (zap_cursor_init(&zc, mos,
317 	    dd->dd_phys->dd_child_dir_zapobj);
318 	    zap_cursor_retrieve(&zc, &za) == 0;
319 	    zap_cursor_advance(&zc)) {
320 		/* XXX recursion could blow stack; esp. za! */
321 		dsl_prop_changed_notify(dp, za.za_first_integer,
322 		    propname, value, FALSE);
323 	}
324 	zap_cursor_fini(&zc);
325 	dsl_dir_close(dd, FTAG);
326 }
327 
328 struct prop_set_arg {
329 	const char *name;
330 	int intsz;
331 	int numints;
332 	const void *buf;
333 };
334 
335 
336 static void
337 dsl_prop_set_sync(void *arg1, void *arg2, dmu_tx_t *tx)
338 {
339 	dsl_dir_t *dd = arg1;
340 	struct prop_set_arg *psa = arg2;
341 	objset_t *mos = dd->dd_pool->dp_meta_objset;
342 	uint64_t zapobj = dd->dd_phys->dd_props_zapobj;
343 	uint64_t intval;
344 	int isint;
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 }
367 
368 int
369 dsl_prop_set(const char *ddname, const char *propname,
370     int intsz, int numints, const void *buf)
371 {
372 	dsl_dir_t *dd;
373 	int err;
374 	struct prop_set_arg psa;
375 
376 	/*
377 	 * We must do these checks before we get to the syncfunc, since
378 	 * it can't fail.
379 	 */
380 	if (strlen(propname) >= ZAP_MAXNAMELEN)
381 		return (ENAMETOOLONG);
382 	if (intsz * numints >= ZAP_MAXVALUELEN)
383 		return (E2BIG);
384 
385 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
386 	if (err)
387 		return (err);
388 
389 	psa.name = propname;
390 	psa.intsz = intsz;
391 	psa.numints = numints;
392 	psa.buf = buf;
393 	err = dsl_sync_task_do(dd->dd_pool,
394 	    NULL, dsl_prop_set_sync, dd, &psa, 2);
395 
396 	dsl_dir_close(dd, FTAG);
397 
398 	return (err);
399 }
400 
401 /*
402  * Iterate over all properties for this dataset and return them in an nvlist.
403  */
404 int
405 dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
406 {
407 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
408 	dsl_dir_t *dd = ds->ds_dir;
409 	int err = 0;
410 	dsl_pool_t *dp;
411 	objset_t *mos;
412 	zap_cursor_t zc;
413 	zap_attribute_t za;
414 	char setpoint[MAXNAMELEN];
415 	char *tmp;
416 	nvlist_t *propval;
417 	zfs_prop_t prop;
418 
419 	if (dsl_dataset_is_snapshot(ds)) {
420 		VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
421 		return (0);
422 	}
423 
424 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
425 
426 	dp = dd->dd_pool;
427 	mos = dp->dp_meta_objset;
428 
429 	rw_enter(&dp->dp_config_rwlock, RW_READER);
430 	for (; dd != NULL; dd = dd->dd_parent) {
431 		dsl_dir_name(dd, setpoint);
432 
433 		for (zap_cursor_init(&zc, mos, dd->dd_phys->dd_props_zapobj);
434 		    (err = zap_cursor_retrieve(&zc, &za)) == 0;
435 		    zap_cursor_advance(&zc)) {
436 			/*
437 			 * Skip non-inheritable properties.
438 			 */
439 			if ((prop = zfs_name_to_prop(za.za_name)) !=
440 			    ZFS_PROP_INVAL && !zfs_prop_inheritable(prop) &&
441 			    dd != ds->ds_dir)
442 				continue;
443 
444 			if (nvlist_lookup_nvlist(*nvp, za.za_name,
445 			    &propval) == 0)
446 				continue;
447 
448 			VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME,
449 			    KM_SLEEP) == 0);
450 			if (za.za_integer_length == 1) {
451 				/*
452 				 * String property
453 				 */
454 				tmp = kmem_alloc(za.za_num_integers, KM_SLEEP);
455 				err = zap_lookup(mos,
456 				    dd->dd_phys->dd_props_zapobj,
457 				    za.za_name, 1, za.za_num_integers,
458 				    tmp);
459 				if (err != 0) {
460 					kmem_free(tmp, za.za_num_integers);
461 					break;
462 				}
463 				VERIFY(nvlist_add_string(propval,
464 				    ZFS_PROP_VALUE, tmp) == 0);
465 				kmem_free(tmp, za.za_num_integers);
466 			} else {
467 				/*
468 				 * Integer property
469 				 */
470 				ASSERT(za.za_integer_length == 8);
471 				(void) nvlist_add_uint64(propval,
472 				    ZFS_PROP_VALUE, za.za_first_integer);
473 			}
474 
475 			VERIFY(nvlist_add_string(propval,
476 			    ZFS_PROP_SOURCE, setpoint) == 0);
477 			VERIFY(nvlist_add_nvlist(*nvp, za.za_name,
478 			    propval) == 0);
479 			nvlist_free(propval);
480 		}
481 		zap_cursor_fini(&zc);
482 
483 		if (err != ENOENT)
484 			break;
485 		err = 0;
486 	}
487 	rw_exit(&dp->dp_config_rwlock);
488 
489 	return (err);
490 }
491