python: Implement can_multi_conn.
[nbdkit/ericb.git] / plugins / example3 / example3.c
blob5d2b626caf346d5cef275146aebe2bde905f121b
1 /* nbdkit
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
6 * met:
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
30 * SUCH DAMAGE.
33 /* example3:
35 * A simple read-write filesystem which stores all changes in
36 * a temporary file that is thrown away after each connection.
39 #include <config.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 #include <string.h>
45 #include <fcntl.h>
46 #include <unistd.h>
48 #include <nbdkit-plugin.h>
50 #ifndef HAVE_FDATASYNC
51 #define fdatasync fsync
52 #endif
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.
63 static void
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.
72 static int
73 example3_config (const char *key, const char *value)
75 int64_t r;
77 if (strcmp (key, "size") == 0) {
78 r = nbdkit_parse_size (value);
79 if (r == -1)
80 return -1;
81 size = (uint64_t) r;
83 else {
84 nbdkit_error ("unknown parameter '%s'", key);
85 return -1;
88 return 0;
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 {
96 int fd;
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.
105 static void *
106 example3_open (int readonly)
108 struct example3_handle *h;
109 char template[] = LARGE_TMPDIR "/diskXXXXXX";
111 h = malloc (sizeof *h);
112 if (h == NULL) {
113 nbdkit_error ("malloc: %m");
114 return NULL;
117 h->fd = mkstemp (template);
118 if (h->fd == -1) {
119 nbdkit_error ("mkstemp: %s: %m", template);
120 free (h);
121 return NULL;
124 unlink (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");
129 close (h->fd);
130 free (h);
131 return NULL;
134 return h;
137 /* Free up the per-connection handle. */
138 static void
139 example3_close (void *handle)
141 struct example3_handle *h = handle;
143 close (h->fd);
144 free (h);
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
150 * default.
152 #define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
154 /* Get the file size. */
155 static int64_t
156 example3_get_size (void *handle)
158 return (int64_t) size;
161 /* Read data from the file. */
162 static int
163 example3_pread (void *handle, void *buf, uint32_t count, uint64_t offset)
165 struct example3_handle *h = handle;
167 while (count > 0) {
168 ssize_t r = pread (h->fd, buf, count, offset);
169 if (r == -1) {
170 nbdkit_error ("pead: %m");
171 return -1;
173 if (r == 0) {
174 nbdkit_error ("pread: unexpected end of file");
175 return -1;
177 buf += r;
178 count -= r;
179 offset += r;
182 return 0;
185 /* Write data to the file. */
186 static int
187 example3_pwrite (void *handle, const void *buf, uint32_t count, uint64_t offset)
189 struct example3_handle *h = handle;
191 while (count > 0) {
192 ssize_t r = pwrite (h->fd, buf, count, offset);
193 if (r == -1) {
194 nbdkit_error ("pwrite: %m");
195 return -1;
197 buf += r;
198 count -= r;
199 offset += r;
202 return 0;
205 /* Flush the file to disk. */
206 static int
207 example3_flush (void *handle)
209 struct example3_handle *h = handle;
211 if (fdatasync (h->fd) == -1) {
212 nbdkit_error ("fdatasync: %m");
213 return -1;
216 return 0;
219 static struct nbdkit_plugin plugin = {
220 .name = "example3",
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)