summaryrefslogtreecommitdiffstats
path: root/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dsl_prop.c
blob: 2fff66d06b1ea7e1a489cfddd3ca599892382dec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/dmu.h>
#include <sys/dmu_objset.h>
#include <sys/dmu_tx.h>
#include <sys/dsl_dataset.h>
#include <sys/dsl_dir.h>
#include <sys/dsl_prop.h>
#include <sys/dsl_synctask.h>
#include <sys/spa.h>
#include <sys/zio_checksum.h> /* for the default checksum value */
#include <sys/zap.h>
#include <sys/fs/zfs.h>

#include "zfs_prop.h"

static int
dodefault(const char *propname, int intsz, int numint, void *buf)
{
	zfs_prop_t prop;

	if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL ||
	    zfs_prop_readonly(prop))
		return (ENOENT);

	if (zfs_prop_get_type(prop) == prop_type_string) {
		if (intsz != 1)
			return (EOVERFLOW);
		(void) strncpy(buf, zfs_prop_default_string(prop), numint);
	} else {
		if (intsz != 8 || numint < 1)
			return (EOVERFLOW);

		*(uint64_t *)buf = zfs_prop_default_numeric(prop);
	}

	return (0);
}

static int
dsl_prop_get_impl(dsl_dir_t *dd, const char *propname,
    int intsz, int numint, void *buf, char *setpoint)
{
	int err = ENOENT;
	zfs_prop_t prop;

	if (setpoint)
		setpoint[0] = '\0';

	prop = zfs_name_to_prop(propname);

	/*
	 * Note: dd may be NULL, therefore we shouldn't dereference it
	 * ouside this loop.
	 */
	for (; dd != NULL; dd = dd->dd_parent) {
		objset_t *mos = dd->dd_pool->dp_meta_objset;
		ASSERT(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
		err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj,
		    propname, intsz, numint, buf);
		if (err != ENOENT) {
			if (setpoint)
				dsl_dir_name(dd, setpoint);
			break;
		}

		/*
		 * Break out of this loop for non-inheritable properties.
		 */
		if (prop != ZFS_PROP_INVAL &&
		    !zfs_prop_inheritable(prop))
			break;
	}
	if (err == ENOENT)
		err = dodefault(propname, intsz, numint, buf);

	return (err);
}

/*
 * Register interest in the named property.  We'll call the callback
 * once to notify it of the current property value, and again each time
 * the property changes, until this callback is unregistered.
 *
 * Return 0 on success, errno if the prop is not an integer value.
 */
int
dsl_prop_register(dsl_dataset_t *ds, const char *propname,
    dsl_prop_changed_cb_t *callback, void *cbarg)
{
	dsl_dir_t *dd = ds->ds_dir;
	uint64_t value;
	dsl_prop_cb_record_t *cbr;
	int err;
	int need_rwlock;

	need_rwlock = !RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock);
	if (need_rwlock)
		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);

	err = dsl_prop_get_impl(dd, propname, 8, 1, &value, NULL);
	if (err != 0) {
		rw_exit(&dd->dd_pool->dp_config_rwlock);
		return (err);
	}

	cbr = kmem_alloc(sizeof (dsl_prop_cb_record_t), KM_SLEEP);
	cbr->cbr_ds = ds;
	cbr->cbr_propname = kmem_alloc(strlen(propname)+1, KM_SLEEP);
	(void) strcpy((char *)cbr->cbr_propname, propname);
	cbr->cbr_func = callback;
	cbr->cbr_arg = cbarg;
	mutex_enter(&dd->dd_lock);
	list_insert_head(&dd->dd_prop_cbs, cbr);
	mutex_exit(&dd->dd_lock);

	cbr->cbr_func(cbr->cbr_arg, value);

	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, dd->dd_object,
	    NULL, cbr, &dd));
	if (need_rwlock)
		rw_exit(&dd->dd_pool->dp_config_rwlock);
	/* Leave dataset open until this callback is unregistered */
	return (0);
}

int
dsl_prop_get_ds(dsl_dir_t *dd, const char *propname,
    int intsz, int numints, void *buf, char *setpoint)
{
	int err;

	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
	err = dsl_prop_get_impl(dd, propname, intsz, numints, buf, setpoint);
	rw_exit(&dd->dd_pool->dp_config_rwlock);

	return (err);
}

int
dsl_prop_get(const char *ddname, const char *propname,
    int intsz, int numints, void *buf, char *setpoint)
{
	dsl_dir_t *dd;
	const char *tail;
	int err;

	err = dsl_dir_open(ddname, FTAG, &dd, &tail);
	if (err)
		return (err);
	if (tail && tail[0] != '@') {
		dsl_dir_close(dd, FTAG);
		return (ENOENT);
	}

	err = dsl_prop_get_ds(dd, propname, intsz, numints, buf, setpoint);

	dsl_dir_close(dd, FTAG);
	return (err);
}

