test-layers: Test .cache usage
[nbdkit/ericb.git] / tests / test-layers-plugin.c
blobe9ffd3b75644b34fa836baf12238bf96c00e5bfd
1 /* nbdkit
2 * Copyright (C) 2018-2019 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 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
40 #define NBDKIT_API_VERSION 2
41 #include <nbdkit-plugin.h>
43 #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
45 #define DEBUG_FUNCTION nbdkit_debug ("%s", __func__)
47 static void
48 test_layers_plugin_load (void)
50 DEBUG_FUNCTION;
53 static void
54 test_layers_plugin_unload (void)
56 DEBUG_FUNCTION;
59 static int
60 test_layers_plugin_config (const char *key, const char *value)
62 DEBUG_FUNCTION;
63 return 0;
66 static int
67 test_layers_plugin_config_complete (void)
69 DEBUG_FUNCTION;
70 return 0;
73 #define test_layers_plugin_config_help "test_layers_plugin_config_help"
75 static void *
76 test_layers_plugin_open (int readonly)
78 static int handle;
80 DEBUG_FUNCTION;
81 return &handle;
84 static void
85 test_layers_plugin_close (void *handle)
87 DEBUG_FUNCTION;
90 static int64_t
91 test_layers_plugin_get_size (void *handle)
93 DEBUG_FUNCTION;
94 return 1024;
97 static int
98 test_layers_plugin_can_write (void *handle)
100 DEBUG_FUNCTION;
101 return 1;
104 static int
105 test_layers_plugin_can_flush (void *handle)
107 DEBUG_FUNCTION;
108 return 1;
111 static int
112 test_layers_plugin_is_rotational (void *handle)
114 DEBUG_FUNCTION;
115 return 1;
118 static int
119 test_layers_plugin_can_trim (void *handle)
121 DEBUG_FUNCTION;
122 return 1;
125 static int
126 test_layers_plugin_can_zero (void *handle)
128 DEBUG_FUNCTION;
129 return 1;
132 static int
133 test_layers_plugin_can_fua (void *handle)
135 DEBUG_FUNCTION;
136 return NBDKIT_FUA_NATIVE;
139 static int
140 test_layers_plugin_can_multi_conn (void *handle)
142 DEBUG_FUNCTION;
143 return 1;
146 static int
147 test_layers_plugin_can_cache (void *handle)
149 DEBUG_FUNCTION;
150 return NBDKIT_CACHE_NATIVE;
153 static int
154 test_layers_plugin_can_extents (void *handle)
156 DEBUG_FUNCTION;
157 return 1;
160 static int
161 test_layers_plugin_pread (void *handle,
162 void *buf, uint32_t count, uint64_t offset,
163 uint32_t flags)
165 DEBUG_FUNCTION;
166 memset (buf, 0, count);
167 return 0;
170 static int
171 test_layers_plugin_pwrite (void *handle,
172 const void *buf, uint32_t count, uint64_t offset,
173 uint32_t flags)
175 DEBUG_FUNCTION;
176 return 0;
179 static int
180 test_layers_plugin_flush (void *handle, uint32_t flags)
182 DEBUG_FUNCTION;
183 return 0;
186 static int
187 test_layers_plugin_trim (void *handle,
188 uint32_t count, uint64_t offset, uint32_t flags)
190 DEBUG_FUNCTION;
191 return 0;
194 static int
195 test_layers_plugin_zero (void *handle,
196 uint32_t count, uint64_t offset, uint32_t flags)
198 DEBUG_FUNCTION;
199 return 0;
202 static int
203 test_layers_plugin_extents (void *handle,
204 uint32_t count, uint64_t offset, uint32_t flags,
205 struct nbdkit_extents *extents)
207 DEBUG_FUNCTION;
208 return nbdkit_add_extent (extents, offset, count, 0);
211 static int
212 test_layers_plugin_cache (void *handle,
213 uint32_t count, uint64_t offset, uint32_t flags)
215 DEBUG_FUNCTION;
216 return 0;
219 static struct nbdkit_plugin plugin = {
220 .name = "testlayersplugin",
221 .version = PACKAGE_VERSION,
222 .load = test_layers_plugin_load,
223 .unload = test_layers_plugin_unload,
224 .config = test_layers_plugin_config,
225 .config_complete = test_layers_plugin_config_complete,
226 .config_help = test_layers_plugin_config_help,
227 .open = test_layers_plugin_open,
228 .close = test_layers_plugin_close,
229 .get_size = test_layers_plugin_get_size,
230 .can_write = test_layers_plugin_can_write,
231 .can_flush = test_layers_plugin_can_flush,
232 .is_rotational = test_layers_plugin_is_rotational,
233 .can_trim = test_layers_plugin_can_trim,
234 .can_zero = test_layers_plugin_can_zero,
235 .can_fua = test_layers_plugin_can_fua,
236 .can_multi_conn = test_layers_plugin_can_multi_conn,
237 .can_extents = test_layers_plugin_can_extents,
238 .can_cache = test_layers_plugin_can_cache,
239 .pread = test_layers_plugin_pread,
240 .pwrite = test_layers_plugin_pwrite,
241 .flush = test_layers_plugin_flush,
242 .trim = test_layers_plugin_trim,
243 .zero = test_layers_plugin_zero,
244 .extents = test_layers_plugin_extents,
245 .cache = test_layers_plugin_cache,
246 /* In this plugin, errno is preserved properly along error return
247 * paths from failed system calls.
249 .errno_is_preserved = 1,
252 NBDKIT_REGISTER_PLUGIN(plugin)