xref: /freebsd/sys/geom/zero/g_zero.c (revision e7437ae907c89bf85a99c5cbb7ddd194a1ff1354)
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 #define	G_ZERO_CLASS_NAME	"ZERO"
44 
45 static int	g_zero_clear_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_PROC(_kern_geom_zero, OID_AUTO, clear,
52     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &g_zero_clear, 0,
53     g_zero_clear_sysctl, "I",
54     "Clear read data buffer");
55 static int g_zero_byte = 0;
56 SYSCTL_INT(_kern_geom_zero, OID_AUTO, byte, CTLFLAG_RW, &g_zero_byte, 0,
57     "Byte (octet) value to clear the buffers with");
58 
59 static struct g_provider *gpp;
60 
61 static int
62 g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS)
63 {
64 	int error;
65 
66 	error = sysctl_handle_int(oidp, &g_zero_clear, 0, req);
67 	if (error != 0 || req->newptr == NULL)
68 		return (error);
69 	if (gpp == NULL)
70 		return (ENXIO);
71 	if (g_zero_clear)
72 		gpp->flags &= ~G_PF_ACCEPT_UNMAPPED;
73 	else
74 		gpp->flags |= G_PF_ACCEPT_UNMAPPED;
75 	return (0);
76 }
77 
78 static void
79 g_zero_start(struct bio *bp)
80 {
81 	int error = ENXIO;
82 
83 	switch (bp->bio_cmd) {
84 	case BIO_READ:
85 		if (g_zero_clear && (bp->bio_flags & BIO_UNMAPPED) == 0)
86 			memset(bp->bio_data, g_zero_byte, bp->bio_length);
87 		/* FALLTHROUGH */
88 	case BIO_DELETE:
89 	case BIO_WRITE:
90 		bp->bio_completed = bp->bio_length;
91 		error = 0;
92 		break;
93 	case BIO_GETATTR:
94 	default:
95 		error = EOPNOTSUPP;
96 		break;
97 	}
98 	g_io_deliver(bp, error);
99 }
100 
101 static void
102 g_zero_init(struct g_class *mp)
103 {
104 	struct g_geom *gp;
105 	struct g_provider *pp;
106 
107 	g_topology_assert();
108 	gp = g_new_geomf(mp, "gzero");
109 	gp->start = g_zero_start;
110 	gp->access = g_std_access;
111 	gpp = pp = g_new_providerf(gp, "%s", gp->name);
112 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
113 	if (!g_zero_clear)
114 		pp->flags |= G_PF_ACCEPT_UNMAPPED;
115 	pp->mediasize = 1152921504606846976LLU;
116 	pp->sectorsize = 512;
117 	g_error_provider(pp, 0);
118 }
119 
120 static int
121 g_zero_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
122     struct g_geom *gp)
123 {
124 	struct g_provider *pp;
125 
126 	g_topology_assert();
127 	if (gp == NULL)
128 		return (0);
129 	pp = LIST_FIRST(&gp->provider);
130 	if (pp == NULL)
131 		return (0);
132 	if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
133 		return (EBUSY);
134 	gpp = NULL;
135 	g_wither_geom(gp, ENXIO);
136 	return (0);
137 }
138 
139 static struct g_class g_zero_class = {
140 	.name = G_ZERO_CLASS_NAME,
141 	.version = G_VERSION,
142 	.init = g_zero_init,
143 	.destroy_geom = g_zero_destroy_geom
144 };
145 
146 DECLARE_GEOM_CLASS(g_zero_class, g_zero);
147 MODULE_VERSION(geom_zero, 0);
148