1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/bio.h>
34#include <sys/kernel.h>
35#include <sys/limits.h>
36#include <sys/malloc.h>
37#include <sys/queue.h>
38#include <sys/sysctl.h>
39#include <sys/systm.h>
40
41#include <geom/geom.h>
42
43
44#define	G_ZERO_CLASS_NAME	"ZERO"
45
46static int	g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS);
47
48SYSCTL_DECL(_kern_geom);
49static SYSCTL_NODE(_kern_geom, OID_AUTO, zero, CTLFLAG_RW, 0,
50    "GEOM_ZERO stuff");
51static int g_zero_clear = 1;
52SYSCTL_PROC(_kern_geom_zero, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW,
53    &g_zero_clear, 0, g_zero_clear_sysctl, "I", "Clear read data buffer");
54static int g_zero_byte = 0;
55SYSCTL_INT(_kern_geom_zero, OID_AUTO, byte, CTLFLAG_RW, &g_zero_byte, 0,
56    "Byte (octet) value to clear the buffers with");
57
58static struct g_provider *gpp;
59
60static int
61g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS)
62{
63	int error;
64
65	error = sysctl_handle_int(oidp, &g_zero_clear, 0, req);
66	if (error != 0 || req->newptr == NULL)
67		return (error);
68	if (gpp == NULL)
69		return (ENXIO);
70	if (g_zero_clear)
71		gpp->flags &= ~G_PF_ACCEPT_UNMAPPED;
72	else
73		gpp->flags |= G_PF_ACCEPT_UNMAPPED;
74	return (0);
75}
76
77static void
78g_zero_start(struct bio *bp)
79{
80	int error = ENXIO;
81
82	switch (bp->bio_cmd) {
83	case BIO_READ:
84		if (g_zero_clear && (bp->bio_flags & BIO_UNMAPPED) == 0)
85			memset(bp->bio_data, g_zero_byte, bp->bio_length);
86		/* FALLTHROUGH */
87	case BIO_DELETE:
88	case BIO_WRITE:
89		bp->bio_completed = bp->bio_length;
90		error = 0;
91		break;
92	case BIO_GETATTR:
93	default:
94		error = EOPNOTSUPP;
95		break;
96	}
97	g_io_deliver(bp, error);
98}
99
100static void
101g_zero_init(struct g_class *mp)
102{
103	struct g_geom *gp;
104	struct g_provider *pp;
105
106	g_topology_assert();
107	gp = g_new_geomf(mp, "gzero");
108	gp->start = g_zero_start;
109	gp->access = g_std_access;
110	gpp = pp = g_new_providerf(gp, "%s", gp->name);
111	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
112	if (!g_zero_clear)
113		pp->flags |= G_PF_ACCEPT_UNMAPPED;
114	pp->mediasize = 1152921504606846976LLU;
115	pp->sectorsize = 512;
116	g_error_provider(pp, 0);
117}
118
119static int
120g_zero_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
121    struct g_geom *gp)
122{
123	struct g_provider *pp;
124
125	g_topology_assert();
126	if (gp == NULL)
127		return (0);
128	pp = LIST_FIRST(&gp->provider);
129	if (pp == NULL)
130		return (0);
131	if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
132		return (EBUSY);
133	gpp = NULL;
134	g_wither_geom(gp, ENXIO);
135	return (0);
136}
137
138static struct g_class g_zero_class = {
139	.name = G_ZERO_CLASS_NAME,
140	.version = G_VERSION,
141	.init = g_zero_init,
142	.destroy_geom = g_zero_destroy_geom
143};
144
145DECLARE_GEOM_CLASS(g_zero_class, g_zero);
146MODULE_VERSION(geom_zero, 0);
147