1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005 Pawel Jakub Dawidek <[email protected]>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/kernel.h>
33 #include <sys/limits.h>
34 #include <sys/malloc.h>
35 #include <sys/queue.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38
39 #include <geom/geom.h>
40
41 #define G_ZERO_CLASS_NAME "ZERO"
42
43 static int g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS);
44
45 SYSCTL_DECL(_kern_geom);
46 static SYSCTL_NODE(_kern_geom, OID_AUTO, zero, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
47 "GEOM_ZERO stuff");
48 static int g_zero_clear = 1;
49 SYSCTL_PROC(_kern_geom_zero, OID_AUTO, clear,
50 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &g_zero_clear, 0,
51 g_zero_clear_sysctl, "I",
52 "Clear read data buffer");
53 static int g_zero_byte = 0;
54 SYSCTL_INT(_kern_geom_zero, OID_AUTO, byte, CTLFLAG_RW, &g_zero_byte, 0,
55 "Byte (octet) value to clear the buffers with");
56
57 static struct g_provider *gpp;
58
59 static int
g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS)60 g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS)
61 {
62 int error;
63
64 error = sysctl_handle_int(oidp, &g_zero_clear, 0, req);
65 if (error != 0 || req->newptr == NULL)
66 return (error);
67 if (gpp == NULL)
68 return (ENXIO);
69 if (g_zero_clear)
70 gpp->flags &= ~G_PF_ACCEPT_UNMAPPED;
71 else
72 gpp->flags |= G_PF_ACCEPT_UNMAPPED;
73 return (0);
74 }
75
76 static void
g_zero_start(struct bio * bp)77 g_zero_start(struct bio *bp)
78 {
79 int error = ENXIO;
80
81 switch (bp->bio_cmd) {
82 case BIO_READ:
83 if (g_zero_clear && (bp->bio_flags & BIO_UNMAPPED) == 0)
84 memset(bp->bio_data, g_zero_byte, bp->bio_length);
85 /* FALLTHROUGH */
86 case BIO_DELETE:
87 case BIO_WRITE:
88 bp->bio_completed = bp->bio_length;
89 error = 0;
90 break;
91 case BIO_GETATTR:
92 default:
93 error = EOPNOTSUPP;
94 break;
95 }
96 g_io_deliver(bp, error);
97 }
98
99 static void
g_zero_init(struct g_class * mp)100 g_zero_init(struct g_class *mp)
101 {
102 struct g_geom *gp;
103 struct g_provider *pp;
104
105 g_topology_assert();
106 gp = g_new_geomf(mp, "gzero");
107 gp->start = g_zero_start;
108 gp->access = g_std_access;
109 gpp = pp = g_new_providerf(gp, "%s", gp->name);
110 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
111 if (!g_zero_clear)
112 pp->flags |= G_PF_ACCEPT_UNMAPPED;
113 pp->mediasize = 1152921504606846976LLU;
114 pp->sectorsize = 512;
115 g_error_provider(pp, 0);
116 }
117
118 static int
g_zero_destroy_geom(struct gctl_req * req __unused,struct g_class * mp __unused,struct g_geom * gp)119 g_zero_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
120 struct g_geom *gp)
121 {
122 struct g_provider *pp;
123
124 g_topology_assert();
125 if (gp == NULL)
126 return (0);
127 pp = LIST_FIRST(&gp->provider);
128 if (pp == NULL)
129 return (0);
130 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
131 return (EBUSY);
132 gpp = NULL;
133 g_wither_geom(gp, ENXIO);
134 return (0);
135 }
136
137 static struct g_class g_zero_class = {
138 .name = G_ZERO_CLASS_NAME,
139 .version = G_VERSION,
140 .init = g_zero_init,
141 .destroy_geom = g_zero_destroy_geom
142 };
143
144 DECLARE_GEOM_CLASS(g_zero_class, g_zero);
145 MODULE_VERSION(geom_zero, 0);
146