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