Update Red Hat Copyright Notices
[nbdkit.git] / filters / scan / scan.c
blob8e77a90d970ccddc1a60f0b9b291bc27a5db3860
1 /* nbdkit
2 * Copyright Red Hat
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 <stdbool.h>
38 #include <stdint.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <pthread.h>
43 #include <nbdkit-filter.h>
45 #include "scan.h"
47 #include "cleanup.h"
48 #include "ispowerof2.h"
49 #include "vector.h"
51 static bool scan_ahead = true;
52 bool scan_clock = true;
53 bool scan_forever = false;
54 unsigned scan_size = 2*1024*1024;
56 static int thread_model = -1; /* Thread model of the underlying plugin. */
58 /* Per-connection data. */
59 struct scan_handle {
60 bool is_default_export; /* If exportname == "". */
61 bool running; /* True if background thread is running. */
62 pthread_t thread; /* The background thread, one per connection. */
63 struct bgthread_ctrl ctrl;
66 static int
67 scan_config (nbdkit_next_config *next, nbdkit_backend *nxdata,
68 const char *key, const char *value)
70 int r;
72 if (strcmp (key, "scan-ahead") == 0) {
73 r = nbdkit_parse_bool (value);
74 if (r == -1)
75 return -1;
76 scan_ahead = r;
77 return 0;
79 else if (strcmp (key, "scan-clock") == 0) {
80 r = nbdkit_parse_bool (value);
81 if (r == -1)
82 return -1;
83 scan_clock = r;
84 return 0;
86 else if (strcmp (key, "scan-forever") == 0) {
87 r = nbdkit_parse_bool (value);
88 if (r == -1)
89 return -1;
90 scan_forever = r;
91 return 0;
93 else if (strcmp (key, "scan-size") == 0) {
94 scan_size = nbdkit_parse_size (value);
95 if (scan_size == -1)
96 return -1;
97 return 0;
100 return next (nxdata, key, value);
103 static int
104 scan_config_complete (nbdkit_next_config_complete *next, nbdkit_backend *nxdata)
106 if (scan_size < 512 || scan_size > 32*1024*1024 ||
107 !is_power_of_2 (scan_size)) {
108 nbdkit_error ("scan-size parameter should be [512..32M] "
109 "and a power of two");
110 return -1;
113 return next (nxdata);
116 #define scan_config_help \
117 "scan-ahead=false Skip ahead when client reads faster.\n" \
118 "scan-clock=false Always start prefetching from beginning.\n" \
119 "scan-forever=true Scan in a loop while clients connected.\n" \
120 "scan-size=NN Set scan block size."
122 /* We need to hook into .get_ready() so we can read the final thread
123 * model (of the whole server).
125 static int
126 scan_get_ready (int final_thread_model)
128 thread_model = final_thread_model;
129 return 0;
132 static int
133 send_command_to_background_thread (struct bgthread_ctrl *ctrl,
134 const struct command cmd)
136 ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&ctrl->lock);
137 if (command_queue_append (&ctrl->cmds, cmd) == -1)
138 return -1;
139 return 0;
142 static void *
143 scan_open (nbdkit_next_open *next, nbdkit_context *nxdata,
144 int readonly, const char *exportname, int is_tls)
146 struct scan_handle *h;
148 if (next (nxdata, readonly, exportname) == -1)
149 return NULL;
151 h = calloc (1, sizeof *h);
152 if (h == NULL) {
153 nbdkit_error ("malloc: %m");
154 return NULL;
157 h->is_default_export = strcmp (exportname, "") == 0;
158 return h;
161 /* In prepare we check if it's possible to support the scan filter on
162 * this connection (or print a warning), and start the background
163 * thread.
165 static int
166 scan_prepare (nbdkit_next *next, void *handle, int readonly)
168 struct scan_handle *h = handle;
169 int r, err;
171 if (!h->is_default_export) {
172 nbdkit_error ("scan: warning: not the default export, not scanning");
173 return 0;
176 if (thread_model != NBDKIT_THREAD_MODEL_PARALLEL) {
177 nbdkit_error ("scan: warning: underlying plugin does not support "
178 "the PARALLEL thread model, not scanning");
179 return 0;
182 /* Call next->can_cache to read the underlying 'can_cache'. */
183 r = next->can_cache (next);
184 if (r == -1)
185 return -1;
186 if (r != NBDKIT_CACHE_NATIVE) {
187 nbdkit_error ("scan: warning: underlying plugin does not support "
188 "NBD_CMD_CACHE, not scanning; try adding --filter=cache "
189 "after this filter");
190 return 0;
193 /* Save the connection in the handle, for the background thread to use. */
194 h->ctrl.next = next;
196 /* Create the background thread. */
197 h->ctrl.cmds = (command_queue) empty_vector;
198 pthread_mutex_init (&h->ctrl.lock, NULL);
200 err = pthread_create (&h->thread, NULL, scan_thread, &h->ctrl);
201 if (err != 0) {
202 errno = err;
203 nbdkit_error ("pthread_create: %m");
204 pthread_mutex_destroy (&h->ctrl.lock);
205 return -1;
208 h->running = true;
210 return 0;
213 /* Finalize cleans up the thread if it is running. */
214 static int
215 scan_finalize (nbdkit_next *next, void *handle)
217 struct scan_handle *h = handle;
218 const struct command quit_cmd = { .type = CMD_QUIT };
220 if (!h->running)
221 return 0;
223 send_command_to_background_thread (&h->ctrl, quit_cmd);
224 pthread_join (h->thread, NULL);
225 pthread_mutex_destroy (&h->ctrl.lock);
226 command_queue_reset (&h->ctrl.cmds);
227 h->running = false;
229 return 0;
232 static void
233 scan_close (void *handle)
235 struct scan_handle *h = handle;
237 free (h);
240 /* Read data. */
241 static int
242 scan_pread (nbdkit_next *next,
243 void *handle, void *buf, uint32_t count, uint64_t offset,
244 uint32_t flags, int *err)
246 struct scan_handle *h = handle;
248 if (scan_ahead && h->running) {
249 const struct command cmd =
250 { .type = CMD_NOTIFY_PREAD, .offset = offset + count };
252 if (send_command_to_background_thread (&h->ctrl, cmd) == -1)
253 return -1;
256 /* Issue the normal read. */
257 return next->pread (next, buf, count, offset, flags, err);
260 static struct nbdkit_filter filter = {
261 .name = "scan",
262 .longname = "nbdkit scan filter",
263 .get_ready = scan_get_ready,
264 .config = scan_config,
265 .config_complete = scan_config_complete,
266 .config_help = scan_config_help,
267 .open = scan_open,
268 .prepare = scan_prepare,
269 .finalize = scan_finalize,
270 .close = scan_close,
271 .pread = scan_pread,
274 NBDKIT_REGISTER_FILTER (filter)