/*
 * Get the current property value.  It may have changed by the time this
 * function returns, so it is NOT safe to follow up with
 * dsl_prop_register() and assume that the value has not changed in
 * between.
 *
 * Return 0 on success, ENOENT if ddname is invalid.
 */
int
dsl_prop_get_integer(const char *ddname, const char *propname,
    uint64_t *valuep, char *setpoint)
{
	return (dsl_prop_get(ddname, propname, 8, 1, valuep, setpoint));
}

/*
 * Unregister this callback.  Return 0 on success, ENOENT if ddname is
 * invalid, ENOMSG if no matching callback registered.
 */
int
dsl_prop_unregister(dsl_dataset_t *ds, const char *propname,
    dsl_prop_changed_cb_t *callback, void *cbarg)
{
	dsl_dir_t *dd = ds->ds_dir;
	dsl_prop_cb_record_t *cbr;

	mutex_enter(&dd->dd_lock);
	for (cbr = list_head(&dd->dd_prop_cbs);
	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
		if (cbr->cbr_ds == ds &&
		    cbr->cbr_func == callback &&
		    cbr->cbr_arg == cbarg &&
		    strcmp(cbr->cbr_propname, propname) == 0)
			break;
	}

	if (cbr == NULL) {
		mutex_exit(&dd->dd_lock);
		return (ENOMSG);
	}

	list_remove(&dd->dd_prop_cbs, cbr);
	mutex_exit(&dd->dd_lock);
	kmem_free((void*)cbr->cbr_propname, strlen(cbr->cbr_propname)+1);
	kmem_free(cbr, sizeof (dsl_prop_cb_record_t));

	/* Clean up from dsl_prop_register */
	dsl_dir_close(dd, cbr);
	return (0);
}

/*
 * Return the number of callbacks that are registered for this dataset.
 */
int
dsl_prop_numcb(dsl_dataset_t *ds)
{
	dsl_dir_t *dd = ds->ds_dir;
	dsl_prop_cb_record_t *cbr;
	int num = 0;

	mutex_enter(&dd->dd_lock);
	for (cbr = list_head(&dd->dd_prop_cbs);
	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
		if (cbr->cbr_ds == ds)
			num++;
	}
	mutex_exit(&dd->dd_lock);

	return (num);
}

static void
dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
    const char *propname, uint64_t value, int first)
{
	dsl_dir_t *dd;
	dsl_prop_cb_record_t *cbr;
	objset_t *mos = dp->dp_meta_objset;
	zap_cursor_t zc;
	zap_attribute_t za;
	int err;

	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
	err = dsl_dir_open_obj(dp, ddobj, NULL, FTAG, &dd);
	if (err)
		return;

	if (!first) {
		/*
		 * If the prop is set here, then this change is not
		 * being inherited here or below; stop the recursion.
		 */
		err = zap_lookup(mos, dd->dd_phys->dd_props_zapobj, propname,
		    8, 1, &value);
		if (err == 0) {
			dsl_dir_close(dd, FTAG);
			return;
		}
		ASSERT3U(err, ==, ENOENT);
	}

	mutex_enter(&dd->dd_lock);
	for (cbr = list_head(&dd->dd_prop_cbs);
	    cbr; cbr = list_next(&dd->dd_prop_cbs, cbr)) {
		if (strcmp(cbr->cbr_propname, propname) == 0) {
			cbr->cbr_func(cbr->cbr_arg, value);
		}
	}
	mutex_exit(&dd->dd_lock);

	for (zap_cursor_init(&zc, mos,
	    dd->dd_phys->dd_child_dir_zapobj);
	    zap_cursor_retrieve(&zc, &za) == 0;
	    zap_cursor_advance(&zc)) {
		/* XXX recursion could blow stack; esp. za! */
		dsl_prop_changed_notify(dp, za.za_first_integer,
		    propname, value, FALSE);
	}
	zap_cursor_fini(&zc);
	dsl_dir_close(dd, FTAG);
}

struct prop_set_arg {
	const char *name;
	int intsz;
	int numints;
	const void *buf;
};


static void
dsl_prop_set_sync(void *arg1, void *arg2, dmu_tx_t *tx)
{
	dsl_dir_t *dd = arg1;
	struct prop_set_arg *psa = arg2;
	objset_t *mos = dd->dd_pool->dp_meta_objset;
	uint64_t zapobj = dd->dd_phys->dd_props_zapobj;
	uint64_t intval;
	int isint;

	isint = (dodefault(psa->name, 8, 1, &intval) == 0);

	if (psa->numints == 0) {
		int err = zap_remove(mos, zapobj, psa->name, tx);
		ASSERT(err == 0 || err == ENOENT);
		if (isint) {
			VERIFY(0 == dsl_prop_get_impl(dd->dd_parent,
			    psa->name, 8, 1, &intval, NULL));
		}
	} else {
		VERIFY(0 == zap_update(mos, zapobj, psa->name,
		    psa->intsz, psa->numints, psa->buf, tx));
		if (isint)
			intval = *(uint64_t *)psa->buf;
	}

	if (isint) {
		dsl_prop_changed_notify(dd->dd_pool,
		    dd->dd_object, psa->name, intval, TRUE);
	}
}

