xref: /freebsd/sys/contrib/openzfs/module/zfs/zcp.c (revision 7cc42f6d)
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 /*
21  * ZFS Channel Programs (ZCP)
22  *
23  * The ZCP interface allows various ZFS commands and operations ZFS
24  * administrative operations (e.g. creating and destroying snapshots, typically
25  * performed via an ioctl to /dev/zfs by the zfs(8) command and
26  * libzfs/libzfs_core) to be run * programmatically as a Lua script.  A ZCP
27  * script is run as a dsl_sync_task and fully executed during one transaction
28  * group sync.  This ensures that no other changes can be written concurrently
29  * with a running Lua script.  Combining multiple calls to the exposed ZFS
30  * functions into one script gives a number of benefits:
31  *
32  * 1. Atomicity.  For some compound or iterative operations, it's useful to be
33  * able to guarantee that the state of a pool has not changed between calls to
34  * ZFS.
35  *
36  * 2. Performance.  If a large number of changes need to be made (e.g. deleting
37  * many filesystems), there can be a significant performance penalty as a
38  * result of the need to wait for a transaction group sync to pass for every
39  * single operation.  When expressed as a single ZCP script, all these changes
40  * can be performed at once in one txg sync.
41  *
42  * A modified version of the Lua 5.2 interpreter is used to run channel program
43  * scripts. The Lua 5.2 manual can be found at:
44  *
45  *      http://www.lua.org/manual/5.2/
46  *
47  * If being run by a user (via an ioctl syscall), executing a ZCP script
48  * requires root privileges in the global zone.
49  *
50  * Scripts are passed to zcp_eval() as a string, then run in a synctask by
51  * zcp_eval_sync().  Arguments can be passed into the Lua script as an nvlist,
52  * which will be converted to a Lua table.  Similarly, values returned from
53  * a ZCP script will be converted to an nvlist.  See zcp_lua_to_nvlist_impl()
54  * for details on exact allowed types and conversion.
55  *
56  * ZFS functionality is exposed to a ZCP script as a library of function calls.
57  * These calls are sorted into submodules, such as zfs.list and zfs.sync, for
58  * iterators and synctasks, respectively.  Each of these submodules resides in
59  * its own source file, with a zcp_*_info structure describing each library
60  * call in the submodule.
61  *
62  * Error handling in ZCP scripts is handled by a number of different methods
63  * based on severity:
64  *
65  * 1. Memory and time limits are in place to prevent a channel program from
66  * consuming excessive system or running forever.  If one of these limits is
67  * hit, the channel program will be stopped immediately and return from
68  * zcp_eval() with an error code. No attempt will be made to roll back or undo
69  * any changes made by the channel program before the error occurred.
70  * Consumers invoking zcp_eval() from elsewhere in the kernel may pass a time
71  * limit of 0, disabling the time limit.
72  *
73  * 2. Internal Lua errors can occur as a result of a syntax error, calling a
74  * library function with incorrect arguments, invoking the error() function,
75  * failing an assert(), or other runtime errors.  In these cases the channel
76  * program will stop executing and return from zcp_eval() with an error code.
77  * In place of a return value, an error message will also be returned in the
78  * 'result' nvlist containing information about the error. No attempt will be
79  * made to roll back or undo any changes made by the channel program before the
80  * error occurred.
81  *
82  * 3. If an error occurs inside a ZFS library call which returns an error code,
83  * the error is returned to the Lua script to be handled as desired.
84  *
85  * In the first two cases, Lua's error-throwing mechanism is used, which
86  * longjumps out of the script execution with luaL_error() and returns with the
87  * error.
88  *
89  * See zfs-program(8) for more information on high level usage.
90  */
91 
92 #include <sys/lua/lua.h>
93 #include <sys/lua/lualib.h>
94 #include <sys/lua/lauxlib.h>
95 
96 #include <sys/dsl_prop.h>
97 #include <sys/dsl_synctask.h>
98 #include <sys/dsl_dataset.h>
99 #include <sys/zcp.h>
100 #include <sys/zcp_iter.h>
101 #include <sys/zcp_prop.h>
102 #include <sys/zcp_global.h>
103 #include <sys/zvol.h>
104 
105 #ifndef KM_NORMALPRI
106 #define	KM_NORMALPRI	0
107 #endif
108 
109 #define	ZCP_NVLIST_MAX_DEPTH 20
110 
111 uint64_t zfs_lua_check_instrlimit_interval = 100;
112 unsigned long zfs_lua_max_instrlimit = ZCP_MAX_INSTRLIMIT;
113 unsigned long zfs_lua_max_memlimit = ZCP_MAX_MEMLIMIT;
114 
115 /*
116  * Forward declarations for mutually recursive functions
117  */
118 static int zcp_nvpair_value_to_lua(lua_State *, nvpair_t *, char *, int);
119 static int zcp_lua_to_nvlist_impl(lua_State *, int, nvlist_t *, const char *,
120     int);
121 
122 /*
123  * The outer-most error callback handler for use with lua_pcall(). On
124  * error Lua will call this callback with a single argument that
125  * represents the error value. In most cases this will be a string
126  * containing an error message, but channel programs can use Lua's
127  * error() function to return arbitrary objects as errors. This callback
128  * returns (on the Lua stack) the original error object along with a traceback.
129  *
130  * Fatal Lua errors can occur while resources are held, so we also call any
131  * registered cleanup function here.
132  */
133 static int
134 zcp_error_handler(lua_State *state)
135 {
136 	const char *msg;
137 
138 	zcp_cleanup(state);
139 
140 	VERIFY3U(1, ==, lua_gettop(state));
141 	msg = lua_tostring(state, 1);
142 	luaL_traceback(state, state, msg, 1);
143 	return (1);
144 }
145 
146 int
147 zcp_argerror(lua_State *state, int narg, const char *msg, ...)
148 {
149 	va_list alist;
150 
151 	va_start(alist, msg);
152 	const char *buf = lua_pushvfstring(state, msg, alist);
153 	va_end(alist);
154 
155 	return (luaL_argerror(state, narg, buf));
156 }
157 
158 /*
159  * Install a new cleanup function, which will be invoked with the given
160  * opaque argument if a fatal error causes the Lua interpreter to longjump out
161  * of a function call.
162  *
163  * If an error occurs, the cleanup function will be invoked exactly once and
164  * then unregistered.
165  *
166  * Returns the registered cleanup handler so the caller can deregister it
167  * if no error occurs.
168  */
169 zcp_cleanup_handler_t *
170 zcp_register_cleanup(lua_State *state, zcp_cleanup_t cleanfunc, void *cleanarg)
171 {
172 	zcp_run_info_t *ri = zcp_run_info(state);
173 
174 	zcp_cleanup_handler_t *zch = kmem_alloc(sizeof (*zch), KM_SLEEP);
175 	zch->zch_cleanup_func = cleanfunc;
176 	zch->zch_cleanup_arg = cleanarg;
177 	list_insert_head(&ri->zri_cleanup_handlers, zch);
178 
179 	return (zch);
180 }
181 
182 void
183 zcp_deregister_cleanup(lua_State *state, zcp_cleanup_handler_t *zch)
184 {
185 	zcp_run_info_t *ri = zcp_run_info(state);
186 	list_remove(&ri->zri_cleanup_handlers, zch);
187 	kmem_free(zch, sizeof (*zch));
188 }
189 
190 /*
191  * Execute the currently registered cleanup handlers then free them and
192  * destroy the handler list.
193  */
194 void
195 zcp_cleanup(lua_State *state)
196 {
197 	zcp_run_info_t *ri = zcp_run_info(state);
198 
199 	for (zcp_cleanup_handler_t *zch =
200 	    list_remove_head(&ri->zri_cleanup_handlers); zch != NULL;
201 	    zch = list_remove_head(&ri->zri_cleanup_handlers)) {
202 		zch->zch_cleanup_func(zch->zch_cleanup_arg);
203 		kmem_free(zch, sizeof (*zch));
204 	}
205 }
206 
207 /*
208  * Convert the lua table at the given index on the Lua stack to an nvlist
209  * and return it.
210  *
211  * If the table can not be converted for any reason, NULL is returned and
212  * an error message is pushed onto the Lua stack.
213  */
214 static nvlist_t *
215 zcp_table_to_nvlist(lua_State *state, int index, int depth)
216 {
217 	nvlist_t *nvl;
218 	/*
219 	 * Converting a Lua table to an nvlist with key uniqueness checking is
220 	 * O(n^2) in the number of keys in the nvlist, which can take a long
221 	 * time when we return a large table from a channel program.
222 	 * Furthermore, Lua's table interface *almost* guarantees unique keys
223 	 * on its own (details below). Therefore, we don't use fnvlist_alloc()
224 	 * here to avoid the built-in uniqueness checking.
225 	 *
226 	 * The *almost* is because it's possible to have key collisions between
227 	 * e.g. the string "1" and the number 1, or the string "true" and the
228 	 * boolean true, so we explicitly check that when we're looking at a
229 	 * key which is an integer / boolean or a string that can be parsed as
230 	 * one of those types. In the worst case this could still devolve into
231 	 * O(n^2), so we only start doing these checks on boolean/integer keys
232 	 * once we've seen a string key which fits this weird usage pattern.
233 	 *
234 	 * Ultimately, we still want callers to know that the keys in this
235 	 * nvlist are unique, so before we return this we set the nvlist's
236 	 * flags to reflect that.
237 	 */
238 	VERIFY0(nvlist_alloc(&nvl, 0, KM_SLEEP));
239 
240 	/*
241 	 * Push an empty stack slot where lua_next() will store each
242 	 * table key.
243 	 */
244 	lua_pushnil(state);
245 	boolean_t saw_str_could_collide = B_FALSE;
246 	while (lua_next(state, index) != 0) {
247 		/*
248 		 * The next key-value pair from the table at index is
249 		 * now on the stack, with the key at stack slot -2 and
250 		 * the value at slot -1.
251 		 */
252 		int err = 0;
253 		char buf[32];
254 		const char *key = NULL;
255 		boolean_t key_could_collide = B_FALSE;
256 
257 		switch (lua_type(state, -2)) {
258 		case LUA_TSTRING:
259 			key = lua_tostring(state, -2);
260 
261 			/* check if this could collide with a number or bool */
262 			long long tmp;
263 			int parselen;
264 			if ((sscanf(key, "%lld%n", &tmp, &parselen) > 0 &&
265 			    parselen == strlen(key)) ||
266 			    strcmp(key, "true") == 0 ||
267 			    strcmp(key, "false") == 0) {
268 				key_could_collide = B_TRUE;
269 				saw_str_could_collide = B_TRUE;
270 			}
271 			break;
272 		case LUA_TBOOLEAN:
273 			key = (lua_toboolean(state, -2) == B_TRUE ?
274 			    "true" : "false");
275 			if (saw_str_could_collide) {
276 				key_could_collide = B_TRUE;
277 			}
278 			break;
279 		case LUA_TNUMBER:
280 			VERIFY3U(sizeof (buf), >,
281 			    snprintf(buf, sizeof (buf), "%lld",
282 			    (longlong_t)lua_tonumber(state, -2)));
283 			key = buf;
284 			if (saw_str_could_collide) {
285 				key_could_collide = B_TRUE;
286 			}
287 			break;
288 		default:
289 			fnvlist_free(nvl);
290 			(void) lua_pushfstring(state, "Invalid key "
291 			    "type '%s' in table",
292 			    lua_typename(state, lua_type(state, -2)));
293 			return (NULL);
294 		}
295 		/*
296 		 * Check for type-mismatched key collisions, and throw an error.
297 		 */
298 		if (key_could_collide && nvlist_exists(nvl, key)) {
299 			fnvlist_free(nvl);
300 			(void) lua_pushfstring(state, "Collision of "
301 			    "key '%s' in table", key);
302 			return (NULL);
303 		}
304 		/*
305 		 * Recursively convert the table value and insert into
306 		 * the new nvlist with the parsed key.  To prevent
307 		 * stack overflow on circular or heavily nested tables,
308 		 * we track the current nvlist depth.
309 		 */
310 		if (depth >= ZCP_NVLIST_MAX_DEPTH) {
311 			fnvlist_free(nvl);
312 			(void) lua_pushfstring(state, "Maximum table "
313 			    "depth (%d) exceeded for table",
314 			    ZCP_NVLIST_MAX_DEPTH);
315 			return (NULL);
316 		}
317 		err = zcp_lua_to_nvlist_impl(state, -1, nvl, key,
318 		    depth + 1);
319 		if (err != 0) {
320 			fnvlist_free(nvl);
321 			/*
322 			 * Error message has been pushed to the lua
323 			 * stack by the recursive call.
324 			 */
325 			return (NULL);
326 		}
327 		/*
328 		 * Pop the value pushed by lua_next().
329 		 */
330 		lua_pop(state, 1);
331 	}
332 
333 	/*
334 	 * Mark the nvlist as having unique keys. This is a little ugly, but we
335 	 * ensured above that there are no duplicate keys in the nvlist.
336 	 */
337 	nvl->nvl_nvflag |= NV_UNIQUE_NAME;
338 
339 	return (nvl);
340 }
341 
342 /*
343  * Convert a value from the given index into the lua stack to an nvpair, adding
344  * it to an nvlist with the given key.
345  *
346  * Values are converted as follows:
347  *
348  *   string -> string
349  *   number -> int64
350  *   boolean -> boolean
351  *   nil -> boolean (no value)
352  *
353  * Lua tables are converted to nvlists and then inserted. The table's keys
354  * are converted to strings then used as keys in the nvlist to store each table
355  * element.  Keys are converted as follows:
356  *
357  *   string -> no change
358  *   number -> "%lld"
359  *   boolean -> "true" | "false"
360  *   nil -> error
361  *
362  * In the case of a key collision, an error is thrown.
363  *
364  * If an error is encountered, a nonzero error code is returned, and an error
365  * string will be pushed onto the Lua stack.
366  */
367 static int
368 zcp_lua_to_nvlist_impl(lua_State *state, int index, nvlist_t *nvl,
369     const char *key, int depth)
370 {
371 	/*
372 	 * Verify that we have enough remaining space in the lua stack to parse
373 	 * a key-value pair and push an error.
374 	 */
375 	if (!lua_checkstack(state, 3)) {
376 		(void) lua_pushstring(state, "Lua stack overflow");
377 		return (1);
378 	}
379 
380 	index = lua_absindex(state, index);
381 
382 	switch (lua_type(state, index)) {
383 	case LUA_TNIL:
384 		fnvlist_add_boolean(nvl, key);
385 		break;
386 	case LUA_TBOOLEAN:
387 		fnvlist_add_boolean_value(nvl, key,
388 		    lua_toboolean(state, index));
389 		break;
390 	case LUA_TNUMBER:
391 		fnvlist_add_int64(nvl, key, lua_tonumber(state, index));
392 		break;
393 	case LUA_TSTRING:
394 		fnvlist_add_string(nvl, key, lua_tostring(state, index));
395 		break;
396 	case LUA_TTABLE: {
397 		nvlist_t *value_nvl = zcp_table_to_nvlist(state, index, depth);
398 		if (value_nvl == NULL)
399 			return (SET_ERROR(EINVAL));
400 
401 		fnvlist_add_nvlist(nvl, key, value_nvl);
402 		fnvlist_free(value_nvl);
403 		break;
404 	}
405 	default:
406 		(void) lua_pushfstring(state,
407 		    "Invalid value type '%s' for key '%s'",
408 		    lua_typename(state, lua_type(state, index)), key);
409 		return (SET_ERROR(EINVAL));
410 	}
411 
412 	return (0);
413 }
414 
415 /*
416  * Convert a lua value to an nvpair, adding it to an nvlist with the given key.
417  */
418 static void
419 zcp_lua_to_nvlist(lua_State *state, int index, nvlist_t *nvl, const char *key)
420 {
421 	/*
422 	 * On error, zcp_lua_to_nvlist_impl pushes an error string onto the Lua
423 	 * stack before returning with a nonzero error code. If an error is
424 	 * returned, throw a fatal lua error with the given string.
425 	 */
426 	if (zcp_lua_to_nvlist_impl(state, index, nvl, key, 0) != 0)
427 		(void) lua_error(state);
428 }
429 
430 static int
431 zcp_lua_to_nvlist_helper(lua_State *state)
432 {
433 	nvlist_t *nv = (nvlist_t *)lua_touserdata(state, 2);
434 	const char *key = (const char *)lua_touserdata(state, 1);
435 	zcp_lua_to_nvlist(state, 3, nv, key);
436 	return (0);
437 }
438 
439 static void
440 zcp_convert_return_values(lua_State *state, nvlist_t *nvl,
441     const char *key, int *result)
442 {
443 	int err;
444 	VERIFY3U(1, ==, lua_gettop(state));
445 	lua_pushcfunction(state, zcp_lua_to_nvlist_helper);
446 	lua_pushlightuserdata(state, (char *)key);
447 	lua_pushlightuserdata(state, nvl);
448 	lua_pushvalue(state, 1);
449 	lua_remove(state, 1);
450 	err = lua_pcall(state, 3, 0, 0); /* zcp_lua_to_nvlist_helper */
451 	if (err != 0) {
452 		zcp_lua_to_nvlist(state, 1, nvl, ZCP_RET_ERROR);
453 		*result = SET_ERROR(ECHRNG);
454 	}
455 }
456 
457 /*
458  * Push a Lua table representing nvl onto the stack.  If it can't be
459  * converted, return EINVAL, fill in errbuf, and push nothing. errbuf may
460  * be specified as NULL, in which case no error string will be output.
461  *
462  * Most nvlists are converted as simple key->value Lua tables, but we make
463  * an exception for the case where all nvlist entries are BOOLEANs (a string
464  * key without a value). In Lua, a table key pointing to a value of Nil
465  * (no value) is equivalent to the key not existing, so a BOOLEAN nvlist
466  * entry can't be directly converted to a Lua table entry. Nvlists of entirely
467  * BOOLEAN entries are frequently used to pass around lists of datasets, so for
468  * convenience we check for this case, and convert it to a simple Lua array of
469  * strings.
470  */
471 int
472 zcp_nvlist_to_lua(lua_State *state, nvlist_t *nvl,
473     char *errbuf, int errbuf_len)
474 {
475 	nvpair_t *pair;
476 	lua_newtable(state);
477 	boolean_t has_values = B_FALSE;
478 	/*
479 	 * If the list doesn't have any values, just convert it to a string
480 	 * array.
481 	 */
482 	for (pair = nvlist_next_nvpair(nvl, NULL);
483 	    pair != NULL; pair = nvlist_next_nvpair(nvl, pair)) {
484 		if (nvpair_type(pair) != DATA_TYPE_BOOLEAN) {
485 			has_values = B_TRUE;
486 			break;
487 		}
488 	}
489 	if (!has_values) {
490 		int i = 1;
491 		for (pair = nvlist_next_nvpair(nvl, NULL);
492 		    pair != NULL; pair = nvlist_next_nvpair(nvl, pair)) {
493 			(void) lua_pushinteger(state, i);
494 			(void) lua_pushstring(state, nvpair_name(pair));
495 			(void) lua_settable(state, -3);
496 			i++;
497 		}
498 	} else {
499 		for (pair = nvlist_next_nvpair(nvl, NULL);
500 		    pair != NULL; pair = nvlist_next_nvpair(nvl, pair)) {
501 			int err = zcp_nvpair_value_to_lua(state, pair,
502 			    errbuf, errbuf_len);
503 			if (err != 0) {
504 				lua_pop(state, 1);
505 				return (err);
506 			}
507 			(void) lua_setfield(state, -2, nvpair_name(pair));
508 		}
509 	}
510 	return (0);
511 }
512 
513 /*
514  * Push a Lua object representing the value of "pair" onto the stack.
515  *
516  * Only understands boolean_value, string, int64, nvlist,
517  * string_array, and int64_array type values.  For other
518  * types, returns EINVAL, fills in errbuf, and pushes nothing.
519  */
520 static int
521 zcp_nvpair_value_to_lua(lua_State *state, nvpair_t *pair,
522     char *errbuf, int errbuf_len)
523 {
524 	int err = 0;
525 
526 	if (pair == NULL) {
527 		lua_pushnil(state);
528 		return (0);
529 	}
530 
531 	switch (nvpair_type(pair)) {
532 	case DATA_TYPE_BOOLEAN_VALUE:
533 		(void) lua_pushboolean(state,
534 		    fnvpair_value_boolean_value(pair));
535 		break;
536 	case DATA_TYPE_STRING:
537 		(void) lua_pushstring(state, fnvpair_value_string(pair));
538 		break;
539 	case DATA_TYPE_INT64:
540 		(void) lua_pushinteger(state, fnvpair_value_int64(pair));
541 		break;
542 	case DATA_TYPE_NVLIST:
543 		err = zcp_nvlist_to_lua(state,
544 		    fnvpair_value_nvlist(pair), errbuf, errbuf_len);
545 		break;
546 	case DATA_TYPE_STRING_ARRAY: {
547 		char **strarr;
548 		uint_t nelem;
549 		(void) nvpair_value_string_array(pair, &strarr, &nelem);
550 		lua_newtable(state);
551 		for (int i = 0; i < nelem; i++) {
552 			(void) lua_pushinteger(state, i + 1);
553 			(void) lua_pushstring(state, strarr[i]);
554 			(void) lua_settable(state, -3);
555 		}
556 		break;
557 	}
558 	case DATA_TYPE_UINT64_ARRAY: {
559 		uint64_t *intarr;
560 		uint_t nelem;
561 		(void) nvpair_value_uint64_array(pair, &intarr, &nelem);
562 		lua_newtable(state);
563 		for (int i = 0; i < nelem; i++) {
564 			(void) lua_pushinteger(state, i + 1);
565 			(void) lua_pushinteger(state, intarr[i]);
566 			(void) lua_settable(state, -3);
567 		}
568 		break;
569 	}
570 	case DATA_TYPE_INT64_ARRAY: {
571 		int64_t *intarr;
572 		uint_t nelem;
573 		(void) nvpair_value_int64_array(pair, &intarr, &nelem);
574 		lua_newtable(state);
575 		for (int i = 0; i < nelem; i++) {
576 			(void) lua_pushinteger(state, i + 1);
577 			(void) lua_pushinteger(state, intarr[i]);
578 			(void) lua_settable(state, -3);
579 		}
580 		break;
581 	}
582 	default: {
583 		if (errbuf != NULL) {
584 			(void) snprintf(errbuf, errbuf_len,
585 			    "Unhandled nvpair type %d for key '%s'",
586 			    nvpair_type(pair), nvpair_name(pair));
587 		}
588 		return (SET_ERROR(EINVAL));
589 	}
590 	}
591 	return (err);
592 }
593 
594 int
595 zcp_dataset_hold_error(lua_State *state, dsl_pool_t *dp, const char *dsname,
596     int error)
597 {
598 	if (error == ENOENT) {
599 		(void) zcp_argerror(state, 1, "no such dataset '%s'", dsname);
600 		return (0); /* not reached; zcp_argerror will longjmp */
601 	} else if (error == EXDEV) {
602 		(void) zcp_argerror(state, 1,
603 		    "dataset '%s' is not in the target pool '%s'",
604 		    dsname, spa_name(dp->dp_spa));
605 		return (0); /* not reached; zcp_argerror will longjmp */
606 	} else if (error == EIO) {
607 		(void) luaL_error(state,
608 		    "I/O error while accessing dataset '%s'", dsname);
609 		return (0); /* not reached; luaL_error will longjmp */
610 	} else if (error != 0) {
611 		(void) luaL_error(state,
612 		    "unexpected error %d while accessing dataset '%s'",
613 		    error, dsname);
614 		return (0); /* not reached; luaL_error will longjmp */
615 	}
616 	return (0);
617 }
618 
619 /*
620  * Note: will longjmp (via lua_error()) on error.
621  * Assumes that the dsname is argument #1 (for error reporting purposes).
622  */
623 dsl_dataset_t *
624 zcp_dataset_hold(lua_State *state, dsl_pool_t *dp, const char *dsname,
625     void *tag)
626 {
627 	dsl_dataset_t *ds;
628 	int error = dsl_dataset_hold(dp, dsname, tag, &ds);
629 	(void) zcp_dataset_hold_error(state, dp, dsname, error);
630 	return (ds);
631 }
632 
633 static int zcp_debug(lua_State *);
634 static zcp_lib_info_t zcp_debug_info = {
635 	.name = "debug",
636 	.func = zcp_debug,
637 	.pargs = {
638 	    { .za_name = "debug string", .za_lua_type = LUA_TSTRING},
639 	    {NULL, 0}
640 	},
641 	.kwargs = {
642 	    {NULL, 0}
643 	}
644 };
645 
646 static int
647 zcp_debug(lua_State *state)
648 {
649 	const char *dbgstring;
650 	zcp_run_info_t *ri = zcp_run_info(state);
651 	zcp_lib_info_t *libinfo = &zcp_debug_info;
652 
653 	zcp_parse_args(state, libinfo->name, libinfo->pargs, libinfo->kwargs);
654 
655 	dbgstring = lua_tostring(state, 1);
656 
657 	zfs_dbgmsg("txg %lld ZCP: %s", ri->zri_tx->tx_txg, dbgstring);
658 
659 	return (0);
660 }
661 
662 static int zcp_exists(lua_State *);
663 static zcp_lib_info_t zcp_exists_info = {
664 	.name = "exists",
665 	.func = zcp_exists,
666 	.pargs = {
667 	    { .za_name = "dataset", .za_lua_type = LUA_TSTRING},
668 	    {NULL, 0}
669 	},
670 	.kwargs = {
671 	    {NULL, 0}
672 	}
673 };
674 
675 static int
676 zcp_exists(lua_State *state)
677 {
678 	zcp_run_info_t *ri = zcp_run_info(state);
679 	dsl_pool_t *dp = ri->zri_pool;
680 	zcp_lib_info_t *libinfo = &zcp_exists_info;
681 
682 	zcp_parse_args(state, libinfo->name, libinfo->pargs, libinfo->kwargs);
683 
684 	const char *dsname = lua_tostring(state, 1);
685 
686 	dsl_dataset_t *ds;
687 	int error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
688 	if (error == 0) {
689 		dsl_dataset_rele(ds, FTAG);
690 		lua_pushboolean(state, B_TRUE);
691 	} else if (error == ENOENT) {
692 		lua_pushboolean(state, B_FALSE);
693 	} else if (error == EXDEV) {
694 		return (luaL_error(state, "dataset '%s' is not in the "
695 		    "target pool", dsname));
696 	} else if (error == EIO) {
697 		return (luaL_error(state, "I/O error opening dataset '%s'",
698 		    dsname));
699 	} else if (error != 0) {
700 		return (luaL_error(state, "unexpected error %d", error));
701 	}
702 
703 	return (1);
704 }
705 
706 /*
707  * Allocate/realloc/free a buffer for the lua interpreter.
708  *
709  * When nsize is 0, behaves as free() and returns NULL.
710  *
711  * If ptr is NULL, behaves as malloc() and returns an allocated buffer of size
712  * at least nsize.
713  *
714  * Otherwise, behaves as realloc(), changing the allocation from osize to nsize.
715  * Shrinking the buffer size never fails.
716  *
717  * The original allocated buffer size is stored as a uint64 at the beginning of
718  * the buffer to avoid actually reallocating when shrinking a buffer, since lua
719  * requires that this operation never fail.
720  */
721 static void *
722 zcp_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
723 {
724 	zcp_alloc_arg_t *allocargs = ud;
725 	int flags = (allocargs->aa_must_succeed) ?
726 	    KM_SLEEP : (KM_NOSLEEP | KM_NORMALPRI);
727 
728 	if (nsize == 0) {
729 		if (ptr != NULL) {
730 			int64_t *allocbuf = (int64_t *)ptr - 1;
731 			int64_t allocsize = *allocbuf;
732 			ASSERT3S(allocsize, >, 0);
733 			ASSERT3S(allocargs->aa_alloc_remaining + allocsize, <=,
734 			    allocargs->aa_alloc_limit);
735 			allocargs->aa_alloc_remaining += allocsize;
736 			vmem_free(allocbuf, allocsize);
737 		}
738 		return (NULL);
739 	} else if (ptr == NULL) {
740 		int64_t *allocbuf;
741 		int64_t allocsize = nsize + sizeof (int64_t);
742 
743 		if (!allocargs->aa_must_succeed &&
744 		    (allocsize <= 0 ||
745 		    allocsize > allocargs->aa_alloc_remaining)) {
746 			return (NULL);
747 		}
748 
749 		allocbuf = vmem_alloc(allocsize, flags);
750 		if (allocbuf == NULL) {
751 			return (NULL);
752 		}
753 		allocargs->aa_alloc_remaining -= allocsize;
754 
755 		*allocbuf = allocsize;
756 		return (allocbuf + 1);
757 	} else if (nsize <= osize) {
758 		/*
759 		 * If shrinking the buffer, lua requires that the reallocation
760 		 * never fail.
761 		 */
762 		return (ptr);
763 	} else {
764 		ASSERT3U(nsize, >, osize);
765 
766 		uint64_t *luabuf = zcp_lua_alloc(ud, NULL, 0, nsize);
767 		if (luabuf == NULL) {
768 			return (NULL);
769 		}
770 		(void) memcpy(luabuf, ptr, osize);
771 		VERIFY3P(zcp_lua_alloc(ud, ptr, osize, 0), ==, NULL);
772 		return (luabuf);
773 	}
774 }
775 
776 /* ARGSUSED */
777 static void
778 zcp_lua_counthook(lua_State *state, lua_Debug *ar)
779 {
780 	lua_getfield(state, LUA_REGISTRYINDEX, ZCP_RUN_INFO_KEY);
781 	zcp_run_info_t *ri = lua_touserdata(state, -1);
782 
783 	/*
784 	 * Check if we were canceled while waiting for the
785 	 * txg to sync or from our open context thread
786 	 */
787 	if (ri->zri_canceled ||
788 	    (!ri->zri_sync && issig(JUSTLOOKING) && issig(FORREAL))) {
789 		ri->zri_canceled = B_TRUE;
790 		(void) lua_pushstring(state, "Channel program was canceled.");
791 		(void) lua_error(state);
792 		/* Unreachable */
793 	}
794 
795 	/*
796 	 * Check how many instructions the channel program has
797 	 * executed so far, and compare against the limit.
798 	 */
799 	ri->zri_curinstrs += zfs_lua_check_instrlimit_interval;
800 	if (ri->zri_maxinstrs != 0 && ri->zri_curinstrs > ri->zri_maxinstrs) {
801 		ri->zri_timed_out = B_TRUE;
802 		(void) lua_pushstring(state,
803 		    "Channel program timed out.");
804 		(void) lua_error(state);
805 		/* Unreachable */
806 	}
807 }
808 
809 static int
810 zcp_panic_cb(lua_State *state)
811 {
812 	panic("unprotected error in call to Lua API (%s)\n",
813 	    lua_tostring(state, -1));
814 	return (0);
815 }
816 
817 static void
818 zcp_eval_impl(dmu_tx_t *tx, zcp_run_info_t *ri)
819 {
820 	int err;
821 	lua_State *state = ri->zri_state;
822 
823 	VERIFY3U(3, ==, lua_gettop(state));
824 
825 	/* finish initializing our runtime state */
826 	ri->zri_pool = dmu_tx_pool(tx);
827 	ri->zri_tx = tx;
828 	list_create(&ri->zri_cleanup_handlers, sizeof (zcp_cleanup_handler_t),
829 	    offsetof(zcp_cleanup_handler_t, zch_node));
830 
831 	/*
832 	 * Store the zcp_run_info_t struct for this run in the Lua registry.
833 	 * Registry entries are not directly accessible by the Lua scripts but
834 	 * can be accessed by our callbacks.
835 	 */
836 	lua_pushlightuserdata(state, ri);
837 	lua_setfield(state, LUA_REGISTRYINDEX, ZCP_RUN_INFO_KEY);
838 	VERIFY3U(3, ==, lua_gettop(state));
839 
840 	/*
841 	 * Tell the Lua interpreter to call our handler every count
842 	 * instructions. Channel programs that execute too many instructions
843 	 * should die with ETIME.
844 	 */
845 	(void) lua_sethook(state, zcp_lua_counthook, LUA_MASKCOUNT,
846 	    zfs_lua_check_instrlimit_interval);
847 
848 	/*
849 	 * Tell the Lua memory allocator to stop using KM_SLEEP before handing
850 	 * off control to the channel program. Channel programs that use too
851 	 * much memory should die with ENOSPC.
852 	 */
853 	ri->zri_allocargs->aa_must_succeed = B_FALSE;
854 
855 	/*
856 	 * Call the Lua function that open-context passed us. This pops the
857 	 * function and its input from the stack and pushes any return
858 	 * or error values.
859 	 */
860 	err = lua_pcall(state, 1, LUA_MULTRET, 1);
861 
862 	/*
863 	 * Let Lua use KM_SLEEP while we interpret the return values.
864 	 */
865 	ri->zri_allocargs->aa_must_succeed = B_TRUE;
866 
867 	/*
868 	 * Remove the error handler callback from the stack. At this point,
869 	 * there shouldn't be any cleanup handler registered in the handler
870 	 * list (zri_cleanup_handlers), regardless of whether it ran or not.
871 	 */
872 	list_destroy(&ri->zri_cleanup_handlers);
873 	lua_remove(state, 1);
874 
875 	switch (err) {
876 	case LUA_OK: {
877 		/*
878 		 * Lua supports returning multiple values in a single return
879 		 * statement.  Return values will have been pushed onto the
880 		 * stack:
881 		 * 1: Return value 1
882 		 * 2: Return value 2
883 		 * 3: etc...
884 		 * To simplify the process of retrieving a return value from a
885 		 * channel program, we disallow returning more than one value
886 		 * to ZFS from the Lua script, yielding a singleton return
887 		 * nvlist of the form { "return": Return value 1 }.
888 		 */
889 		int return_count = lua_gettop(state);
890 
891 		if (return_count == 1) {
892 			ri->zri_result = 0;
893 			zcp_convert_return_values(state, ri->zri_outnvl,
894 			    ZCP_RET_RETURN, &ri->zri_result);
895 		} else if (return_count > 1) {
896 			ri->zri_result = SET_ERROR(ECHRNG);
897 			lua_settop(state, 0);
898 			(void) lua_pushfstring(state, "Multiple return "
899 			    "values not supported");
900 			zcp_convert_return_values(state, ri->zri_outnvl,
901 			    ZCP_RET_ERROR, &ri->zri_result);
902 		}
903 		break;
904 	}
905 	case LUA_ERRRUN:
906 	case LUA_ERRGCMM: {
907 		/*
908 		 * The channel program encountered a fatal error within the
909 		 * script, such as failing an assertion, or calling a function
910 		 * with incompatible arguments. The error value and the
911 		 * traceback generated by zcp_error_handler() should be on the
912 		 * stack.
913 		 */
914 		VERIFY3U(1, ==, lua_gettop(state));
915 		if (ri->zri_timed_out) {
916 			ri->zri_result = SET_ERROR(ETIME);
917 		} else if (ri->zri_canceled) {
918 			ri->zri_result = SET_ERROR(EINTR);
919 		} else {
920 			ri->zri_result = SET_ERROR(ECHRNG);
921 		}
922 
923 		zcp_convert_return_values(state, ri->zri_outnvl,
924 		    ZCP_RET_ERROR, &ri->zri_result);
925 
926 		if (ri->zri_result == ETIME && ri->zri_outnvl != NULL) {
927 			(void) nvlist_add_uint64(ri->zri_outnvl,
928 			    ZCP_ARG_INSTRLIMIT, ri->zri_curinstrs);
929 		}
930 		break;
931 	}
932 	case LUA_ERRERR: {
933 		/*
934 		 * The channel program encountered a fatal error within the
935 		 * script, and we encountered another error while trying to
936 		 * compute the traceback in zcp_error_handler(). We can only
937 		 * return the error message.
938 		 */
939 		VERIFY3U(1, ==, lua_gettop(state));
940 		if (ri->zri_timed_out) {
941 			ri->zri_result = SET_ERROR(ETIME);
942 		} else if (ri->zri_canceled) {
943 			ri->zri_result = SET_ERROR(EINTR);
944 		} else {
945 			ri->zri_result = SET_ERROR(ECHRNG);
946 		}
947 
948 		zcp_convert_return_values(state, ri->zri_outnvl,
949 		    ZCP_RET_ERROR, &ri->zri_result);
950 		break;
951 	}
952 	case LUA_ERRMEM:
953 		/*
954 		 * Lua ran out of memory while running the channel program.
955 		 * There's not much we can do.
956 		 */
957 		ri->zri_result = SET_ERROR(ENOSPC);
958 		break;
959 	default:
960 		VERIFY0(err);
961 	}
962 }
963 
964 static void
965 zcp_pool_error(zcp_run_info_t *ri, const char *poolname)
966 {
967 	ri->zri_result = SET_ERROR(ECHRNG);
968 	lua_settop(ri->zri_state, 0);
969 	(void) lua_pushfstring(ri->zri_state, "Could not open pool: %s",
970 	    poolname);
971 	zcp_convert_return_values(ri->zri_state, ri->zri_outnvl,
972 	    ZCP_RET_ERROR, &ri->zri_result);
973 
974 }
975 
976 /*
977  * This callback is called when txg_wait_synced_sig encountered a signal.
978  * The txg_wait_synced_sig will continue to wait for the txg to complete
979  * after calling this callback.
980  */
981 /* ARGSUSED */
982 static void
983 zcp_eval_sig(void *arg, dmu_tx_t *tx)
984 {
985 	zcp_run_info_t *ri = arg;
986 
987 	ri->zri_canceled = B_TRUE;
988 }
989 
990 static void
991 zcp_eval_sync(void *arg, dmu_tx_t *tx)
992 {
993 	zcp_run_info_t *ri = arg;
994 
995 	/*
996 	 * Open context should have setup the stack to contain:
997 	 * 1: Error handler callback
998 	 * 2: Script to run (converted to a Lua function)
999 	 * 3: nvlist input to function (converted to Lua table or nil)
1000 	 */
1001 	VERIFY3U(3, ==, lua_gettop(ri->zri_state));
1002 
1003 	zcp_eval_impl(tx, ri);
1004 }
1005 
1006 static void
1007 zcp_eval_open(zcp_run_info_t *ri, const char *poolname)
1008 {
1009 	int error;
1010 	dsl_pool_t *dp;
1011 	dmu_tx_t *tx;
1012 
1013 	/*
1014 	 * See comment from the same assertion in zcp_eval_sync().
1015 	 */
1016 	VERIFY3U(3, ==, lua_gettop(ri->zri_state));
1017 
1018 	error = dsl_pool_hold(poolname, FTAG, &dp);
1019 	if (error != 0) {
1020 		zcp_pool_error(ri, poolname);
1021 		return;
1022 	}
1023 
1024 	/*
1025 	 * As we are running in open-context, we have no transaction associated
1026 	 * with the channel program. At the same time, functions from the
1027 	 * zfs.check submodule need to be associated with a transaction as
1028 	 * they are basically dry-runs of their counterparts in the zfs.sync
1029 	 * submodule. These functions should be able to run in open-context.
1030 	 * Therefore we create a new transaction that we later abort once
1031 	 * the channel program has been evaluated.
1032 	 */
1033 	tx = dmu_tx_create_dd(dp->dp_mos_dir);
1034 
1035 	zcp_eval_impl(tx, ri);
1036 
1037 	dmu_tx_abort(tx);
1038 
1039 	dsl_pool_rele(dp, FTAG);
1040 }
1041 
1042 int
1043 zcp_eval(const char *poolname, const char *program, boolean_t sync,
1044     uint64_t instrlimit, uint64_t memlimit, nvpair_t *nvarg, nvlist_t *outnvl)
1045 {
1046 	int err;
1047 	lua_State *state;
1048 	zcp_run_info_t runinfo;
1049 
1050 	if (instrlimit > zfs_lua_max_instrlimit)
1051 		return (SET_ERROR(EINVAL));
1052 	if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
1053 		return (SET_ERROR(EINVAL));
1054 
1055 	zcp_alloc_arg_t allocargs = {
1056 		.aa_must_succeed = B_TRUE,
1057 		.aa_alloc_remaining = (int64_t)memlimit,
1058 		.aa_alloc_limit = (int64_t)memlimit,
1059 	};
1060 
1061 	/*
1062 	 * Creates a Lua state with a memory allocator that uses KM_SLEEP.
1063 	 * This should never fail.
1064 	 */
1065 	state = lua_newstate(zcp_lua_alloc, &allocargs);
1066 	VERIFY(state != NULL);
1067 	(void) lua_atpanic(state, zcp_panic_cb);
1068 
1069 	/*
1070 	 * Load core Lua libraries we want access to.
1071 	 */
1072 	VERIFY3U(1, ==, luaopen_base(state));
1073 	lua_pop(state, 1);
1074 	VERIFY3U(1, ==, luaopen_coroutine(state));
1075 	lua_setglobal(state, LUA_COLIBNAME);
1076 	VERIFY0(lua_gettop(state));
1077 	VERIFY3U(1, ==, luaopen_string(state));
1078 	lua_setglobal(state, LUA_STRLIBNAME);
1079 	VERIFY0(lua_gettop(state));
1080 	VERIFY3U(1, ==, luaopen_table(state));
1081 	lua_setglobal(state, LUA_TABLIBNAME);
1082 	VERIFY0(lua_gettop(state));
1083 
1084 	/*
1085 	 * Load globally visible variables such as errno aliases.
1086 	 */
1087 	zcp_load_globals(state);
1088 	VERIFY0(lua_gettop(state));
1089 
1090 	/*
1091 	 * Load ZFS-specific modules.
1092 	 */
1093 	lua_newtable(state);
1094 	VERIFY3U(1, ==, zcp_load_list_lib(state));
1095 	lua_setfield(state, -2, "list");
1096 	VERIFY3U(1, ==, zcp_load_synctask_lib(state, B_FALSE));
1097 	lua_setfield(state, -2, "check");
1098 	VERIFY3U(1, ==, zcp_load_synctask_lib(state, B_TRUE));
1099 	lua_setfield(state, -2, "sync");
1100 	VERIFY3U(1, ==, zcp_load_get_lib(state));
1101 	lua_pushcclosure(state, zcp_debug_info.func, 0);
1102 	lua_setfield(state, -2, zcp_debug_info.name);
1103 	lua_pushcclosure(state, zcp_exists_info.func, 0);
1104 	lua_setfield(state, -2, zcp_exists_info.name);
1105 	lua_setglobal(state, "zfs");
1106 	VERIFY0(lua_gettop(state));
1107 
1108 	/*
1109 	 * Push the error-callback that calculates Lua stack traces on
1110 	 * unexpected failures.
1111 	 */
1112 	lua_pushcfunction(state, zcp_error_handler);
1113 	VERIFY3U(1, ==, lua_gettop(state));
1114 
1115 	/*
1116 	 * Load the actual script as a function onto the stack as text ("t").
1117 	 * The only valid error condition is a syntax error in the script.
1118 	 * ERRMEM should not be possible because our allocator is using
1119 	 * KM_SLEEP.  ERRGCMM should not be possible because we have not added
1120 	 * any objects with __gc metamethods to the interpreter that could
1121 	 * fail.
1122 	 */
1123 	err = luaL_loadbufferx(state, program, strlen(program),
1124 	    "channel program", "t");
1125 	if (err == LUA_ERRSYNTAX) {
1126 		fnvlist_add_string(outnvl, ZCP_RET_ERROR,
1127 		    lua_tostring(state, -1));
1128 		lua_close(state);
1129 		return (SET_ERROR(EINVAL));
1130 	}
1131 	VERIFY0(err);
1132 	VERIFY3U(2, ==, lua_gettop(state));
1133 
1134 	/*
1135 	 * Convert the input nvlist to a Lua object and put it on top of the
1136 	 * stack.
1137 	 */
1138 	char errmsg[128];
1139 	err = zcp_nvpair_value_to_lua(state, nvarg,
1140 	    errmsg, sizeof (errmsg));
1141 	if (err != 0) {
1142 		fnvlist_add_string(outnvl, ZCP_RET_ERROR, errmsg);
1143 		lua_close(state);
1144 		return (SET_ERROR(EINVAL));
1145 	}
1146 	VERIFY3U(3, ==, lua_gettop(state));
1147 
1148 	runinfo.zri_state = state;
1149 	runinfo.zri_allocargs = &allocargs;
1150 	runinfo.zri_outnvl = outnvl;
1151 	runinfo.zri_result = 0;
1152 	runinfo.zri_cred = CRED();
1153 	runinfo.zri_proc = curproc;
1154 	runinfo.zri_timed_out = B_FALSE;
1155 	runinfo.zri_canceled = B_FALSE;
1156 	runinfo.zri_sync = sync;
1157 	runinfo.zri_space_used = 0;
1158 	runinfo.zri_curinstrs = 0;
1159 	runinfo.zri_maxinstrs = instrlimit;
1160 	runinfo.zri_new_zvols = fnvlist_alloc();
1161 
1162 	if (sync) {
1163 		err = dsl_sync_task_sig(poolname, NULL, zcp_eval_sync,
1164 		    zcp_eval_sig, &runinfo, 0, ZFS_SPACE_CHECK_ZCP_EVAL);
1165 		if (err != 0)
1166 			zcp_pool_error(&runinfo, poolname);
1167 	} else {
1168 		zcp_eval_open(&runinfo, poolname);
1169 	}
1170 	lua_close(state);
1171 
1172 	/*
1173 	 * Create device minor nodes for any new zvols.
1174 	 */
1175 	for (nvpair_t *pair = nvlist_next_nvpair(runinfo.zri_new_zvols, NULL);
1176 	    pair != NULL;
1177 	    pair = nvlist_next_nvpair(runinfo.zri_new_zvols, pair)) {
1178 		zvol_create_minor(nvpair_name(pair));
1179 	}
1180 	fnvlist_free(runinfo.zri_new_zvols);
1181 
1182 	return (runinfo.zri_result);
1183 }
1184 
1185 /*
1186  * Retrieve metadata about the currently running channel program.
1187  */
1188 zcp_run_info_t *
1189 zcp_run_info(lua_State *state)
1190 {
1191 	zcp_run_info_t *ri;
1192 
1193 	lua_getfield(state, LUA_REGISTRYINDEX, ZCP_RUN_INFO_KEY);
1194 	ri = lua_touserdata(state, -1);
1195 	lua_pop(state, 1);
1196 	return (ri);
1197 }
1198 
1199 /*
1200  * Argument Parsing
1201  * ================
1202  *
1203  * The Lua language allows methods to be called with any number
1204  * of arguments of any type. When calling back into ZFS we need to sanitize
1205  * arguments from channel programs to make sure unexpected arguments or
1206  * arguments of the wrong type result in clear error messages. To do this
1207  * in a uniform way all callbacks from channel programs should use the
1208  * zcp_parse_args() function to interpret inputs.
1209  *
1210  * Positional vs Keyword Arguments
1211  * ===============================
1212  *
1213  * Every callback function takes a fixed set of required positional arguments
1214  * and optional keyword arguments. For example, the destroy function takes
1215  * a single positional string argument (the name of the dataset to destroy)
1216  * and an optional "defer" keyword boolean argument. When calling lua functions
1217  * with parentheses, only positional arguments can be used:
1218  *
1219  *     zfs.sync.snapshot("rpool@snap")
1220  *
1221  * To use keyword arguments functions should be called with a single argument
1222  * that is a lua table containing mappings of integer -> positional arguments
1223  * and string -> keyword arguments:
1224  *
1225  *     zfs.sync.snapshot({1="rpool@snap", defer=true})
1226  *
1227  * The lua language allows curly braces to be used in place of parenthesis as
1228  * syntactic sugar for this calling convention:
1229  *
1230  *     zfs.sync.snapshot{"rpool@snap", defer=true}
1231  */
1232 
1233 /*
1234  * Throw an error and print the given arguments.  If there are too many
1235  * arguments to fit in the output buffer, only the error format string is
1236  * output.
1237  */
1238 static void
1239 zcp_args_error(lua_State *state, const char *fname, const zcp_arg_t *pargs,
1240     const zcp_arg_t *kwargs, const char *fmt, ...)
1241 {
1242 	int i;
1243 	char errmsg[512];
1244 	size_t len = sizeof (errmsg);
1245 	size_t msglen = 0;
1246 	va_list argp;
1247 
1248 	va_start(argp, fmt);
1249 	VERIFY3U(len, >, vsnprintf(errmsg, len, fmt, argp));
1250 	va_end(argp);
1251 
1252 	/*
1253 	 * Calculate the total length of the final string, including extra
1254 	 * formatting characters. If the argument dump would be too large,
1255 	 * only print the error string.
1256 	 */
1257 	msglen = strlen(errmsg);
1258 	msglen += strlen(fname) + 4; /* : + {} + null terminator */
1259 	for (i = 0; pargs[i].za_name != NULL; i++) {
1260 		msglen += strlen(pargs[i].za_name);
1261 		msglen += strlen(lua_typename(state, pargs[i].za_lua_type));
1262 		if (pargs[i + 1].za_name != NULL || kwargs[0].za_name != NULL)
1263 			msglen += 5; /* < + ( + )> + , */
1264 		else
1265 			msglen += 4; /* < + ( + )> */
1266 	}
1267 	for (i = 0; kwargs[i].za_name != NULL; i++) {
1268 		msglen += strlen(kwargs[i].za_name);
1269 		msglen += strlen(lua_typename(state, kwargs[i].za_lua_type));
1270 		if (kwargs[i + 1].za_name != NULL)
1271 			msglen += 4; /* =( + ) + , */
1272 		else
1273 			msglen += 3; /* =( + ) */
1274 	}
1275 
1276 	if (msglen >= len)
1277 		(void) luaL_error(state, errmsg);
1278 
1279 	VERIFY3U(len, >, strlcat(errmsg, ": ", len));
1280 	VERIFY3U(len, >, strlcat(errmsg, fname, len));
1281 	VERIFY3U(len, >, strlcat(errmsg, "{", len));
1282 	for (i = 0; pargs[i].za_name != NULL; i++) {
1283 		VERIFY3U(len, >, strlcat(errmsg, "<", len));
1284 		VERIFY3U(len, >, strlcat(errmsg, pargs[i].za_name, len));
1285 		VERIFY3U(len, >, strlcat(errmsg, "(", len));
1286 		VERIFY3U(len, >, strlcat(errmsg,
1287 		    lua_typename(state, pargs[i].za_lua_type), len));
1288 		VERIFY3U(len, >, strlcat(errmsg, ")>", len));
1289 		if (pargs[i + 1].za_name != NULL || kwargs[0].za_name != NULL) {
1290 			VERIFY3U(len, >, strlcat(errmsg, ", ", len));
1291 		}
1292 	}
1293 	for (i = 0; kwargs[i].za_name != NULL; i++) {
1294 		VERIFY3U(len, >, strlcat(errmsg, kwargs[i].za_name, len));
1295 		VERIFY3U(len, >, strlcat(errmsg, "=(", len));
1296 		VERIFY3U(len, >, strlcat(errmsg,
1297 		    lua_typename(state, kwargs[i].za_lua_type), len));
1298 		VERIFY3U(len, >, strlcat(errmsg, ")", len));
1299 		if (kwargs[i + 1].za_name != NULL) {
1300 			VERIFY3U(len, >, strlcat(errmsg, ", ", len));
1301 		}
1302 	}
1303 	VERIFY3U(len, >, strlcat(errmsg, "}", len));
1304 
1305 	(void) luaL_error(state, errmsg);
1306 	panic("unreachable code");
1307 }
1308 
1309 static void
1310 zcp_parse_table_args(lua_State *state, const char *fname,
1311     const zcp_arg_t *pargs, const zcp_arg_t *kwargs)
1312 {
1313 	int i;
1314 	int type;
1315 
1316 	for (i = 0; pargs[i].za_name != NULL; i++) {
1317 		/*
1318 		 * Check the table for this positional argument, leaving it
1319 		 * on the top of the stack once we finish validating it.
1320 		 */
1321 		lua_pushinteger(state, i + 1);
1322 		lua_gettable(state, 1);
1323 
1324 		type = lua_type(state, -1);
1325 		if (type == LUA_TNIL) {
1326 			zcp_args_error(state, fname, pargs, kwargs,
1327 			    "too few arguments");
1328 			panic("unreachable code");
1329 		} else if (type != pargs[i].za_lua_type) {
1330 			zcp_args_error(state, fname, pargs, kwargs,
1331 			    "arg %d wrong type (is '%s', expected '%s')",
1332 			    i + 1, lua_typename(state, type),
1333 			    lua_typename(state, pargs[i].za_lua_type));
1334 			panic("unreachable code");
1335 		}
1336 
1337 		/*
1338 		 * Remove the positional argument from the table.
1339 		 */
1340 		lua_pushinteger(state, i + 1);
1341 		lua_pushnil(state);
1342 		lua_settable(state, 1);
1343 	}
1344 
1345 	for (i = 0; kwargs[i].za_name != NULL; i++) {
1346 		/*
1347 		 * Check the table for this keyword argument, which may be
1348 		 * nil if it was omitted. Leave the value on the top of
1349 		 * the stack after validating it.
1350 		 */
1351 		lua_getfield(state, 1, kwargs[i].za_name);
1352 
1353 		type = lua_type(state, -1);
1354 		if (type != LUA_TNIL && type != kwargs[i].za_lua_type) {
1355 			zcp_args_error(state, fname, pargs, kwargs,
1356 			    "kwarg '%s' wrong type (is '%s', expected '%s')",
1357 			    kwargs[i].za_name, lua_typename(state, type),
1358 			    lua_typename(state, kwargs[i].za_lua_type));
1359 			panic("unreachable code");
1360 		}
1361 
1362 		/*
1363 		 * Remove the keyword argument from the table.
1364 		 */
1365 		lua_pushnil(state);
1366 		lua_setfield(state, 1, kwargs[i].za_name);
1367 	}
1368 
1369 	/*
1370 	 * Any entries remaining in the table are invalid inputs, print
1371 	 * an error message based on what the entry is.
1372 	 */
1373 	lua_pushnil(state);
1374 	if (lua_next(state, 1)) {
1375 		if (lua_isnumber(state, -2) && lua_tointeger(state, -2) > 0) {
1376 			zcp_args_error(state, fname, pargs, kwargs,
1377 			    "too many positional arguments");
1378 		} else if (lua_isstring(state, -2)) {
1379 			zcp_args_error(state, fname, pargs, kwargs,
1380 			    "invalid kwarg '%s'", lua_tostring(state, -2));
1381 		} else {
1382 			zcp_args_error(state, fname, pargs, kwargs,
1383 			    "kwarg keys must be strings");
1384 		}
1385 		panic("unreachable code");
1386 	}
1387 
1388 	lua_remove(state, 1);
1389 }
1390 
1391 static void
1392 zcp_parse_pos_args(lua_State *state, const char *fname, const zcp_arg_t *pargs,
1393     const zcp_arg_t *kwargs)
1394 {
1395 	int i;
1396 	int type;
1397 
1398 	for (i = 0; pargs[i].za_name != NULL; i++) {
1399 		type = lua_type(state, i + 1);
1400 		if (type == LUA_TNONE) {
1401 			zcp_args_error(state, fname, pargs, kwargs,
1402 			    "too few arguments");
1403 			panic("unreachable code");
1404 		} else if (type != pargs[i].za_lua_type) {
1405 			zcp_args_error(state, fname, pargs, kwargs,
1406 			    "arg %d wrong type (is '%s', expected '%s')",
1407 			    i + 1, lua_typename(state, type),
1408 			    lua_typename(state, pargs[i].za_lua_type));
1409 			panic("unreachable code");
1410 		}
1411 	}
1412 	if (lua_gettop(state) != i) {
1413 		zcp_args_error(state, fname, pargs, kwargs,
1414 		    "too many positional arguments");
1415 		panic("unreachable code");
1416 	}
1417 
1418 	for (i = 0; kwargs[i].za_name != NULL; i++) {
1419 		lua_pushnil(state);
1420 	}
1421 }
1422 
1423 /*
1424  * Checks the current Lua stack against an expected set of positional and
1425  * keyword arguments. If the stack does not match the expected arguments
1426  * aborts the current channel program with a useful error message, otherwise
1427  * it re-arranges the stack so that it contains the positional arguments
1428  * followed by the keyword argument values in declaration order. Any missing
1429  * keyword argument will be represented by a nil value on the stack.
1430  *
1431  * If the stack contains exactly one argument of type LUA_TTABLE the curly
1432  * braces calling convention is assumed, otherwise the stack is parsed for
1433  * positional arguments only.
1434  *
1435  * This function should be used by every function callback. It should be called
1436  * before the callback manipulates the Lua stack as it assumes the stack
1437  * represents the function arguments.
1438  */
1439 void
1440 zcp_parse_args(lua_State *state, const char *fname, const zcp_arg_t *pargs,
1441     const zcp_arg_t *kwargs)
1442 {
1443 	if (lua_gettop(state) == 1 && lua_istable(state, 1)) {
1444 		zcp_parse_table_args(state, fname, pargs, kwargs);
1445 	} else {
1446 		zcp_parse_pos_args(state, fname, pargs, kwargs);
1447 	}
1448 }
1449 
1450 /* BEGIN CSTYLED */
1451 ZFS_MODULE_PARAM(zfs_lua, zfs_lua_, max_instrlimit, ULONG, ZMOD_RW,
1452 	"Max instruction limit that can be specified for a channel program");
1453 
1454 ZFS_MODULE_PARAM(zfs_lua, zfs_lua_, max_memlimit, ULONG, ZMOD_RW,
1455 	"Max memory limit that can be specified for a channel program");
1456 /* END CSTYLED */
1457