xref: /illumos-gate/usr/src/uts/common/fs/zfs/zcp_set.c (revision 528737823843346cf95a4a701612f82089135554)
1 /*
2  * CDDL HEADER START
3  *
4  * This file and its contents are supplied under the terms of the
5  * Common Development and Distribution License ("CDDL"), version 1.0.
6  * You may only use this file in accordance with the terms of version
7  * 1.0 of the CDDL.
8  *
9  * A full copy of the text of the CDDL should have accompanied this
10  * source.  A copy of the CDDL is also available via the Internet at
11  * http://www.illumos.org/license/CDDL.
12  *
13  * CDDL HEADER END
14  */
15 
16 /*
17  * Copyright (c) 2016 by Delphix. All rights reserved.
18  * Copyrigh 2020 Joyent, Inc.
19  */
20 
21 #include "lua.h"
22 #include "lualib.h"
23 #include "lauxlib.h"
24 
25 #include <sys/dsl_prop.h>
26 #include <sys/dsl_dir.h>
27 #include <sys/dsl_synctask.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/zcp.h>
30 #include <sys/zcp_set.h>
31 #include <sys/zcp_iter.h>
32 #include <sys/zcp_global.h>
33 #include <sys/zvol.h>
34 
35 #include <zfs_prop.h>
36 
37 static void
38 zcp_set_user_prop(lua_State *state, dsl_pool_t *dp, const char *dsname,
39     const char *prop_name, const char *prop_val, dmu_tx_t *tx)
40 {
41 	dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dsname, FTAG);
42 	if (ds == NULL)
43 		return; /* not reached; zcp_dataset_hold() longjmp'd */
44 
45 	nvlist_t *nvl = fnvlist_alloc();
46 	fnvlist_add_string(nvl, prop_name, prop_val);
47 
48 	dsl_props_set_sync_impl(ds, ZPROP_SRC_LOCAL, nvl, tx);
49 
50 	fnvlist_free(nvl);
51 	dsl_dataset_rele(ds, FTAG);
52 }
53 
54 int
55 zcp_set_prop_check(void *arg, dmu_tx_t *tx)
56 {
57 	zcp_set_prop_arg_t *args = arg;
58 	const char *prop_name = args->prop;
59 	dsl_props_set_arg_t dpsa = {
60 		.dpsa_dsname = args->dsname,
61 		.dpsa_source = ZPROP_SRC_LOCAL,
62 	};
63 	nvlist_t *nvl = NULL;
64 	int ret = 0;
65 
66 	/*
67 	 * Only user properties are currently supported. When non-user
68 	 * properties are supported, we will want to use
69 	 * zfs_valid_proplist() to verify the properties.
70 	 */
71 	if (!zfs_prop_user(prop_name)) {
72 		return (EINVAL);
73 	}
74 
75 	nvl = fnvlist_alloc();
76 	fnvlist_add_string(nvl, args->prop, args->val);
77 	dpsa.dpsa_props = nvl;
78 
79 	ret = dsl_props_set_check(&dpsa, tx);
80 	nvlist_free(nvl);
81 
82 	return (ret);
83 }
84 
85 void
86 zcp_set_prop_sync(void *arg, dmu_tx_t *tx)
87 {
88 	zcp_set_prop_arg_t *args = arg;
89 	zcp_run_info_t *ri = zcp_run_info(args->state);
90 	dsl_pool_t *dp = ri->zri_pool;
91 
92 	const char *dsname = args->dsname;
93 	const char *prop_name = args->prop;
94 	const char *prop_val = args->val;
95 
96 	if (zfs_prop_user(prop_name)) {
97 		zcp_set_user_prop(args->state, dp, dsname, prop_name,
98 		    prop_val, tx);
99 	}
100 }
101