=head1 NAME

nbdkit-ruby-plugin - nbdkit ruby plugin

=head1 SYNOPSIS

 nbdkit ruby /path/to/plugin.rb [arguments...]

=head1 WARNING

The Ruby language is fundamentally broken when it comes to embedding
in a program which uses pthreads.  This means you may see random
"stack overflows" when using this plugin on some versions of Ruby but
not others.

For the whole sorry saga, see:
L<https://redmine.ruby-lang.org/issues/2294>

=head1 DESCRIPTION

C<nbdkit-ruby-plugin> is an embedded Ruby interpreter for
L<nbdkit(1)>, allowing you to write nbdkit plugins in Ruby.

=head2 If you have been given an nbdkit Ruby plugin

Assuming you have a Ruby script which is an nbdkit plugin, you run it
like this:

 nbdkit ruby /path/to/ruby.rb

You may have to add further C<key=value> arguments to the command
line.  Read the Ruby script to see if it requires any.

=head1 WRITING A RUBY NBDKIT PLUGIN

For an example plugin written in Ruby, see:
L<https://github.com/libguestfs/nbdkit/blob/master/plugins/ruby/example.rb>

Broadly speaking, Ruby nbdkit plugins work like C ones, so you should
read L<nbdkit-plugin(3)> first.

To write a Ruby nbdkit plugin, you create a Ruby file which
contains at least the following required functions:

 def open(readonly)
   # see below
 end
 def get_size(h)
   # see below
 end
 def pread(h, count, offset)
   # see below
 end

Note that the subroutines must have those literal names (like C<open>),
because the C part looks up and calls those functions directly.  You
may want to include documentation and globals (eg. for storing global
state).  Any other top level statements are run when the script is
loaded, just like ordinary Ruby.

=head2 Executable script

If you want you can make the script executable and include a "shebang"
at the top:

 #!/usr/sbin/nbdkit ruby

See also L<nbdkit(1)/Shebang scripts>.

These scripts can also be installed in the C<$plugindir>.  See
L<nbdkit-plugin(3)/WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES>.

=head2 Methods

Your script has access to the C<Nbdkit> module, with the following
singleton methods:

 Nbdkit.set_error(err)

Record C<err> as the reason you are about to raise an
exception. C<err> should either be a class that defines an C<Errno>
constant (all of the subclasses of C<SystemCallError> in module
C<Errno> have this property), an object that defines an C<errno>
method with no arguments (all instances of C<SystemCallError> have
this property), or an integer value corresponding to the usual errno
values.

=head2 Exceptions

Ruby callbacks should throw exceptions to indicate errors. Remember
to use C<Nbdkit.set_error> if you need to control which error is sent
back to the client; if omitted, the client will see an error of C<EIO>.

=head2 Ruby callbacks

This just documents the arguments to the callbacks in Ruby, and any
way that they differ from the C callbacks.  In all other respects they
work the same way as the C callbacks, so you should go and read
L<nbdkit-plugin(3)>.

=over 4

=item C<dump_plugin>

(Optional)

There are no arguments or return value.

=item C<config>

(Optional)

 def config(key, value)
   # no return value
 end

=item C<config_complete>

(Optional)

There are no arguments or return value.

=item C<open>

(Required)

 def open(readonly)
   # return handle
 end

You can return any non-nil Ruby value as the handle.  It is passed
back in subsequent calls.

=item C<close>

(Optional)

 def close(h)
   # no return value
 end

=item C<get_size>

(Required)

 def get_size(h)
   # return the size of the disk
 end

=item C<can_write>

(Optional)

 def can_write(h)
   # return a boolean
 end

=item C<can_flush>

(Optional)

 def can_flush(h)
   # return a boolean
 end

=item C<is_rotational>

(Optional)

 def is_rotational(h)
   # return a boolean
 end

=item C<can_trim>

(Optional)

 def can_trim(h)
   # return a boolean
 end

=item C<pread>

(Required)

 def pread(h, count, offset)
   # construct a string of length count bytes and return it
 end

The body of your C<pread> function should construct a string of length
(at least) C<count> bytes.  You should read C<count> bytes from the
disk starting at C<offset>.

NBD only supports whole reads, so your function should try to read
the whole region (perhaps requiring a loop).  If the read fails or
is partial, your function should throw an exception, optionally using
C<Nbdkit.set_error> first.

=item C<pwrite>

(Optional)

 def pwrite(h, buf, offset)
   length = buf.length
   # no return value
 end

The body of your C<pwrite> function should write the C<buf> string to
the disk.  You should write C<count> bytes to the disk starting at
C<offset>.

NBD only supports whole writes, so your function should try to
write the whole region (perhaps requiring a loop).  If the write
fails or is partial, your function should throw an exception, optionally
using C<Nbdkit.set_error> first.

=item C<flush>

(Optional)

 def flush(h)
   # no return value
 end

The body of your C<flush> function should do a L<sync(2)> or
L<fdatasync(2)> or equivalent on the backing store.

If the flush fails, your function should throw an exception, optionally
using C<Nbdkit.set_error> first.

=item C<trim>

(Optional)

 def trim(h, count, offset)
   # no return value
 end

The body of your C<trim> function should "punch a hole" in the
backing store.  If the trim fails, your function should throw an
exception, optionally using C<Nbdkit.set_error> first.

=item C<zero>

(Optional)

 def zero(h, count, offset, may_trim)
   # no return value

The body of your C<zero> function should ensure that C<count> bytes
of the disk, starting at C<offset>, will read back as zero.  If
C<may_trim> is true, the operation may be optimized as a trim as long
as subsequent reads see zeroes.

NBD only supports whole writes, so your function should try to
write the whole region (perhaps requiring a loop).  If the write
fails or is partial, your function should throw an exception,
optionally using C<Nbdkit.set_error> first.  In particular, if
you would like to automatically fall back to C<pwrite> (perhaps
because there is nothing to optimize if C<may_trim> is false),
use C<Nbdkit.set_error(Errno::EOPNOTSUPP)>.

=back

=head2 Missing callbacks

=over 4

=item Missing: C<load> and C<unload>

These are not needed because you can just use ordinary Ruby
constructs.

=item Missing: C<name>, C<version>, C<longname>, C<description>,
C<config_help>, C<can_fua>, C<can_cache>, C<cache>

These are not yet supported.

=back

=head2 Threads

The thread model for Ruby callbacks currently cannot be set from Ruby.
It is hard-coded in the C part to
C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>.  This may change or be
settable in future.

=head1 FILES

=over 4

=item F<$plugindir/nbdkit-ruby-plugin.so>

The plugin.

Use C<nbdkit --dump-config> to find the location of C<$plugindir>.

=back

=head1 VERSION

C<nbdkit-ruby-plugin> first appeared in nbdkit 1.2.

=head1 SEE ALSO

L<nbdkit(1)>,
L<nbdkit-plugin(3)>,
L<ruby(1)>.

=head1 AUTHORS

Eric Blake

Richard W.M. Jones

=head1 COPYRIGHT

Copyright (C) 2013-2018 Red Hat Inc.
