aboutsummaryrefslogtreecommitdiff
path: root/sys/geom/geom_slice.c
blob: 0bdb10c8222fcda4525c932dbcd53d03ca4cef92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*-
 * Copyright (c) 2002 Poul-Henning Kamp
 * Copyright (c) 2002 Networks Associates Technology, Inc.
 * All rights reserved.
 *
 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
 * and NAI Labs, the Security Research Division of Network Associates, Inc.
 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
 * DARPA CHATS research program.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the authors may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/bio.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/errno.h>
#include <sys/sbuf.h>
#include <geom/geom.h>
#include <geom/geom_slice.h>
#include <machine/stdarg.h>

static g_access_t g_slice_access;
static g_start_t g_slice_start;

static struct g_slicer *
g_slice_alloc(unsigned nslice, unsigned scsize)
{
	struct g_slicer *gsp;

	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
	if (scsize > 0)
		gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
	else
		gsp->softc = NULL;
	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
	    M_WAITOK | M_ZERO);
	gsp->nslice = nslice;
	return (gsp);
}

static void
g_slice_free(struct g_slicer *gsp)
{

	if (gsp == NULL)	/* XXX: phk thinks about this */
		return;
	g_free(gsp->slices);
	if (gsp->hotspot != NULL)
		g_free(gsp->hotspot);
	if (gsp->softc != NULL)
		g_free(gsp->softc);
	g_free(gsp);
}

static int
g_slice_access(struct g_provider *pp, int dr, int dw, int de)
{
	int error;
	u_int u;
	struct g_geom *gp;
	struct g_consumer *cp;
	struct g_provider *pp2;
	struct g_slicer *gsp;
	struct g_slice *gsl, *gsl2;

	gp = pp->geom;
	cp = LIST_FIRST(&gp->consumer);
	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
	gsp = gp->softc;
	if (dr > 0 || dw > 0 || de > 0) {
		gsl = &gsp->slices[pp->index];
		for (u = 0; u < gsp->nslice; u++) {
			gsl2 = &gsp->slices[u];
			if (gsl2->length == 0)
				continue;
			if (u == pp->index)
				continue;
			if (gsl->offset + gsl->length <= gsl2->offset)
				continue;
			if (gsl2->offset + gsl2->length <= gsl->offset)
				continue;
			/* overlap */
			pp2 = gsl2->provider;
			if ((pp->acw + dw) > 0 && pp2->ace > 0)
				return (EPERM);
			if ((pp->ace + de) > 0 && pp2->acw > 0)
				return (EPERM);
		}
	}
	/* On first open, grab an extra "exclusive" bit */
	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
		de++;
	/* ... and let go of it on last close */
	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
		de--;
	error = g_access(cp, dr, dw, de);
	return (error);
}

/*
 * XXX: It should be possible to specify here if we should finish all of the
 * XXX: bio, or only the non-hot bits.  This would get messy if there were
 * XXX: two hot spots in the same bio, so for now we simply finish off the
 * XXX: entire bio.  Modifying hot data on the way to disk is frowned on
 * XXX: so making that considerably harder is not a bad idea anyway.
 */
void
g_slice_finish_hot(struct bio *bp)
{
	struct bio *bp2;
	struct g_geom *gp;
	struct g_consumer *cp;
	struct g_slicer *gsp;
	struct g_slice *gsl;
	int idx;

	KASSERT(bp->bio_to != NULL,
	    ("NULL bio_to in g_slice_finish_hot(%p)", bp));
	KASSERT(bp->bio_from != NULL,
	    ("NULL bio_from in g_slice_finish_hot(%p)", bp));
	gp = bp->bio_to->geom;
	gsp = gp->softc;
	cp = LIST_FIRST(&gp->consumer);
	KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
	idx = bp->bio_to->index;
	gsl = &gsp->slices[idx];

	bp2 = g_clone_bio(bp);
	if (bp2 == NULL) {
		g_io_deliver(bp, ENOMEM);
		return;
	}
	if (bp2->bio_offset + bp2->bio_length > gsl->length)
		bp2->bio_length = gsl->length - bp2->bio_offset;
	bp2->bio_done = g_std_done;
	bp2->bio_offset += gsl->offset;
	g_io_request(bp2, cp);
	return;
}

static void
g_slice_done(struct bio *bp)
{

	KASSERT(bp->bio_cmd == BIO_GETATTR &&
	    strcmp(bp->bio_attribute, "GEOM::ident") == 0,
	    ("bio_cmd=0x%x bio_attribute=%s", bp->bio_cmd, bp->bio_attribute));

	if (bp->bio_error == 0 && bp->bio_data[0] != '\0') {
		char idx[8];

		/* Add index to the ident received. */
		snprintf(idx, sizeof(idx), "s%d",
		    bp->bio_parent->bio_to->index);
		if (strlcat(bp->bio_data, idx, bp->bio_length) >=
		    bp->bio_length) {
			bp->bio_error = EFAULT;
		}
	}
	g_std_done(bp);
}

