xref: /freebsd/sys/contrib/openzfs/include/sys/zcp.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
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, 2018 by Delphix. All rights reserved.
15  */
16 
17 #ifndef _SYS_ZCP_H
18 #define	_SYS_ZCP_H
19 
20 #include <sys/dmu_tx.h>
21 #include <sys/dsl_pool.h>
22 
23 #include <sys/lua/lua.h>
24 #include <sys/lua/lualib.h>
25 #include <sys/lua/lauxlib.h>
26 
27 #ifdef	__cplusplus
28 extern "C" {
29 #endif
30 
31 #define	ZCP_RUN_INFO_KEY "runinfo"
32 
33 extern uint64_t zfs_lua_max_instrlimit;
34 extern uint64_t zfs_lua_max_memlimit;
35 
36 int zcp_argerror(lua_State *, int, const char *, ...);
37 
38 int zcp_eval(const char *, const char *, boolean_t, uint64_t, uint64_t,
39     nvpair_t *, nvlist_t *);
40 
41 int zcp_load_list_lib(lua_State *);
42 
43 int zcp_load_synctask_lib(lua_State *, boolean_t);
44 
45 typedef void (zcp_cleanup_t)(void *);
46 typedef struct zcp_cleanup_handler {
47 	zcp_cleanup_t *zch_cleanup_func;
48 	void *zch_cleanup_arg;
49 	list_node_t zch_node;
50 } zcp_cleanup_handler_t;
51 
52 typedef struct zcp_alloc_arg {
53 	boolean_t	aa_must_succeed;
54 	int64_t		aa_alloc_remaining;
55 	int64_t		aa_alloc_limit;
56 } zcp_alloc_arg_t;
57 
58 typedef struct zcp_run_info {
59 	dsl_pool_t	*zri_pool;
60 
61 	/*
62 	 * An estimate of the total amount of space consumed by all
63 	 * synctasks we have successfully performed so far in this
64 	 * channel program. Used to generate ENOSPC errors for syncfuncs.
65 	 */
66 	int		zri_space_used;
67 
68 	/*
69 	 * The credentials of the thread which originally invoked the channel
70 	 * program. Since channel programs are always invoked from the synctask
71 	 * thread they should always do permissions checks against this cred
72 	 * rather than the 'current' thread's.
73 	 */
74 	cred_t		*zri_cred;
75 
76 	/*
77 	 * The tx in which this channel program is running.
78 	 */
79 	dmu_tx_t	*zri_tx;
80 
81 	/*
82 	 * The maximum number of Lua instructions the channel program is allowed
83 	 * to execute. If it takes longer than this it will time out. A value
84 	 * of 0 indicates no instruction limit.
85 	 */
86 	uint64_t	zri_maxinstrs;
87 
88 	/*
89 	 * The number of Lua instructions the channel program has executed.
90 	 */
91 	uint64_t	zri_curinstrs;
92 
93 	/*
94 	 * Boolean indicating whether or not the channel program exited
95 	 * because it timed out.
96 	 */
97 	boolean_t	zri_timed_out;
98 
99 	/*
100 	 * Channel program was canceled by user
101 	 */
102 	boolean_t	zri_canceled;
103 
104 	/*
105 	 * Boolean indicating whether or not we are running in syncing
106 	 * context.
107 	 */
108 	boolean_t	zri_sync;
109 
110 	/*
111 	 * List of currently registered cleanup handlers, which will be
112 	 * triggered in the event of a fatal error.
113 	 */
114 	list_t		zri_cleanup_handlers;
115 
116 	/*
117 	 * The Lua state context of our channel program.
118 	 */
119 	lua_State	*zri_state;
120 
121 	/*
122 	 * Lua memory allocator arguments.
123 	 */
124 	zcp_alloc_arg_t	*zri_allocargs;
125 
126 	/*
127 	 * Contains output values from zcp script or error string.
128 	 */
129 	nvlist_t	*zri_outnvl;
130 
131 	/*
132 	 * The keys of this nvlist are datasets which may be zvols and may need
133 	 * to have device minor nodes created.  This information is passed from
134 	 * syncing context (where the zvol is created) to open context (where we
135 	 * create the minor nodes).
136 	 */
137 	nvlist_t	*zri_new_zvols;
138 
139 	/*
140 	 * The errno number returned to caller of zcp_eval().
141 	 */
142 	int		zri_result;
143 } zcp_run_info_t;
144 
145 zcp_run_info_t *zcp_run_info(lua_State *);
146 zcp_cleanup_handler_t *zcp_register_cleanup(lua_State *, zcp_cleanup_t, void *);
147 void zcp_deregister_cleanup(lua_State *, zcp_cleanup_handler_t *);
148 void zcp_cleanup(lua_State *);
149 
150 /*
151  * Argument parsing routines for channel program callback functions.
152  */
153 typedef struct zcp_arg {
154 	/*
155 	 * The name of this argument. For keyword arguments this is the name
156 	 * functions will use to set the argument. For positional arguments
157 	 * the name has no programmatic meaning, but will appear in error
158 	 * messages and help output.
159 	 */
160 	const char *za_name;
161 
162 	/*
163 	 * The Lua type this argument should have (e.g. LUA_TSTRING,
164 	 * LUA_TBOOLEAN) see the lua_type() function documentation for a
165 	 * complete list. Calling a function with an argument that does
166 	 * not match the expected type will result in the program terminating.
167 	 */
168 	const int za_lua_type;
169 } zcp_arg_t;
170 
171 void zcp_parse_args(lua_State *, const char *, const zcp_arg_t *,
172     const zcp_arg_t *);
173 int zcp_nvlist_to_lua(lua_State *, nvlist_t *, char *, int);
174 int zcp_dataset_hold_error(lua_State *, dsl_pool_t *, const char *, int);
175 struct dsl_dataset *zcp_dataset_hold(lua_State *, dsl_pool_t *,
176     const char *, const void *);
177 
178 typedef int (zcp_lib_func_t)(lua_State *);
179 typedef struct zcp_lib_info {
180 	const char *name;
181 	zcp_lib_func_t *func;
182 	const zcp_arg_t pargs[4];
183 	const zcp_arg_t kwargs[2];
184 } zcp_lib_info_t;
185 
186 #ifdef	__cplusplus
187 }
188 #endif
189 
190 #endif	/* _SYS_ZCP_H */
191