Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
8677 Open-Context Channel Programs
Browse files Browse the repository at this point in the history
Reviewed by: Matt Ahrens <mahrens@delphix.com>
Reviewed by: Chris Williamson <chris.williamson@delphix.com>
Reviewed by: Pavel Zakharov <pavel.zakharov@delphix.com>

We want to be able to run channel programs outside of synching context.
This would greatly improve performance for channel programs that just gather
information, as they won't have to wait for synching context anymore.

=== What is implemented?

This feature introduces the following:
- A new command line flag in "zfs program" to specify our intention
  to run in open context. (The -n option)
- A new flag/option within the channel program ioctl which selects
  the context.
- Appropriate error handling whenever we try a channel program in
  open-context that contains zfs.sync* expressions.
- Documentation for the new feature in the manual pages.

=== How do we handle zfs.sync functions in open context?

When such a function is found by the interpreter and we are running
in open context we abort the script and we spit out a descriptive
runtime error. For example, given the script below ...

arg = ...
fs = arg["argv"][1]
err = zfs.sync.destroy(fs)
msg = "destroying " .. fs .. " err=" .. err
return msg

if we run it in open context, we will get back the following error:

Channel program execution failed:
[string "channel program"]:3: running functions from the zfs.sync submodule requires passing sync=TRUE to lzc_channel_program() (i.e. do not specify the "-n" command line argument)
stack traceback:
        [C]: in function 'destroy'
        [string "channel program"]:3: in main chunk

=== What about testing?

We've introduced new wrappers for all channel program tests that
run each channel program as both (startard & open-context) and
expect the appropriate behavior depending on the program using
the zfs.sync module.

Closes #469
  • Loading branch information
