2 * Copyright (C) 2013 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * A simple read-write filesystem which stores all changes in
36 * a temporary file that is thrown away after each connection.
48 #include <nbdkit-plugin.h>
50 #ifndef HAVE_FDATASYNC
51 #define fdatasync fsync
54 /* The size of disk in bytes (initialized by size=<SIZE> parameter).
55 * If size parameter is not specified, it defaults to 100M.
57 static uint64_t size
= 100 * 1024 * 1024;
59 /* If you want to display extra information about the plugin when
60 * the user does ‘nbdkit example3 --dump-plugin’ then you can print
61 * ‘key=value’ lines here.
64 example3_dump_plugin (void)
66 printf ("example3_extra=hello\n");
69 /* Called for each key=value passed on the command line. This plugin
70 * only accepts optional size=<SIZE> parameter.
73 example3_config (const char *key
, const char *value
)
77 if (strcmp (key
, "size") == 0) {
78 r
= nbdkit_parse_size (value
);
84 nbdkit_error ("unknown parameter '%s'", key
);
91 #define example3_config_help \
92 "size=<SIZE> (optional) Size of the backing disk (default: 100M)"
94 /* The per-connection handle. */
95 struct example3_handle
{
99 /* Create the per-connection handle.
101 * Note we ignore the 'readonly' parameter. In effect this means that
102 * we ignore the -r option on the nbdkit command line, which sort of
103 * makes sense for this particular plugin.
106 example3_open (int readonly
)
108 struct example3_handle
*h
;
109 char template[] = LARGE_TMPDIR
"/diskXXXXXX";
111 h
= malloc (sizeof *h
);
113 nbdkit_error ("malloc: %m");
117 h
->fd
= mkstemp (template);
119 nbdkit_error ("mkstemp: %s: %m", template);
126 /* This creates a raw-format sparse file of the required size. */
127 if (ftruncate (h
->fd
, size
) == -1) {
128 nbdkit_error ("ftruncate: %m");
137 /* Free up the per-connection handle. */
139 example3_close (void *handle
)
141 struct example3_handle
*h
= handle
;
147 /* In fact NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS would work here.
148 * However for the benefit of people who blindly cut and paste code
149 * without bothering to read any documentation, leave this at a safe
152 #define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
154 /* Get the file size. */
156 example3_get_size (void *handle
)
158 return (int64_t) size
;
161 /* Read data from the file. */
163 example3_pread (void *handle
, void *buf
, uint32_t count
, uint64_t offset
)
165 struct example3_handle
*h
= handle
;
168 ssize_t r
= pread (h
->fd
, buf
, count
, offset
);
170 nbdkit_error ("pead: %m");
174 nbdkit_error ("pread: unexpected end of file");
185 /* Write data to the file. */
187 example3_pwrite (void *handle
, const void *buf
, uint32_t count
, uint64_t offset
)
189 struct example3_handle
*h
= handle
;
192 ssize_t r
= pwrite (h
->fd
, buf
, count
, offset
);
194 nbdkit_error ("pwrite: %m");
205 /* Flush the file to disk. */
207 example3_flush (void *handle
)
209 struct example3_handle
*h
= handle
;
211 if (fdatasync (h
->fd
) == -1) {
212 nbdkit_error ("fdatasync: %m");
219 static struct nbdkit_plugin plugin
= {
221 .version
= PACKAGE_VERSION
,
222 .dump_plugin
= example3_dump_plugin
,
223 .config
= example3_config
,
224 .config_help
= example3_config_help
,
225 .open
= example3_open
,
226 .close
= example3_close
,
227 .get_size
= example3_get_size
,
228 .pread
= example3_pread
,
229 .pwrite
= example3_pwrite
,
230 .flush
= example3_flush
,
231 /* In this plugin, errno is preserved properly along error return
232 * paths from failed system calls.
234 .errno_is_preserved
= 1,
237 NBDKIT_REGISTER_PLUGIN(plugin
)