xref: /freebsd/sys/geom/zero/g_zero.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
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  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bio.h>
31 #include <sys/kernel.h>
32 #include <sys/limits.h>
33 #include <sys/malloc.h>
34 #include <sys/queue.h>
35 #include <sys/sysctl.h>
36 #include <sys/systm.h>
37 
38 #include <geom/geom.h>
39 
40 
41 #define	G_ZERO_CLASS_NAME	"ZERO"
42 
43 SYSCTL_DECL(_kern_geom);
44 SYSCTL_NODE(_kern_geom, OID_AUTO, zero, CTLFLAG_RW, 0, "GEOM_ZERO stuff");
45 static u_int g_zero_clear = 1;
46 SYSCTL_UINT(_kern_geom_zero, OID_AUTO, clear, CTLFLAG_RW, &g_zero_clear, 0,
47     "Zero-fill bio_data.");
48 
49 static void
50 g_zero_start(struct bio *bp)
51 {
52 	int error = ENXIO;
53 
54 	switch (bp->bio_cmd) {
55 	case BIO_READ:
56 		if (g_zero_clear)
57 			bzero(bp->bio_data, bp->bio_length);
58 		/* FALLTHROUGH */
59 	case BIO_DELETE:
60 	case BIO_WRITE:
61 		bp->bio_completed = bp->bio_length;
62 		error = 0;
63 		break;
64 	case BIO_GETATTR:
65 	default:
66 		error = EOPNOTSUPP;
67 		break;
68 	}
69 	g_io_deliver(bp, error);
70 }
71 
72 static void
73 g_zero_init(struct g_class *mp)
74 {
75 	struct g_geom *gp;
76 	struct g_provider *pp;
77 
78 	g_topology_assert();
79 	gp = g_new_geomf(mp, "gzero");
80 	gp->start = g_zero_start;
81 	gp->access = g_std_access;
82 	pp = g_new_providerf(gp, "%s", gp->name);
83 	pp->mediasize = 1152921504606846976LLU;
84 	pp->sectorsize = 512;
85 	g_error_provider(pp, 0);
86 }
87 
88 static int
89 g_zero_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
90     struct g_geom *gp)
91 {
92 	struct g_provider *pp;
93 
94 	g_topology_assert();
95 	if (gp == NULL)
96 		return (0);
97 	pp = LIST_FIRST(&gp->provider);
98 	if (pp == NULL)
99 		return (0);
100 	if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
101 		return (EBUSY);
102 	g_wither_geom(gp, ENXIO);
103 	return (EBUSY);
104 }
105 
106 static struct g_class g_zero_class = {
107 	.name = G_ZERO_CLASS_NAME,
108 	.version = G_VERSION,
109 	.init = g_zero_init,
110 	.destroy_geom = g_zero_destroy_geom
111 };
112 
113 DECLARE_GEOM_CLASS(g_zero_class, g_zero);
114