xref: /freebsd/sys/geom/geom_slice.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37 
38 
39 #include <sys/param.h>
40 #ifndef _KERNEL
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <signal.h>
45 #include <err.h>
46 #else
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/bio.h>
51 #include <sys/sysctl.h>
52 #include <sys/proc.h>
53 #include <sys/kthread.h>
54 #include <sys/lock.h>
55 #include <sys/mutex.h>
56 #endif
57 #include <sys/errno.h>
58 #include <sys/sbuf.h>
59 #include <geom/geom.h>
60 #include <geom/geom_slice.h>
61 #include <machine/stdarg.h>
62 
63 struct g_slicer *
64 g_slice_init(unsigned nslice, unsigned scsize)
65 {
66 	struct g_slicer *gsp;
67 
68 	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
69 	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
70 	gsp->slices = g_malloc(nslice * sizeof(struct g_slice), M_WAITOK | M_ZERO);
71 	gsp->nslice = nslice;
72 	return (gsp);
73 }
74 
75 int
76 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
77 {
78 	int error, i;
79 	struct g_geom *gp;
80 	struct g_consumer *cp;
81 	struct g_provider *pp2;
82 	struct g_slicer *gsp;
83 	struct g_slice *gsl, *gsl2;
84 
85 	gp = pp->geom;
86 	cp = LIST_FIRST(&gp->consumer);
87 	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
88 	gsp = gp->softc;
89 	gsl = &gsp->slices[pp->index];
90 	for (i = 0; i < gsp->nslice; i++) {
91 		gsl2 = &gsp->slices[i];
92 		if (gsl2->length == 0)
93 			continue;
94 		if (i == pp->index)
95 			continue;
96 		if (gsl->offset + gsl->length <= gsl2->offset)
97 			continue;
98 		if (gsl2->offset + gsl2->length <= gsl->offset)
99 			continue;
100 		/* overlap */
101 		pp2 = gsl2->provider;
102 		if ((pp->acw + dw) > 0 && pp2->ace > 0)
103 			return (EPERM);
104 		if ((pp->ace + de) > 0 && pp2->acw > 0)
105 			return (EPERM);
106 	}
107 	/* On first open, grab an extra "exclusive" bit */
108 	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
109 		de++;
110 	/* ... and let go of it on last close */
111 	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
112 		de--;
113 	error = g_access_rel(cp, dr, dw, de);
114 	return (error);
115 }
116 
117 void
118 g_slice_start(struct bio *bp)
119 {
120 	struct bio *bp2;
121 	struct g_provider *pp;
122 	struct g_geom *gp;
123 	struct g_consumer *cp;
124 	struct g_slicer *gsp;
125 	struct g_slice *gsl;
126 	int index;
127 
128 	pp = bp->bio_to;
129 	gp = pp->geom;
130 	gsp = gp->softc;
131 	cp = LIST_FIRST(&gp->consumer);
132 	index = pp->index;
133 	switch(bp->bio_cmd) {
134 	case BIO_READ:
135 	case BIO_WRITE:
136 	case BIO_DELETE:
137 		gsl = &gsp->slices[index];
138 		if (bp->bio_offset > gsl->length) {
139 			bp->bio_error = EINVAL; /* XXX: EWHAT ? */
140 			g_io_deliver(bp);
141 			return;
142 		}
143 		bp2 = g_clone_bio(bp);
144 		if (bp2->bio_offset + bp2->bio_length > gsl->length)
145 			bp2->bio_length = gsl->length - bp2->bio_offset;
146 		bp2->bio_done = g_std_done;
147 		bp2->bio_offset += gsl->offset;
148 		g_io_request(bp2, cp);
149 		return;
150 	case BIO_GETATTR:
151 	case BIO_SETATTR:
152 		if (g_haveattr_off_t(bp, "GEOM::mediasize",
153 		    gsp->slices[index].length))
154 			return;
155 		if (gsp->start(bp))
156 			return;
157 		bp2 = g_clone_bio(bp);
158 		bp2->bio_done = g_std_done;
159 		g_io_request(bp2, cp);
160 		break;
161 	default:
162 		bp->bio_error = EOPNOTSUPP;
163 		g_io_deliver(bp);
164 		return;
165 	}
166 }
167 
168 void
169 g_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
170 {
171 	struct g_mbr_softc *mp;
172 	struct g_slicer *gsp;
173 
174 	gsp = gp->softc;
175 	mp = gsp->softc;
176 	if (pp != NULL) {
177 		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
178 		sbuf_printf(sb, "%s<length>%llu</length>\n",
179 		    indent, gsp->slices[pp->index].length);
180 		sbuf_printf(sb, "%s<seclength>%llu</seclength>\n",
181 		    indent, gsp->slices[pp->index].length / 512);
182 		sbuf_printf(sb, "%s<offset>%llu</offset>\n",
183 		    indent, gsp->slices[pp->index].offset);
184 		sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n",
185 		    indent, gsp->slices[pp->index].offset / 512);
186 	}
187 }
188 
189 struct g_provider *
190 g_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...)
191 {
192 	struct g_provider *pp;
193 	struct g_slicer *gsp;
194 	va_list ap;
195 	struct sbuf *sb;
196 
197 	g_trace(G_T_TOPOLOGY, "g_slice_addslice()");
198 	g_topology_lock();
199 	gsp = gp->softc;
200 	va_start(ap, fmt);
201 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
202 	sbuf_vprintf(sb, fmt, ap);
203 	sbuf_finish(sb);
204 	pp = g_new_providerf(gp, sbuf_data(sb));
205 
206 	pp->index = index;
207 	gsp->slices[index].length = length;
208 	gsp->slices[index].offset = offset;
209 	gsp->slices[index].provider = pp;
210 	sbuf_delete(sb);
211 	g_topology_unlock();
212 	return(pp);
213 }
214 
215 struct g_geom *
216 g_slice_new(struct g_method *mp, int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start)
217 {
218 	struct g_geom *gp;
219 	struct g_slicer *gsp;
220 	struct g_consumer *cp;
221 	void **vp;
222 	int error;
223 
224 	g_topology_assert();
225 	vp = (void **)extrap;
226 	gp = g_new_geomf(mp, "%s", pp->name);
227 	gsp = g_slice_init(slices, extra);
228 	gsp->start = start;
229 	gp->softc = gsp;
230 	gp->start = g_slice_start;
231 	gp->spoiled = g_std_spoiled;
232 	gp->dumpconf = g_slice_dumpconf;
233 	cp = g_new_consumer(gp);
234 	g_attach(cp, pp);
235 	error = g_access_rel(cp, 1, 0, 0);
236 	if (error) {
237 		g_dettach(cp);
238 		g_destroy_consumer(cp);
239 		g_free(gsp->slices);
240 		g_free(gp->softc);
241 		g_destroy_geom(gp);
242 		return (NULL);
243 	}
244 	*vp = gsp->softc;
245 	*cpp = cp;
246 	return (gp);
247 }
248 
249 void
250 g_slice_orphan(struct g_consumer *cp, struct thread *tp __unused)
251 {
252 	struct g_geom *gp;
253 	struct g_provider *pp;
254 	int error;
255 
256 	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
257 	g_topology_assert();
258 	KASSERT(cp->provider->error != 0,
259 	    ("g_slice_orphan with error == 0"));
260 
261 	gp = cp->geom;
262 	gp->flags |= G_GEOM_WITHER;
263 	/* First prevent any new requests */
264 	error = cp->provider->error;
265 	LIST_FOREACH(pp, &gp->provider, provider)
266 		g_orphan_provider(pp, error);
267 
268 	return;
269 }
270