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