1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
6 * Copyright (c) 2025 Mateusz Piotrowski <0mp@FreeBSD.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
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 #include <sys/uio.h>
39 #include <sys/types.h>
40
41 #include <geom/geom.h>
42
43 #define G_ZERO_CLASS_NAME "ZERO"
44
45 static int g_zero_byte_sysctl(SYSCTL_HANDLER_ARGS);
46
47 SYSCTL_DECL(_kern_geom);
48 static SYSCTL_NODE(_kern_geom, OID_AUTO, zero, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
49 "GEOM_ZERO stuff");
50 static int g_zero_clear = 1;
51 SYSCTL_INT(_kern_geom_zero, OID_AUTO, clear,
52 CTLFLAG_RWTUN, &g_zero_clear, 0,
53 "Clear read data buffer");
54 static int g_zero_byte = 0;
55 static uint8_t g_zero_buffer[PAGE_SIZE];
56 SYSCTL_PROC(_kern_geom_zero, OID_AUTO, byte,
57 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, &g_zero_byte, 0,
58 g_zero_byte_sysctl, "I",
59 "Byte (octet) value to clear the buffers with");
60
61 static struct g_provider *gpp;
62
63 static int
g_zero_byte_sysctl(SYSCTL_HANDLER_ARGS)64 g_zero_byte_sysctl(SYSCTL_HANDLER_ARGS)
65 {
66 int error;
67
68 // XXX: Confirm that this is called on module load as well.
69 // XXX: Shouldn't we lock here to avoid changing the byte value if the
70 // driver is in the process of handling I/O?
71 error = sysctl_handle_int(oidp, &g_zero_byte, 0, req);
72 if (error != 0 || req->newptr == NULL)
73 return (error);
74 memset(g_zero_buffer, g_zero_byte, PAGE_SIZE);
75 return (0);
76 }
77
78 static void
g_zero_fill_pages(struct bio * bp)79 g_zero_fill_pages(struct bio *bp)
80 {
81 struct iovec aiovec;
82 struct uio auio;
83 size_t length;
84 vm_offset_t offset;
85
86 auio.uio_segflg = UIO_SYSSPACE;
87 auio.uio_rw = UIO_WRITE;
88 auio.uio_td = curthread;
89
90 /*
91 * To handle the unmapped I/O request, we need to fill the pages in the
92 * bp->bio_ma array with the g_zero_byte value. However, instead of
93 * setting every byte individually, we use uiomove_fromphys() to fill a
94 * page at a time with g_zero_buffer.
95 */
96 bp->bio_resid = bp->bio_length;
97 offset = bp->bio_ma_offset & PAGE_MASK;
98 for (int i = 0; i < bp->bio_ma_n && bp->bio_resid > 0; i++) {
99 aiovec.iov_base = g_zero_buffer;
100 aiovec.iov_len = PAGE_SIZE;
101 auio.uio_iov = &aiovec;
102 auio.uio_iovcnt = 1;
103 auio.uio_offset = 0;
104
105 length = MIN(PAGE_SIZE - offset, bp->bio_resid);
106 auio.uio_resid = length;
107
108 (void)uiomove_fromphys(&bp->bio_ma[i], offset, length, &auio);
109
110 offset = 0;
111 bp->bio_resid -= length;
112 }
113 }
114
115
116 static void
g_zero_start(struct bio * bp)117 g_zero_start(struct bio *bp)
118 {
119 int error;
120
121 switch (bp->bio_cmd) {
122 case BIO_READ:
123 if (g_zero_clear) {
124 if ((bp->bio_flags & BIO_UNMAPPED) != 0)
125 g_zero_fill_pages(bp);
126 else
127 memset(bp->bio_data, g_zero_byte,
128 bp->bio_length);
129 }
130 /* FALLTHROUGH */
131 case BIO_DELETE:
132 case BIO_WRITE:
133 bp->bio_completed = bp->bio_length;
134 error = 0;
135 break;
136 case BIO_GETATTR:
137 default:
138 error = EOPNOTSUPP;
139 break;
140 }
141 g_io_deliver(bp, error);
142 }
143
144 static void
g_zero_init(struct g_class * mp)145 g_zero_init(struct g_class *mp)
146 {
147 struct g_geom *gp;
148 struct g_provider *pp;
149
150 g_topology_assert();
151 gp = g_new_geom(mp, "gzero");
152 gp->start = g_zero_start;
153 gp->access = g_std_access;
154 gpp = pp = g_new_providerf(gp, "%s", gp->name);
155 pp->flags |= G_PF_ACCEPT_UNMAPPED | G_PF_DIRECT_SEND |
156 G_PF_DIRECT_RECEIVE;
157 pp->mediasize = 1152921504606846976LLU;
158 pp->sectorsize = 512;
159 g_error_provider(pp, 0);
160 }
161
162 static int
g_zero_destroy_geom(struct gctl_req * req __unused,struct g_class * mp __unused,struct g_geom * gp)163 g_zero_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
164 struct g_geom *gp)
165 {
166 struct g_provider *pp;
167
168 g_topology_assert();
169 if (gp == NULL)
170 return (0);
171 pp = LIST_FIRST(&gp->provider);
172 if (pp == NULL)
173 return (0);
174 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
175 return (EBUSY);
176 gpp = NULL;
177 g_wither_geom(gp, ENXIO);
178 return (0);
179 }
180
181 static struct g_class g_zero_class = {
182 .name = G_ZERO_CLASS_NAME,
183 .version = G_VERSION,
184 .init = g_zero_init,
185 .destroy_geom = g_zero_destroy_geom
186 };
187
188 DECLARE_GEOM_CLASS(g_zero_class, g_zero);
189 MODULE_VERSION(geom_zero, 0);
190