xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zcp.h (revision 5f82aa32fbc5dc2c59bca6ff315f44a4c4c9ea86)
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  */
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 "lua.h"
27 #include "lualib.h"
28 #include "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 *, uint64_t, uint64_t, nvpair_t *,
42     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 
50 typedef struct zcp_run_info {
51 	dsl_pool_t	*zri_pool;
52 
53 	/*
54 	 * An estimate of the total ammount of space consumed by all
55 	 * synctasks we have successfully performed so far in this
56 	 * channel program. Used to generate ENOSPC errors for syncfuncs.
57 	 */
58 	int		zri_space_used;
59 
60 	/*
61 	 * The credentials of the thread which originally invoked the channel
62 	 * program. Since channel programs are always invoked from the synctask
63 	 * thread they should always do permissions checks against this cred
64 	 * rather than the 'current' thread's.
65 	 */
66 	cred_t		*zri_cred;
67 
68 	/*
69 	 * The tx in which this channel program is running.
70 	 */
71 	dmu_tx_t	*zri_tx;
72 
73 	/*
74 	 * The maximum number of Lua instructions the channel program is allowed
75 	 * to execute. If it takes longer than this it will time out. A value
76 	 * of 0 indicates no instruction limit.
77 	 */
78 	uint64_t	zri_maxinstrs;
79 
80 	/*
81 	 * The number of Lua instructions the channel program has executed.
82 	 */
83 	uint64_t	zri_curinstrs;
84 
85 	/*
86 	 * Boolean indicating whether or not the channel program exited
87 	 * because it timed out.
88 	 */
89 	boolean_t	zri_timed_out;
90 
91 	/*
92 	 * The currently registered cleanup function, which will be called
93 	 * with the stored argument if a fatal error occurs.
94 	 */
95 	zcp_cleanup_t	*zri_cleanup;
96 	void		*zri_cleanup_arg;
97 } zcp_run_info_t;
98 
99 zcp_run_info_t *zcp_run_info(lua_State *);
100 void zcp_register_cleanup(lua_State *, zcp_cleanup_t, void *);
101 void zcp_clear_cleanup(lua_State *);
102 void zcp_cleanup(lua_State *);
103 
104 /*
105  * Argument parsing routines for channel program callback functions.
106  */
107 typedef struct zcp_arg {
108 	/*
109 	 * The name of this argument. For keyword arguments this is the name
110 	 * functions will use to set the argument. For positional arguments
111 	 * the name has no programatic meaning, but will appear in error
112 	 * messages and help output.
113 	 */
114 	const char *za_name;
115 
116 	/*
117 	 * The Lua type this argument should have (e.g. LUA_TSTRING,
118 	 * LUA_TBOOLEAN) see the lua_type() function documentation for a
119 	 * complete list. Calling a function with an argument that does
120 	 * not match the expected type will result in the program terminating.
121 	 */
122 	const int za_lua_type;
123 } zcp_arg_t;
124 
125 void zcp_parse_args(lua_State *, const char *, const zcp_arg_t *,
126     const zcp_arg_t *);
127 int zcp_nvlist_to_lua(lua_State *, nvlist_t *, char *, int);
128 int zcp_dataset_hold_error(lua_State *, dsl_pool_t *, const char *, int);
129 struct dsl_dataset *zcp_dataset_hold(lua_State *, dsl_pool_t *,
130     const char *, void *);
131 
132 typedef int (zcp_lib_func_t)(lua_State *);
133 typedef struct zcp_lib_info {
134 	const char *name;
135 	zcp_lib_func_t *func;
136 	const zcp_arg_t pargs[4];
137 	const zcp_arg_t kwargs[2];
138 } zcp_lib_info_t;
139 
140 int zcp_nvlist_to_lua(lua_State *, nvlist_t *, char *, int);
141 
142 #ifdef	__cplusplus
143 }
144 #endif
145 
146 #endif	/* _SYS_ZCP_H */
147