Update Red Hat Copyright Notices
[nbdkit.git] / plugins / tcl / nbdkit-tcl-plugin.pod
blob684107bac3b8e1465a3ae76a6260b14157ba30fd
1 =head1 NAME
3 nbdkit-tcl-plugin - nbdkit Tcl plugin
5 =head1 SYNOPSIS
7  nbdkit tcl /path/to/plugin.tcl [arguments...]
9 =head1 DESCRIPTION
11 C<nbdkit-tcl-plugin> is an embedded Tcl interpreter for
12 L<nbdkit(1)>, allowing you to write nbdkit plugins in Tcl.
14 =head2 If you have been given an nbdkit Tcl plugin
16 Assuming you have a Tcl script which is an nbdkit plugin, you run it
17 like this:
19  nbdkit tcl /path/to/plugin.tcl
21 You may have to add further C<key=value> arguments to the command
22 line.  Read the Tcl script to see if it requires any.
24 =head1 WRITING A TCL NBDKIT PLUGIN
26 For an example plugin written in Tcl, see:
27 L<https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/tcl/example.tcl>
29 Broadly speaking, Tcl nbdkit plugins work like C ones, so you should
30 read L<nbdkit-plugin(3)> first.
32 To write a Tcl nbdkit plugin, you create a Tcl file which contains
33 at least the following required subroutines:
35  proc plugin_open {readonly} {
36      # see below
37      return $h
38  }
39  proc get_size {h} {
40      # see below
41      return $size
42  }
43  proc pread {h count offset} {
44      # see below
45      return $buf
46  }
48 Note that the subroutines must have those literal names (like
49 C<plugin_open>), because the C part looks up and calls those functions
50 directly.  You may want to include documentation and globals (eg. for
51 storing global state).  Also any top-level statements are run when
52 nbdkit starts up.
54 =head2 Executable script
56 If you want you can make the script executable and include a "shebang"
57 at the top:
59  #!/usr/sbin/nbdkit tcl
61 See also L<nbdkit(1)/Shebang scripts>.
63 These scripts can also be installed in the C<$plugindir>.  See
64 L<nbdkit-plugin(3)/WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES>.
66 =head2 Exceptions
68 Tcl plugin methods can indicate an error by calling C<error>.
70 =head2 Binary data
72 When writing your Tcl script, be careful to ensure that it is
73 processing binary data (not Unicode).  If reading and writing from
74 local disk files, you should use:
76  fconfigure $fp -translation binary
78 Note also that the value returned from C<pread> should convertible to
79 a byte array, and the buffer passed to C<pwrite> is also a byte array.
81 See also: L<https://wiki.tcl.tk/1180>
83 =head2 Tcl callbacks
85 This just documents the arguments to the callbacks in Tcl, and any
86 way that they differ from the C callbacks.  In all other respects they
87 work the same way as the C callbacks, so you should go and read
88 L<nbdkit-plugin(3)>.
90 =over 4
92 =item C<dump_plugin>
94 (Optional)
96 There are no arguments or return value.
98 =item C<config>
100 (Optional)
102  proc config {key value} {
103      # No return value.
106 =item C<config_complete>
108 (Optional)
110 There are no arguments or return value.
112 =item C<plugin_open>
114 (Required)
116  proc plugin_open {readonly} {
117      set handle ...
118      return $handle
121 The C<readonly> flag is a boolean.
123 You can return any Tcl string or object as the handle.  It is passed
124 back to subsequent calls.
126 =item C<plugin_close>
128 (Optional)
130  proc plugin_close {h} {
131      # No return value
134 After C<plugin_close> returns, the reference count of the handle is
135 decremented in the C part, which usually means that the handle and its
136 contents will be garbage collected.
138 =item C<get_size>
140 (Required)
142  proc get_size {h} {
143      set size .. the size of the disk ..
144      return $size
147 This returns the size of the disk.
149 =item C<can_write>
151 (Optional)
153  proc can_write {h} {
154      return $bool
157 Return a boolean indicating whether the disk is writable.
159 =item C<can_flush>
161 (Optional)
163  proc can_flush {h} {
164      return $bool
167 Return a boolean indicating whether flush can be performed.
169 =item C<is_rotational>
171 (Optional)
173  proc is_rotational {h} {
174      return $bool
177 Return a boolean indicating whether the disk is rotational.
179 =item C<can_trim>
181 (Optional)
183  proc can_trim {h} {
184      return $bool
187 Return a boolean indicating whether trim/discard can be performed.
189 =item C<pread>
191 (Required)
193  proc pread {h count offset} {
194     # Construct a buffer of length $count bytes and return it.
195     return $buf
198 The body of your C<pread> function should construct a buffer of length
199 (at least) C<$count> bytes.  You should read C<$count> bytes from the
200 disk starting at C<$offset>.
202 NBD only supports whole reads, so your function should try to read the
203 whole region (perhaps requiring a loop).  If the read fails or is
204 partial, your function should call C<error>.
206 =item C<pwrite>
208 (Optional)
210  proc pwrite {h buf offset} {
211     # No return value
214 The body of your C<pwrite> function should write the C<$buf> string to
215 the disk.  You should write C<$count> bytes to the disk starting at
216 C<$offset>.
218 NBD only supports whole writes, so your function should try to write
219 the whole region (perhaps requiring a loop).  If the write fails or is
220 partial, your function should call C<error>.
222 =item C<plugin_flush>
224 (Optional)
226  proc plugin_flush {h} {
227      # No return value
230 The body of your C<plugin_flush> function should do a L<sync(2)> or
231 L<fdatasync(2)> or equivalent on the backing store.
233 =item C<trim>
235 (Optional)
237  proc trim {h count offset} {
238      # No return value
241 The body of your C<trim> function should "punch a hole" in the backing
242 store.
244 =item C<zero>
246 (Optional)
248  proc zero {h count offset may_trim} {
249     # No return value
252 The body of your C<zero> function should ensure that C<$count> bytes
253 of the disk, starting at C<$offset>, will read back as zero.  If
254 C<$may_trim> is true, the operation may be optimized as a trim as long
255 as subsequent reads see zeroes.
257 NBD only supports whole writes, so your function should try to write
258 the whole region (perhaps requiring a loop).  If the write fails or is
259 partial, your function should call C<error>.
261 =back
263 =head2 Missing callbacks
265 =over 4
267 =item Missing: C<load>, C<unload>, C<name>, C<version>, C<longname>,
268 C<description>, C<config_help>, C<can_zero>, C<can_fua>, C<can_cache>,
269 C<cache>
271 These are not yet supported.
273 =back
275 =head2 Threads
277 The thread model for Tcl callbacks currently cannot be set from Tcl.
278 It is hard-coded in the C part to
279 C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>.  This may change or be
280 settable in future.
282 =head1 FILES
284 =over 4
286 =item F<$plugindir/nbdkit-tcl-plugin.so>
288 The plugin.
290 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
292 =back
294 =head1 VERSION
296 C<nbdkit-tcl-plugin> first appeared in nbdkit 1.4.
298 =head1 SEE ALSO
300 L<nbdkit(1)>,
301 L<nbdkit-plugin(3)>.
303 =head1 AUTHORS
305 Richard W.M. Jones
307 =head1 COPYRIGHT
309 Copyright Red Hat