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