int
dsl_prop_set_dd(dsl_dir_t *dd, const char *propname,
    int intsz, int numints, const void *buf)
{
	struct prop_set_arg psa;

	psa.name = propname;
	psa.intsz = intsz;
	psa.numints = numints;
	psa.buf = buf;

	return (dsl_sync_task_do(dd->dd_pool,
	    NULL, dsl_prop_set_sync, dd, &psa, 2));
}

int
dsl_prop_set(const char *ddname, const char *propname,
    int intsz, int numints, const void *buf)
{
	dsl_dir_t *dd;
	int err;

	/*
	 * We must do these checks before we get to the syncfunc, since
	 * it can't fail.
	 */
	if (strlen(propname) >= ZAP_MAXNAMELEN)
		return (ENAMETOOLONG);
	if (intsz * numints >= ZAP_MAXVALUELEN)
		return (E2BIG);

	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
	if (err)
		return (err);
	err = dsl_prop_set_dd(dd, propname, intsz, numints, buf);
	dsl_dir_close(dd, FTAG);
	return (err);
}

/*
 * Iterate over all properties for this dataset and return them in an nvlist.
 */
int
dsl_prop_get_all(objset_t *os, nvlist_t **nvp)
{
	dsl_dataset_t *ds = os->os->os_dsl_dataset;
	dsl_dir_t *dd = ds->ds_dir;
	int err = 0;
	dsl_pool_t *dp;
	objset_t *mos;

	if (dsl_dataset_is_snapshot(ds)) {
		VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
		return (0);
	}

	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);

	dp = dd->dd_pool;
	mos = dp->dp_meta_objset;

	rw_enter(&dp->dp_config_rwlock, RW_READER);
	for (; dd != NULL; dd = dd->dd_parent) {
		char setpoint[MAXNAMELEN];
		zap_cursor_t zc;
		zap_attribute_t za;

		dsl_dir_name(dd, setpoint);

		for (zap_cursor_init(&zc, mos, dd->dd_phys->dd_props_zapobj);
		    (err = zap_cursor_retrieve(&zc, &za)) == 0;
		    zap_cursor_advance(&zc)) {
			nvlist_t *propval;
			zfs_prop_t prop;
			/*
			 * Skip non-inheritable properties.
			 */
			if ((prop = zfs_name_to_prop(za.za_name)) !=
			    ZFS_PROP_INVAL && !zfs_prop_inheritable(prop) &&
			    dd != ds->ds_dir)
				continue;

			if (nvlist_lookup_nvlist(*nvp, za.za_name,
			    &propval) == 0)
				continue;

			VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME,
			    KM_SLEEP) == 0);
			if (za.za_integer_length == 1) {
				/*
				 * String property
				 */
				char *tmp = kmem_alloc(za.za_num_integers,
				    KM_SLEEP);
				err = zap_lookup(mos,
				    dd->dd_phys->dd_props_zapobj,
				    za.za_name, 1, za.za_num_integers,
				    tmp);
				if (err != 0) {
					kmem_free(tmp, za.za_num_integers);
					break;
				}
				VERIFY(nvlist_add_string(propval,
				    ZFS_PROP_VALUE, tmp) == 0);
				kmem_free(tmp, za.za_num_integers);
			} else {
				/*
				 * Integer property
				 */
				ASSERT(za.za_integer_length == 8);
				(void) nvlist_add_uint64(propval,
				    ZFS_PROP_VALUE, za.za_first_integer);
			}

			VERIFY(nvlist_add_string(propval,
			    ZFS_PROP_SOURCE, setpoint) == 0);
			VERIFY(nvlist_add_nvlist(*nvp, za.za_name,
			    propval) == 0);
			nvlist_free(propval);
		}
		zap_cursor_fini(&zc);

		if (err != ENOENT)
			break;
		err = 0;
	}
	rw_exit(&dp->dp_config_rwlock);

	return (err);
}

void
dsl_prop_nvlist_add_uint64(nvlist_t *nv, zfs_prop_t prop, uint64_t value)
{
	nvlist_t *propval;

	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
	VERIFY(nvlist_add_uint64(propval, ZFS_PROP_VALUE, value) == 0);
	VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
	nvlist_free(propval);
}

void
dsl_prop_nvlist_add_string(nvlist_t *nv, zfs_prop_t prop, const char *value)
{
	nvlist_t *propval;

	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
	VERIFY(nvlist_add_string(propval, ZFS_PROP_VALUE, value) == 0);
	VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(prop), propval) == 0);
	nvlist_free(propval);
}
OpenPOWER on IntegriCloud