static void
g_slice_start(struct bio *bp)
{
	struct bio *bp2;
	struct g_provider *pp;
	struct g_geom *gp;
	struct g_consumer *cp;
	struct g_slicer *gsp;
	struct g_slice *gsl;
	struct g_slice_hot *ghp;
	int idx, error;
	u_int m_index;
	off_t t;

	pp = bp->bio_to;
	gp = pp->geom;
	gsp = gp->softc;
	cp = LIST_FIRST(&gp->consumer);
	idx = pp->index;
	gsl = &gsp->slices[idx];
	switch(bp->bio_cmd) {
	case BIO_READ:
	case BIO_WRITE:
	case BIO_DELETE:
		if (bp->bio_offset > gsl->length) {
			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
			return;
		}
		/*
		 * Check if we collide with any hot spaces, and call the
		 * method once if so.
		 */
		t = bp->bio_offset + gsl->offset;
		for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
			ghp = &gsp->hotspot[m_index];
			if (t >= ghp->offset + ghp->length)
				continue;
			if (t + bp->bio_length <= ghp->offset)
				continue;
			switch(bp->bio_cmd) {
			case BIO_READ:		idx = ghp->ract; break;
			case BIO_WRITE:		idx = ghp->wact; break;
			case BIO_DELETE:	idx = ghp->dact; break;
			}
			switch(idx) {
			case G_SLICE_HOT_ALLOW:
				/* Fall out and continue normal processing */
				continue;
			case G_SLICE_HOT_DENY:
				g_io_deliver(bp, EROFS);
				return;
			case G_SLICE_HOT_START:
				error = gsp->start(bp);
				if (error && error != EJUSTRETURN)
					g_io_deliver(bp, error);
				return;
			case G_SLICE_HOT_CALL:
				error = g_post_event(gsp->hot, bp, M_NOWAIT,
				    gp, NULL);
				if (error)
					g_io_deliver(bp, error);
				return;
			}
			break;
		}
		bp2 = g_clone_bio(bp);
		if (bp2 == NULL) {
			g_io_deliver(bp, ENOMEM);
			return;
		}
		if (bp2->bio_offset + bp2->bio_length > gsl->length)
			bp2->bio_length = gsl->length - bp2->bio_offset;
		bp2->bio_done = g_std_done;
		bp2->bio_offset += gsl->offset;
		g_io_request(bp2, cp);
		return;
	case BIO_GETATTR:
		/* Give the real method a chance to override */
		if (gsp->start != NULL && gsp->start(bp))
			return;
		if (!strcmp("GEOM::ident", bp->bio_attribute)) {
			bp2 = g_clone_bio(bp);
			if (bp2 == NULL) {
				g_io_deliver(bp, ENOMEM);
				return;
			}
			bp2->bio_done = g_slice_done;
			g_io_request(bp2, cp);
			return;
		}
		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
			struct g_kerneldump *gkd;

			gkd = (struct g_kerneldump *)bp->bio_data;
			gkd->offset += gsp->slices[idx].offset;
			if (gkd->length > gsp->slices[idx].length)
				gkd->length = gsp->slices[idx].length;
			/* now, pass it on downwards... */
		}
		/* FALLTHROUGH */
	case BIO_FLUSH:
		bp2 = g_clone_bio(bp);
		if (bp2 == NULL) {
			g_io_deliver(bp, ENOMEM);
			return;
		}
		bp2->bio_done = g_std_done;
		g_io_request(bp2, cp);
		break;
	default:
		g_io_deliver(bp, EOPNOTSUPP);
		return;
	}
}

void
g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
{
	struct g_slicer *gsp;

	gsp = gp->softc;
	if (indent == NULL) {
		sbuf_printf(sb, " i %u", pp->index);
		sbuf_printf(sb, " o %ju", 
		    (uintmax_t)gsp->slices[pp->index].offset);
		return;
	}
	if (pp != NULL) {
		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
		sbuf_printf(sb, "%s<length>%ju</length>\n",
		    indent, (uintmax_t)gsp->slices[pp->index].length);
		sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
		    (uintmax_t)gsp->slices[pp->index].length / 512);
		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
		    (uintmax_t)gsp->slices[pp->index].offset);
		sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
		    (uintmax_t)gsp->slices[pp->index].offset / 512);
	}
}

