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