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