int
g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
{
	struct g_provider *pp, *pp2;
	struct g_slicer *gsp;
	struct g_slice *gsl;
	va_list ap;
	struct sbuf *sb;
	int acc;

	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
	     gp->name, idx, how);
	g_topology_assert();
	gsp = gp->softc;
	if (idx >= gsp->nslice)
		return(EINVAL);
	gsl = &gsp->slices[idx];
	pp = gsl->provider;
	if (pp != NULL)
		acc = pp->acr + pp->acw + pp->ace;
	else
		acc = 0;
	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
		if (length < gsl->length)
			return(EBUSY);
		if (offset != gsl->offset)
			return(EBUSY);
	}
	/* XXX: check offset + length <= MEDIASIZE */
	if (how == G_SLICE_CONFIG_CHECK)
		return (0);
	gsl->length = length;
	gsl->offset = offset;
	gsl->sectorsize = sectorsize;
	if (length == 0) {
		if (pp == NULL)
			return (0);
		if (bootverbose)
			printf("GEOM: Deconfigure %s\n", pp->name);
		g_wither_provider(pp, ENXIO);
		gsl->provider = NULL;
		gsp->nprovider--;
		return (0);
	}
	if (pp != NULL) {
		if (bootverbose)
			printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
			    pp->name, (intmax_t)offset, (intmax_t)length,
			    (intmax_t)(offset + length - 1));
		pp->mediasize = gsl->length;
		return (0);
	}
	sb = sbuf_new_auto();
	va_start(ap, fmt);
	sbuf_vprintf(sb, fmt, ap);
	va_end(ap);
	sbuf_finish(sb);
	pp = g_new_providerf(gp, sbuf_data(sb));
	pp2 = LIST_FIRST(&gp->consumer)->provider;
	pp->flags = pp2->flags & G_PF_CANDELETE;
	if (pp2->stripesize > 0) {
		pp->stripesize = pp2->stripesize;
		pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize;
	}
	if (0 && bootverbose)
		printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
		    pp->name, (intmax_t)offset, (intmax_t)length,
		    (intmax_t)(offset + length - 1));
	pp->index = idx;
	pp->mediasize = gsl->length;
	pp->sectorsize = gsl->sectorsize;
	gsl->provider = pp;
	gsp->nprovider++;
	g_error_provider(pp, 0);
	sbuf_delete(sb);
	return(0);
}

/*
 * Configure "hotspots".  A hotspot is a piece of the parent device which
 * this particular slicer cares about for some reason.  Typically because
 * it contains meta-data used to configure the slicer.
 * A hotspot is identified by its index number. The offset and length are
 * relative to the parent device, and the three "?act" fields specify
 * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
 *
 * XXX: There may be a race relative to g_slice_start() here, if an existing
 * XXX: hotspot is changed wile I/O is happening.  Should this become a problem
 * XXX: we can protect the hotspot stuff with a mutex.
 */

int
g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
{
	struct g_slicer *gsp;
	struct g_slice_hot *gsl, *gsl2;

	g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
	    gp->name, idx, (intmax_t)offset, (intmax_t)length);
	g_topology_assert();
	gsp = gp->softc;
	gsl = gsp->hotspot;
	if(idx >= gsp->nhotspot) {
		gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
		if (gsp->hotspot != NULL)
			bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
		gsp->hotspot = gsl2;
		if (gsp->hotspot != NULL)
			g_free(gsl);
		gsl = gsl2;
		gsp->nhotspot = idx + 1;
	}
	gsl[idx].offset = offset;
	gsl[idx].length = length;
	KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
	    || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
	/* XXX: check that we _have_ a start function if HOT_START specified */
	gsl[idx].ract = ract;
	gsl[idx].dact = dact;
	gsl[idx].wact = wact;
	return (0);
}

void
g_slice_spoiled(struct g_consumer *cp)
{
	struct g_geom *gp;
	struct g_slicer *gsp;

	g_topology_assert();
	gp = cp->geom;
	g_trace(G_T_TOPOLOGY, "g_slice_spoiled(%p/%s)", cp, gp->name);
	gsp = gp->softc;
	gp->softc = NULL;
	g_slice_free(gsp);
	g_wither_geom(gp, ENXIO);
}

int
g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
{

	g_slice_spoiled(LIST_FIRST(&gp->consumer));
	return (0);
}

struct g_geom *
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)
{
	struct g_geom *gp;
	struct g_slicer *gsp;
	struct g_consumer *cp;
	void **vp;
	int error;

	g_topology_assert();
	vp = (void **)extrap;
	gp = g_new_geomf(mp, "%s", pp->name);
	gsp = g_slice_alloc(slices, extra);
	gsp->start = start;
	gp->access = g_slice_access;
	gp->orphan = g_slice_orphan;
	gp->softc = gsp;
	gp->start = g_slice_start;
	gp->spoiled = g_slice_spoiled;
	if (gp->dumpconf == NULL)
		gp->dumpconf = g_slice_dumpconf;
	if (gp->class->destroy_geom == NULL)
		gp->class->destroy_geom = g_slice_destroy_geom;
	cp = g_new_consumer(gp);
	error = g_attach(cp, pp);
	if (error == 0)
		error = g_access(cp, 1, 0, 0);
	if (error) {
		g_wither_geom(gp, ENXIO);
		return (NULL);
	}
	if (extrap != NULL)
		*vp = gsp->softc;
	*cpp = cp;
	return (gp);
}

void
g_slice_orphan(struct g_consumer *cp)
{

	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
	g_topology_assert();
	KASSERT(cp->provider->error != 0,
	    ("g_slice_orphan with error == 0"));

	/* XXX: Not good enough we leak the softc and its suballocations */
	g_slice_free(cp->geom->softc);
	g_wither_geom(cp->geom, cp->provider->error);
}