xref: /freebsd/sys/geom/geom_slice.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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 <string.h>
46 #include <err.h>
47 #else
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/bio.h>
52 #include <sys/sysctl.h>
53 #include <sys/proc.h>
54 #include <sys/kthread.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #endif
58 #include <sys/errno.h>
59 #include <sys/sbuf.h>
60 #include <geom/geom.h>
61 #include <geom/geom_slice.h>
62 #include <machine/stdarg.h>
63 
64 static g_orphan_t g_slice_orphan;
65 static g_access_t g_slice_access;
66 static g_start_t g_slice_start;
67 
68 static struct g_slicer *
69 g_slice_init(unsigned nslice, unsigned scsize)
70 {
71 	struct g_slicer *gsp;
72 
73 	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
74 	gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
75 	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
76 	    M_WAITOK | M_ZERO);
77 	gsp->nslice = nslice;
78 	return (gsp);
79 }
80 
81 static int
82 g_slice_access(struct g_provider *pp, int dr, int dw, int de)
83 {
84 	int error;
85 	u_int u;
86 	struct g_geom *gp;
87 	struct g_consumer *cp;
88 	struct g_provider *pp2;
89 	struct g_slicer *gsp;
90 	struct g_slice *gsl, *gsl2;
91 
92 	gp = pp->geom;
93 	cp = LIST_FIRST(&gp->consumer);
94 	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
95 	gsp = gp->softc;
96 	gsl = &gsp->slices[pp->index];
97 	for (u = 0; u < gsp->nslice; u++) {
98 		gsl2 = &gsp->slices[u];
99 		if (gsl2->length == 0)
100 			continue;
101 		if (u == pp->index)
102 			continue;
103 		if (gsl->offset + gsl->length <= gsl2->offset)
104 			continue;
105 		if (gsl2->offset + gsl2->length <= gsl->offset)
106 			continue;
107 		/* overlap */
108 		pp2 = gsl2->provider;
109 		if ((pp->acw + dw) > 0 && pp2->ace > 0)
110 			return (EPERM);
111 		if ((pp->ace + de) > 0 && pp2->acw > 0)
112 			return (EPERM);
113 	}
114 	/* On first open, grab an extra "exclusive" bit */
115 	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
116 		de++;
117 	/* ... and let go of it on last close */
118 	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
119 		de--;
120 	error = g_access_rel(cp, dr, dw, de);
121 	return (error);
122 }
123 
124 void
125 g_slice_finish_hot(struct bio *bp)
126 {
127 	struct bio *bp2;
128 	struct g_geom *gp;
129 	struct g_consumer *cp;
130 	struct g_slicer *gsp;
131 	struct g_slice *gsl;
132 	int idx;
133 
134 	KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_slice_finish_hot(%p)", bp));
135 	KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_slice_finish_hot(%p)", bp));
136 	gp = bp->bio_to->geom;
137 	gsp = gp->softc;
138 	cp = LIST_FIRST(&gp->consumer);
139 	KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
140 	idx = bp->bio_to->index;
141 	gsl = &gsp->slices[idx];
142 
143 	bp2 = g_clone_bio(bp);
144 	if (bp2 == NULL) {
145 		g_io_deliver(bp, ENOMEM);
146 		return;
147 	}
148 	if (bp2->bio_offset + bp2->bio_length > gsl->length)
149 		bp2->bio_length = gsl->length - bp2->bio_offset;
150 	bp2->bio_done = g_std_done;
151 	bp2->bio_offset += gsl->offset;
152 	g_io_request(bp2, cp);
153 	return;
154 }
155 
156 static void
157 g_slice_start(struct bio *bp)
158 {
159 	struct bio *bp2;
160 	struct g_provider *pp;
161 	struct g_geom *gp;
162 	struct g_consumer *cp;
163 	struct g_slicer *gsp;
164 	struct g_slice *gsl, *gmp;
165 	int idx, error;
166 	u_int m_index;
167 	off_t t;
168 
169 	pp = bp->bio_to;
170 	gp = pp->geom;
171 	gsp = gp->softc;
172 	cp = LIST_FIRST(&gp->consumer);
173 	idx = pp->index;
174 	gsl = &gsp->slices[idx];
175 	switch(bp->bio_cmd) {
176 	case BIO_READ:
177 	case BIO_WRITE:
178 	case BIO_DELETE:
179 		if (bp->bio_offset > gsl->length) {
180 			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
181 			return;
182 		}
183 		/*
184 		 * Check if we collide with any hot spaces, and call the
185 		 * method once if so.
186 		 */
187 		t = bp->bio_offset + gsl->offset;
188 		/* .ctl devices may take us negative */
189 		if (t < 0 || (t + bp->bio_length) < 0) {
190 			g_io_deliver(bp, EINVAL);
191 			return;
192 		}
193 		for (m_index = 0; m_index < gsp->nhot; m_index++) {
194 			gmp = &gsp->hot[m_index];
195 			if (t >= gmp->offset + gmp->length)
196 				continue;
197 			if (t + bp->bio_length <= gmp->offset)
198 				continue;
199 			error = gsp->start(bp);
200 			if (error == EJUSTRETURN)
201 				return;
202 			else if (error) {
203 				g_io_deliver(bp, error);
204 				return;
205 			}
206 			break;
207 		}
208 		bp2 = g_clone_bio(bp);
209 		if (bp2 == NULL) {
210 			g_io_deliver(bp, ENOMEM);
211 			return;
212 		}
213 		if (bp2->bio_offset + bp2->bio_length > gsl->length)
214 			bp2->bio_length = gsl->length - bp2->bio_offset;
215 		bp2->bio_done = g_std_done;
216 		bp2->bio_offset += gsl->offset;
217 		g_io_request(bp2, cp);
218 		return;
219 	case BIO_GETATTR:
220 	case BIO_SETATTR:
221 		/* Give the real method a chance to override */
222 		if (gsp->start(bp))
223 			return;
224 		if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) {
225 			t = gsp->cfrontstuff;
226 			if (gsp->frontstuff > t)
227 				t = gsp->frontstuff;
228 			t -= gsl->offset;
229 			if (t < 0)
230 				t = 0;
231 			if (t > gsl->length)
232 				t = gsl->length;
233 			g_handleattr_off_t(bp, "GEOM::frontstuff", t);
234 			return;
235 		}
236 #ifdef _KERNEL
237 		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
238 			struct g_kerneldump *gkd;
239 
240 			gkd = (struct g_kerneldump *)bp->bio_data;
241 			gkd->offset += gsp->slices[idx].offset;
242 			if (gkd->length > gsp->slices[idx].length)
243 				gkd->length = gsp->slices[idx].length;
244 			/* now, pass it on downwards... */
245 		}
246 #endif
247 		bp2 = g_clone_bio(bp);
248 		if (bp2 == NULL) {
249 			g_io_deliver(bp, ENOMEM);
250 			return;
251 		}
252 		bp2->bio_done = g_std_done;
253 		g_io_request(bp2, cp);
254 		break;
255 	default:
256 		g_io_deliver(bp, EOPNOTSUPP);
257 		return;
258 	}
259 }
260 
261 void
262 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
263 {
264 	struct g_slicer *gsp;
265 
266 	gsp = gp->softc;
267 	if (indent == NULL) {
268 		sbuf_printf(sb, " i %u", pp->index);
269 		sbuf_printf(sb, " o %ju",
270 		    (uintmax_t)gsp->slices[pp->index].offset);
271 		return;
272 	}
273 	if (gp != NULL && (pp == NULL && cp == NULL)) {
274 		sbuf_printf(sb, "%s<frontstuff>%ju</frontstuff>\n",
275 		    indent, (intmax_t)gsp->frontstuff);
276 	}
277 	if (pp != NULL) {
278 		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
279 		sbuf_printf(sb, "%s<length>%ju</length>\n",
280 		    indent, (uintmax_t)gsp->slices[pp->index].length);
281 		sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
282 		    (uintmax_t)gsp->slices[pp->index].length / 512);
283 		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
284 		    (uintmax_t)gsp->slices[pp->index].offset);
285 		sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
286 		    (uintmax_t)gsp->slices[pp->index].offset / 512);
287 	}
288 }
289 
290 int
291 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
292 {
293 	struct g_provider *pp, *pp2;
294 	struct g_slicer *gsp;
295 	struct g_slice *gsl;
296 	va_list ap;
297 	struct sbuf *sb;
298 	int error, acc;
299 
300 	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
301 	     gp->name, idx, how);
302 	g_topology_assert();
303 	gsp = gp->softc;
304 	error = 0;
305 	if (idx >= gsp->nslice)
306 		return(EINVAL);
307 	gsl = &gsp->slices[idx];
308 	pp = gsl->provider;
309 	if (pp != NULL)
310 		acc = pp->acr + pp->acw + pp->ace;
311 	else
312 		acc = 0;
313 	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
314 		if (length < gsl->length)
315 			return(EBUSY);
316 		if (offset != gsl->offset)
317 			return(EBUSY);
318 	}
319 	/* XXX: check offset + length <= MEDIASIZE */
320 	if (how == G_SLICE_CONFIG_CHECK)
321 		return (0);
322 	gsl->length = length;
323 	gsl->offset = offset;
324 	gsl->sectorsize = sectorsize;
325 	if (length == 0) {
326 		if (pp == NULL)
327 			return (0);
328 		if (bootverbose)
329 			printf("GEOM: Deconfigure %s\n", pp->name);
330 		g_orphan_provider(pp, ENXIO);
331 		gsl->provider = NULL;
332 		gsp->nprovider--;
333 		return (0);
334 	}
335 	if (pp != NULL) {
336 		if (bootverbose)
337 			printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
338 			    pp->name, (intmax_t)offset, (intmax_t)length,
339 			    (intmax_t)(offset + length - 1));
340 		pp->mediasize = gsl->length;
341 		return (0);
342 	}
343 	va_start(ap, fmt);
344 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
345 	sbuf_vprintf(sb, fmt, ap);
346 	sbuf_finish(sb);
347 	pp = g_new_providerf(gp, sbuf_data(sb));
348 	pp2 = LIST_FIRST(&gp->consumer)->provider;
349 	pp->flags = pp2->flags & G_PF_CANDELETE;
350 	if (pp2->stripesize > 0) {
351 		pp->stripesize = pp2->stripesize;
352 		pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize;
353 	}
354 	if (bootverbose)
355 		printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
356 		    pp->name, (intmax_t)offset, (intmax_t)length,
357 		    (intmax_t)(offset + length - 1));
358 	pp->index = idx;
359 	pp->mediasize = gsl->length;
360 	pp->sectorsize = gsl->sectorsize;
361 	gsl->provider = pp;
362 	gsp->nprovider++;
363 	g_error_provider(pp, 0);
364 	sbuf_delete(sb);
365 	return(0);
366 }
367 
368 int
369 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length)
370 {
371 	struct g_slicer *gsp;
372 	struct g_slice *gsl, *gsl2;
373 
374 	g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()");
375 	g_topology_assert();
376 	gsp = gp->softc;
377 	gsl = gsp->hot;
378 	if(idx >= gsp->nhot) {
379 		gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
380 		if (gsp->hot != NULL)
381 			bcopy(gsp->hot, gsl2, gsp->nhot * sizeof *gsl2);
382 		gsp->hot = gsl2;
383 		if (gsp->hot != NULL)
384 			g_free(gsl);
385 		gsl = gsl2;
386 		gsp->nhot = idx + 1;
387 	}
388 	if (bootverbose)
389 		printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n",
390 		    gp->name, idx, (intmax_t)offset, (intmax_t)length,
391 		    (intmax_t)(offset + length - 1));
392 	gsl[idx].offset = offset;
393 	gsl[idx].length = length;
394 	return (0);
395 }
396 
397 struct g_geom *
398 g_slice_new(struct g_class *mp, u_int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start)
399 {
400 	struct g_geom *gp;
401 	struct g_slicer *gsp;
402 	struct g_consumer *cp;
403 	void **vp;
404 	int error, i;
405 
406 	g_topology_assert();
407 	vp = (void **)extrap;
408 	gp = g_new_geomf(mp, "%s", pp->name);
409 	gsp = g_slice_init(slices, extra);
410 	gsp->start = start;
411 	gp->access = g_slice_access;
412 	gp->orphan = g_slice_orphan;
413 	gp->softc = gsp;
414 	gp->start = g_slice_start;
415 	gp->spoiled = g_std_spoiled;
416 	gp->dumpconf = g_slice_dumpconf;
417 	cp = g_new_consumer(gp);
418 	error = g_attach(cp, pp);
419 	if (error == 0)
420 		error = g_access_rel(cp, 1, 0, 0);
421 	if (error) {
422 		if (cp->provider != NULL)
423 			g_detach(cp);
424 		g_destroy_consumer(cp);
425 		g_free(gsp->slices);
426 		g_free(gp->softc);
427 		g_destroy_geom(gp);
428 		return (NULL);
429 	}
430 	/* Find out if there are any magic bytes on the consumer */
431 	i = sizeof gsp->cfrontstuff;
432 	error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff);
433 	if (error)
434 		gsp->cfrontstuff = 0;
435 	*vp = gsp->softc;
436 	*cpp = cp;
437 	return (gp);
438 }
439 
440 static void
441 g_slice_orphan(struct g_consumer *cp)
442 {
443 	struct g_geom *gp;
444 	struct g_provider *pp;
445 	int error;
446 
447 	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
448 	g_topology_assert();
449 	KASSERT(cp->provider->error != 0,
450 	    ("g_slice_orphan with error == 0"));
451 
452 	gp = cp->geom;
453 	/* XXX: Not good enough we leak the softc and its suballocations */
454 	gp->flags |= G_GEOM_WITHER;
455 	error = cp->provider->error;
456 	LIST_FOREACH(pp, &gp->provider, provider)
457 		g_orphan_provider(pp, error);
458 	return;
459 }
460