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>
47 /* IGNORE is defined as a macro in Windows headers files ... */
52 static enum ZeroMode
{
59 static enum FastZeroMode
{
67 nozero_config (nbdkit_next_config
*next
, nbdkit_backend
*nxdata
,
68 const char *key
, const char *value
)
70 if (strcmp (key
, "zeromode") == 0) {
71 if (strcmp (value
, "emulate") == 0)
73 else if (strcmp (value
, "notrim") == 0)
75 else if (strcmp (value
, "plugin") == 0)
77 else if (strcmp (value
, "none") != 0) {
78 nbdkit_error ("unknown zeromode '%s'", value
);
84 if (strcmp (key
, "fastzeromode") == 0) {
85 if (strcmp (value
, "none") == 0)
86 fastzeromode
= NOFAST
;
87 else if (strcmp (value
, "ignore") == 0)
88 fastzeromode
= IGNORE
;
89 else if (strcmp (value
, "slow") == 0)
91 else if (strcmp (value
, "default") != 0) {
92 nbdkit_error ("unknown fastzeromode '%s'", value
);
98 return next (nxdata
, key
, value
);
101 #define nozero_config_help \
102 "zeromode=<MODE> One of 'none' (default), 'emulate', 'notrim', 'plugin'.\n" \
103 "fastzeromode=<MODE> One of 'default', 'none', 'slow', 'ignore'.\n"
105 /* Check that desired mode is supported by plugin. */
107 nozero_prepare (nbdkit_next
*next
, void *handle
,
112 /* If we are opened readonly, this filter has no impact */
116 if (zeromode
== NOTRIM
|| zeromode
== PLUGIN
) {
117 r
= next
->can_zero (next
);
121 nbdkit_error ("zeromode '%s' requires plugin zero support",
122 zeromode
== NOTRIM
? "notrim" : "plugin");
129 /* Advertise desired WRITE_ZEROES mode. */
131 nozero_can_zero (nbdkit_next
*next
, void *handle
)
135 return NBDKIT_ZERO_NONE
;
137 return NBDKIT_ZERO_EMULATE
;
139 return next
->can_zero (next
);
143 /* Advertise desired FAST_ZERO mode. */
145 nozero_can_fast_zero (nbdkit_next
*next
,
148 if (zeromode
== NONE
)
150 if (zeromode
!= EMULATE
&& fastzeromode
== DEFAULT
)
151 return next
->can_fast_zero (next
);
152 return fastzeromode
!= NOFAST
;
156 nozero_zero (nbdkit_next
*next
,
157 void *handle
, uint32_t count
, uint64_t offs
, uint32_t flags
,
160 assert (zeromode
!= NONE
&& zeromode
!= EMULATE
);
161 if (flags
& NBDKIT_FLAG_FAST_ZERO
) {
162 assert (fastzeromode
!= NOFAST
);
163 if (fastzeromode
== SLOW
) {
167 if (fastzeromode
== IGNORE
)
168 flags
&= ~NBDKIT_FLAG_FAST_ZERO
;
171 if (zeromode
== NOTRIM
)
172 flags
&= ~NBDKIT_FLAG_MAY_TRIM
;
174 return next
->zero (next
, count
, offs
, flags
, err
);
177 static struct nbdkit_filter filter
= {
179 .longname
= "nbdkit nozero filter",
180 .config
= nozero_config
,
181 .config_help
= nozero_config_help
,
182 .prepare
= nozero_prepare
,
183 .can_zero
= nozero_can_zero
,
184 .can_fast_zero
= nozero_can_fast_zero
,
188 NBDKIT_REGISTER_FILTER (filter
)