sdimitro authored and prakashsurya committed Nov 17, 2017
1 parent 79cd17b commit 17a49e1
Show file tree
Hide file tree
Showing 26 changed files with 411 additions and 124 deletions.
18 changes: 14 additions & 4 deletions usr/src/cmd/zfs/zfs_main.c
Expand Up @@ -329,7 +329,7 @@ get_usage(zfs_help_t idx)
case HELP_BOOKMARK:
return (gettext("\tbookmark <snapshot> <bookmark>\n"));
case HELP_CHANNEL_PROGRAM:
return (gettext("\tprogram [-t <instruction limit>] "
return (gettext("\tprogram [-n] [-t <instruction limit>] "
"[-m <memory limit (b)>] <pool> <program file> "
"[lua args...]\n"));
}
Expand Down Expand Up @@ -7009,11 +7009,12 @@ zfs_do_channel_program(int argc, char **argv)
nvlist_t *outnvl;
uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
boolean_t sync_flag = B_TRUE;
zpool_handle_t *zhp;

/* check options */
while (-1 !=
(c = getopt(argc, argv, "t:(instr-limit)m:(memory-limit)"))) {
(c = getopt(argc, argv, "nt:(instr-limit)m:(memory-limit)"))) {
switch (c) {
case 't':
case 'm': {
Expand Down Expand Up @@ -7051,6 +7052,10 @@ zfs_do_channel_program(int argc, char **argv)
}
break;
}
case 'n': {
sync_flag = B_FALSE;
break;
}
case '?':
(void) fprintf(stderr, gettext("invalid option '%c'\n"),
optopt);
Expand Down Expand Up @@ -7122,8 +7127,13 @@ zfs_do_channel_program(int argc, char **argv)
nvlist_t *argnvl = fnvlist_alloc();
fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);

ret = lzc_channel_program(poolname, progbuf, instrlimit, memlimit,
argnvl, &outnvl);
if (sync_flag) {
ret = lzc_channel_program(poolname, progbuf,
instrlimit, memlimit, argnvl, &outnvl);
} else {
ret = lzc_channel_program_nosync(poolname, progbuf,
instrlimit, memlimit, argnvl, &outnvl);
}

if (ret != 0) {
/*
Expand Down
2 changes: 1 addition & 1 deletion usr/src/lib/libzfs/common/libzfs_dataset.c
Expand Up @@ -2348,7 +2348,7 @@ zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));

error = lzc_channel_program(poolname, program,
error = lzc_channel_program_nosync(poolname, program,
10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);

if (error == 0) {
Expand Down
51 changes: 40 additions & 11 deletions usr/src/lib/libzfs_core/common/libzfs_core.c
Expand Up @@ -881,6 +881,25 @@ lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
return (error);
}

static int
lzc_channel_program_impl(const char *pool, const char *program, boolean_t sync,
uint64_t instrlimit, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
{
int error;
nvlist_t *args;

args = fnvlist_alloc();
fnvlist_add_string(args, ZCP_ARG_PROGRAM, program);
fnvlist_add_nvlist(args, ZCP_ARG_ARGLIST, argnvl);
fnvlist_add_boolean_value(args, ZCP_ARG_SYNC, sync);
fnvlist_add_uint64(args, ZCP_ARG_INSTRLIMIT, instrlimit);
fnvlist_add_uint64(args, ZCP_ARG_MEMLIMIT, memlimit);
error = lzc_ioctl(ZFS_IOC_CHANNEL_PROGRAM, pool, args, outnvl);
fnvlist_free(args);

return (error);
}

/*
* Executes a channel program.
*
Expand Down Expand Up @@ -918,16 +937,26 @@ int
lzc_channel_program(const char *pool, const char *program, uint64_t instrlimit,
uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
{
int error;
nvlist_t *args;

args = fnvlist_alloc();
fnvlist_add_string(args, ZCP_ARG_PROGRAM, program);
fnvlist_add_nvlist(args, ZCP_ARG_ARGLIST, argnvl);
fnvlist_add_uint64(args, ZCP_ARG_INSTRLIMIT, instrlimit);
fnvlist_add_uint64(args, ZCP_ARG_MEMLIMIT, memlimit);
error = lzc_ioctl(ZFS_IOC_CHANNEL_PROGRAM, pool, args, outnvl);
fnvlist_free(args);
return (lzc_channel_program_impl(pool, program, B_TRUE, instrlimit,
memlimit, argnvl, outnvl));
}

return (error);
/*
* Executes a read-only channel program.
*
* A read-only channel program works programmatically the same way as a
* normal channel program executed with lzc_channel_program(). The only
* difference is it runs exclusively in open-context and therefore can
* return faster. The downside to that, is that the program cannot change
* on-disk state by calling functions from the zfs.sync submodule.
*
* The return values of this function (and their meaning) are exactly the
* same as the ones described in lzc_channel_program().
*/
int
lzc_channel_program_nosync(const char *pool, const char *program,
uint64_t timeout, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
{
return (lzc_channel_program_impl(pool, program, B_FALSE, timeout,
memlimit, argnvl, outnvl));
}
6 changes: 4 additions & 2 deletions usr/src/lib/libzfs_core/common/libzfs_core.h
Expand Up @@ -86,8 +86,10 @@ boolean_t lzc_exists(const char *);
int lzc_rollback(const char *, char *, int);
int lzc_rollback_to(const char *, const char *);

int lzc_channel_program(const char *, const char *, uint64_t, uint64_t,
nvlist_t *, nvlist_t **);
int lzc_channel_program(const char *, const char *, uint64_t,
uint64_t, nvlist_t *, nvlist_t **);
int lzc_channel_program_nosync(const char *, const char *, uint64_t,
uint64_t, nvlist_t *, nvlist_t **);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions usr/src/lib/libzfs_core/common/mapfile-vers
Expand Up @@ -44,6 +44,7 @@ SYMBOL_VERSION ILLUMOS_0.1 {
libzfs_core_init;
lzc_bookmark;
lzc_channel_program;
lzc_channel_program_nosync;
lzc_clone;
lzc_promote;
lzc_create;
Expand Down
9 changes: 9 additions & 0 deletions usr/src/man/man1m/zfs-program.1m
Expand Up @@ -18,6 +18,7 @@
.Nd executes ZFS channel programs
.Sh SYNOPSIS
.Cm "zfs program"
.Op Fl n
.Op Fl t Ar instruction-limit
.Op Fl m Ar memory-limit
.Ar pool
Expand Down Expand Up @@ -45,6 +46,14 @@ will be run on
and any attempts to access or modify other pools will cause an error.
.Sh OPTIONS
.Bl -tag -width "-t"
.It Fl n
Executes a read-only channel program, which runs faster.
The program cannot change on-disk state by calling functions from the
zfs.sync submodule.
The program can be used to gather information such as properties and
determining if changes would succeed (zfs.check.*).
Without this flag, all pending changes must be synced to disk before a
channel program can complete.
.It Fl t Ar instruction-limit
Execution time limit, in number of Lua instructions to execute.
If a channel program executes more than the specified number of instructions,
Expand Down
11 changes: 10 additions & 1 deletion usr/src/man/man1m/zfs.1m
Expand Up @@ -267,6 +267,7 @@
.Ar snapshot Ar snapshot Ns | Ns Ar filesystem
.Nm
.Cm program
.Op Fl n
.Op Fl t Ar timeout
.Op Fl m Ar memory_limit
.Ar pool script
Expand Down Expand Up @@ -3408,6 +3409,7 @@ Display the path's inode change time as the first column of output.
.It Xo
.Nm
.Cm program
.Op Fl n
.Op Fl t Ar timeout
.Op Fl m Ar memory_limit
.Ar pool script
Expand All @@ -3427,8 +3429,15 @@ Channel programs may only be run with root privileges.
.sp
For full documentation of the ZFS channel program interface, see the manual
page for
.Xr zfs-program 1M .
.Bl -tag -width ""
.It Fl n
Executes a read-only channel program, which runs faster.
The program cannot change on-disk state by calling functions from
the zfs.sync submodule.
The program can be used to gather information such as properties and
determining if changes would succeed (zfs.check.*).
Without this flag, all pending changes must be synced to disk before
a channel program can complete.
.It Fl t Ar timeout
Execution time limit, in milliseconds.
If a channel program executes for longer than the provided timeout, it will
Expand Down

0 comments on commit 17a49e1

Please sign in to comment.