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
43 #include <nbdkit-filter.h>
48 #include "ispowerof2.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. */
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
;
67 scan_config (nbdkit_next_config
*next
, nbdkit_backend
*nxdata
,
68 const char *key
, const char *value
)
72 if (strcmp (key
, "scan-ahead") == 0) {
73 r
= nbdkit_parse_bool (value
);
79 else if (strcmp (key
, "scan-clock") == 0) {
80 r
= nbdkit_parse_bool (value
);
86 else if (strcmp (key
, "scan-forever") == 0) {
87 r
= nbdkit_parse_bool (value
);
93 else if (strcmp (key
, "scan-size") == 0) {
94 scan_size
= nbdkit_parse_size (value
);
100 return next (nxdata
, key
, value
);
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");
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).
126 scan_get_ready (int final_thread_model
)
128 thread_model
= final_thread_model
;
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)
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)
151 h
= calloc (1, sizeof *h
);
153 nbdkit_error ("malloc: %m");
157 h
->is_default_export
= strcmp (exportname
, "") == 0;
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
166 scan_prepare (nbdkit_next
*next
, void *handle
, int readonly
)
168 struct scan_handle
*h
= handle
;
171 if (!h
->is_default_export
) {
172 nbdkit_error ("scan: warning: not the default export, not scanning");
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");
182 /* Call next->can_cache to read the underlying 'can_cache'. */
183 r
= next
->can_cache (next
);
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");
193 /* Save the connection in the handle, for the background thread to use. */
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
);
203 nbdkit_error ("pthread_create: %m");
204 pthread_mutex_destroy (&h
->ctrl
.lock
);
213 /* Finalize cleans up the thread if it is running. */
215 scan_finalize (nbdkit_next
*next
, void *handle
)
217 struct scan_handle
*h
= handle
;
218 const struct command quit_cmd
= { .type
= CMD_QUIT
};
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
);
233 scan_close (void *handle
)
235 struct scan_handle
*h
= handle
;
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)
256 /* Issue the normal read. */
257 return next
->pread (next
, buf
, count
, offset
, flags
, err
);
260 static struct nbdkit_filter filter
= {
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
,
268 .prepare
= scan_prepare
,
269 .finalize
= scan_finalize
,
274 NBDKIT_REGISTER_FILTER (filter
)