xref: /freebsd/sys/geom/zero/g_zero.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*-
2  * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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 
39 #include <geom/geom.h>
40 
41 
42 #define	G_ZERO_CLASS_NAME	"ZERO"
43 
44 SYSCTL_DECL(_kern_geom);
45 static SYSCTL_NODE(_kern_geom, OID_AUTO, zero, CTLFLAG_RW, 0,
46     "GEOM_ZERO stuff");
47 static int g_zero_clear = 1;
48 SYSCTL_INT(_kern_geom_zero, OID_AUTO, clear, CTLFLAG_RW, &g_zero_clear, 0,
49     "Clear read data buffer");
50 static int g_zero_byte = 0;
51 SYSCTL_INT(_kern_geom_zero, OID_AUTO, byte, CTLFLAG_RW, &g_zero_byte, 0,
52     "Byte (octet) value to clear the buffers with");
53 
54 static void
55 g_zero_start(struct bio *bp)
56 {
57 	int error = ENXIO;
58 
59 	switch (bp->bio_cmd) {
60 	case BIO_READ:
61 		if (g_zero_clear)
62 			memset(bp->bio_data, g_zero_byte, bp->bio_length);
63 		/* FALLTHROUGH */
64 	case BIO_DELETE:
65 	case BIO_WRITE:
66 		bp->bio_completed = bp->bio_length;
67 		error = 0;
68 		break;
69 	case BIO_GETATTR:
70 	default:
71 		error = EOPNOTSUPP;
72 		break;
73 	}
74 	g_io_deliver(bp, error);
75 }
76 
77 static void
78 g_zero_init(struct g_class *mp)
79 {
80 	struct g_geom *gp;
81 	struct g_provider *pp;
82 
83 	g_topology_assert();
84 	gp = g_new_geomf(mp, "gzero");
85 	gp->start = g_zero_start;
86 	gp->access = g_std_access;
87 	pp = g_new_providerf(gp, "%s", gp->name);
88 	pp->mediasize = 1152921504606846976LLU;
89 	pp->sectorsize = 512;
90 	g_error_provider(pp, 0);
91 }
92 
93 static int
94 g_zero_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
95     struct g_geom *gp)
96 {
97 	struct g_provider *pp;
98 
99 	g_topology_assert();
100 	if (gp == NULL)
101 		return (0);
102 	pp = LIST_FIRST(&gp->provider);
103 	if (pp == NULL)
104 		return (0);
105 	if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
106 		return (EBUSY);
107 	g_wither_geom(gp, ENXIO);
108 	return (0);
109 }
110 
111 static struct g_class g_zero_class = {
112 	.name = G_ZERO_CLASS_NAME,
113 	.version = G_VERSION,
114 	.init = g_zero_init,
115 	.destroy_geom = g_zero_destroy_geom
116 };
117 
118 DECLARE_GEOM_CLASS(g_zero_class, g_zero);
119