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