xref: /freebsd/sys/geom/nop/g_nop.c (revision 298cf604ccf133b101c6fad42d1a078a1fac58ca)
1 /*-
2  * Copyright (c) 2004-2006 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/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/bio.h>
37 #include <sys/sbuf.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <geom/geom.h>
41 #include <geom/nop/g_nop.h>
42 
43 
44 SYSCTL_DECL(_kern_geom);
45 static SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff");
46 static u_int g_nop_debug = 0;
47 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0,
48     "Debug level");
49 
50 static int g_nop_destroy(struct g_geom *gp, boolean_t force);
51 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp,
52     struct g_geom *gp);
53 static void g_nop_config(struct gctl_req *req, struct g_class *mp,
54     const char *verb);
55 static void g_nop_dumpconf(struct sbuf *sb, const char *indent,
56     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
57 
58 struct g_class g_nop_class = {
59 	.name = G_NOP_CLASS_NAME,
60 	.version = G_VERSION,
61 	.ctlreq = g_nop_config,
62 	.destroy_geom = g_nop_destroy_geom
63 };
64 
65 
66 static void
67 g_nop_orphan(struct g_consumer *cp)
68 {
69 
70 	g_topology_assert();
71 	g_nop_destroy(cp->geom, 1);
72 }
73 
74 static void
75 g_nop_resize(struct g_consumer *cp)
76 {
77 	struct g_nop_softc *sc;
78 	struct g_geom *gp;
79 	struct g_provider *pp;
80 	off_t size;
81 
82 	g_topology_assert();
83 
84 	gp = cp->geom;
85 	sc = gp->softc;
86 
87 	if (sc->sc_explicitsize != 0)
88 		return;
89 	if (cp->provider->mediasize < sc->sc_offset) {
90 		g_nop_destroy(gp, 1);
91 		return;
92 	}
93 	size = cp->provider->mediasize - sc->sc_offset;
94 	LIST_FOREACH(pp, &gp->provider, provider)
95 		g_resize_provider(pp, size);
96 }
97 
98 static void
99 g_nop_start(struct bio *bp)
100 {
101 	struct g_nop_softc *sc;
102 	struct g_geom *gp;
103 	struct g_provider *pp;
104 	struct bio *cbp;
105 	u_int failprob = 0;
106 
107 	gp = bp->bio_to->geom;
108 	sc = gp->softc;
109 	G_NOP_LOGREQ(bp, "Request received.");
110 	switch (bp->bio_cmd) {
111 	case BIO_READ:
112 		sc->sc_reads++;
113 		sc->sc_readbytes += bp->bio_length;
114 		failprob = sc->sc_rfailprob;
115 		break;
116 	case BIO_WRITE:
117 		sc->sc_writes++;
118 		sc->sc_wrotebytes += bp->bio_length;
119 		failprob = sc->sc_wfailprob;
120 		break;
121 	}
122 	if (failprob > 0) {
123 		u_int rval;
124 
125 		rval = arc4random() % 100;
126 		if (rval < failprob) {
127 			G_NOP_LOGREQ(bp, "Returning error=%d.", sc->sc_error);
128 			g_io_deliver(bp, sc->sc_error);
129 			return;
130 		}
131 	}
132 	cbp = g_clone_bio(bp);
133 	if (cbp == NULL) {
134 		g_io_deliver(bp, ENOMEM);
135 		return;
136 	}
137 	cbp->bio_done = g_std_done;
138 	cbp->bio_offset = bp->bio_offset + sc->sc_offset;
139 	cbp->bio_data = bp->bio_data;
140 	cbp->bio_length = bp->bio_length;
141 	pp = LIST_FIRST(&gp->provider);
142 	KASSERT(pp != NULL, ("NULL pp"));
143 	cbp->bio_to = pp;
144 	G_NOP_LOGREQ(cbp, "Sending request.");
145 	g_io_request(cbp, LIST_FIRST(&gp->consumer));
146 }
147 
148 static int
149 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
150 {
151 	struct g_geom *gp;
152 	struct g_consumer *cp;
153 	int error;
154 
155 	gp = pp->geom;
156 	cp = LIST_FIRST(&gp->consumer);
157 	error = g_access(cp, dr, dw, de);
158 
159 	return (error);
160 }
161 
162 static int
163 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
164     int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
165     u_int secsize)
166 {
167 	struct g_nop_softc *sc;
168 	struct g_geom *gp;
169 	struct g_provider *newpp;
170 	struct g_consumer *cp;
171 	char name[64];
172 	int error;
173 	off_t explicitsize;
174 
175 	g_topology_assert();
176 
177 	gp = NULL;
178 	newpp = NULL;
179 	cp = NULL;
180 
181 	if ((offset % pp->sectorsize) != 0) {
182 		gctl_error(req, "Invalid offset for provider %s.", pp->name);
183 		return (EINVAL);
184 	}
185 	if ((size % pp->sectorsize) != 0) {
186 		gctl_error(req, "Invalid size for provider %s.", pp->name);
187 		return (EINVAL);
188 	}
189 	if (offset >= pp->mediasize) {
190 		gctl_error(req, "Invalid offset for provider %s.", pp->name);
191 		return (EINVAL);
192 	}
193 	explicitsize = size;
194 	if (size == 0)
195 		size = pp->mediasize - offset;
196 	if (offset + size > pp->mediasize) {
197 		gctl_error(req, "Invalid size for provider %s.", pp->name);
198 		return (EINVAL);
199 	}
200 	if (secsize == 0)
201 		secsize = pp->sectorsize;
202 	else if ((secsize % pp->sectorsize) != 0) {
203 		gctl_error(req, "Invalid secsize for provider %s.", pp->name);
204 		return (EINVAL);
205 	}
206 	if (secsize > MAXPHYS) {
207 		gctl_error(req, "secsize is too big.");
208 		return (EINVAL);
209 	}
210 	size -= size % secsize;
211 	snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
212 	LIST_FOREACH(gp, &mp->geom, geom) {
213 		if (strcmp(gp->name, name) == 0) {
214 			gctl_error(req, "Provider %s already exists.", name);
215 			return (EEXIST);
216 		}
217 	}
218 	gp = g_new_geomf(mp, "%s", name);
219 	sc = g_malloc(sizeof(*sc), M_WAITOK);
220 	sc->sc_offset = offset;
221 	sc->sc_explicitsize = explicitsize;
222 	sc->sc_error = ioerror;
223 	sc->sc_rfailprob = rfailprob;
224 	sc->sc_wfailprob = wfailprob;
225 	sc->sc_reads = 0;
226 	sc->sc_writes = 0;
227 	sc->sc_readbytes = 0;
228 	sc->sc_wrotebytes = 0;
229 	gp->softc = sc;
230 	gp->start = g_nop_start;
231 	gp->orphan = g_nop_orphan;
232 	gp->resize = g_nop_resize;
233 	gp->access = g_nop_access;
234 	gp->dumpconf = g_nop_dumpconf;
235 
236 	newpp = g_new_providerf(gp, "%s", gp->name);
237 	newpp->mediasize = size;
238 	newpp->sectorsize = secsize;
239 
240 	cp = g_new_consumer(gp);
241 	error = g_attach(cp, pp);
242 	if (error != 0) {
243 		gctl_error(req, "Cannot attach to provider %s.", pp->name);
244 		goto fail;
245 	}
246 
247 	g_error_provider(newpp, 0);
248 	G_NOP_DEBUG(0, "Device %s created.", gp->name);
249 	return (0);
250 fail:
251 	if (cp->provider != NULL)
252 		g_detach(cp);
253 	g_destroy_consumer(cp);
254 	g_destroy_provider(newpp);
255 	g_free(gp->softc);
256 	g_destroy_geom(gp);
257 	return (error);
258 }
259 
260 static int
261 g_nop_destroy(struct g_geom *gp, boolean_t force)
262 {
263 	struct g_provider *pp;
264 
265 	g_topology_assert();
266 	if (gp->softc == NULL)
267 		return (ENXIO);
268 	pp = LIST_FIRST(&gp->provider);
269 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
270 		if (force) {
271 			G_NOP_DEBUG(0, "Device %s is still open, so it "
272 			    "can't be definitely removed.", pp->name);
273 		} else {
274 			G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
275 			    pp->name, pp->acr, pp->acw, pp->ace);
276 			return (EBUSY);
277 		}
278 	} else {
279 		G_NOP_DEBUG(0, "Device %s removed.", gp->name);
280 	}
281 	g_free(gp->softc);
282 	gp->softc = NULL;
283 	g_wither_geom(gp, ENXIO);
284 
285 	return (0);
286 }
287 
288 static int
289 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
290 {
291 
292 	return (g_nop_destroy(gp, 0));
293 }
294 
295 static void
296 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
297 {
298 	struct g_provider *pp;
299 	intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size;
300 	const char *name;
301 	char param[16];
302 	int i, *nargs;
303 
304 	g_topology_assert();
305 
306 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
307 	if (nargs == NULL) {
308 		gctl_error(req, "No '%s' argument", "nargs");
309 		return;
310 	}
311 	if (*nargs <= 0) {
312 		gctl_error(req, "Missing device(s).");
313 		return;
314 	}
315 	error = gctl_get_paraml(req, "error", sizeof(*error));
316 	if (error == NULL) {
317 		gctl_error(req, "No '%s' argument", "error");
318 		return;
319 	}
320 	rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
321 	if (rfailprob == NULL) {
322 		gctl_error(req, "No '%s' argument", "rfailprob");
323 		return;
324 	}
325 	if (*rfailprob < -1 || *rfailprob > 100) {
326 		gctl_error(req, "Invalid '%s' argument", "rfailprob");
327 		return;
328 	}
329 	wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
330 	if (wfailprob == NULL) {
331 		gctl_error(req, "No '%s' argument", "wfailprob");
332 		return;
333 	}
334 	if (*wfailprob < -1 || *wfailprob > 100) {
335 		gctl_error(req, "Invalid '%s' argument", "wfailprob");
336 		return;
337 	}
338 	offset = gctl_get_paraml(req, "offset", sizeof(*offset));
339 	if (offset == NULL) {
340 		gctl_error(req, "No '%s' argument", "offset");
341 		return;
342 	}
343 	if (*offset < 0) {
344 		gctl_error(req, "Invalid '%s' argument", "offset");
345 		return;
346 	}
347 	size = gctl_get_paraml(req, "size", sizeof(*size));
348 	if (size == NULL) {
349 		gctl_error(req, "No '%s' argument", "size");
350 		return;
351 	}
352 	if (*size < 0) {
353 		gctl_error(req, "Invalid '%s' argument", "size");
354 		return;
355 	}
356 	secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
357 	if (secsize == NULL) {
358 		gctl_error(req, "No '%s' argument", "secsize");
359 		return;
360 	}
361 	if (*secsize < 0) {
362 		gctl_error(req, "Invalid '%s' argument", "secsize");
363 		return;
364 	}
365 
366 	for (i = 0; i < *nargs; i++) {
367 		snprintf(param, sizeof(param), "arg%d", i);
368 		name = gctl_get_asciiparam(req, param);
369 		if (name == NULL) {
370 			gctl_error(req, "No 'arg%d' argument", i);
371 			return;
372 		}
373 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
374 			name += strlen("/dev/");
375 		pp = g_provider_by_name(name);
376 		if (pp == NULL) {
377 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
378 			gctl_error(req, "Provider %s is invalid.", name);
379 			return;
380 		}
381 		if (g_nop_create(req, mp, pp,
382 		    *error == -1 ? EIO : (int)*error,
383 		    *rfailprob == -1 ? 0 : (u_int)*rfailprob,
384 		    *wfailprob == -1 ? 0 : (u_int)*wfailprob,
385 		    (off_t)*offset, (off_t)*size, (u_int)*secsize) != 0) {
386 			return;
387 		}
388 	}
389 }
390 
391 static void
392 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
393 {
394 	struct g_nop_softc *sc;
395 	struct g_provider *pp;
396 	intmax_t *error, *rfailprob, *wfailprob;
397 	const char *name;
398 	char param[16];
399 	int i, *nargs;
400 
401 	g_topology_assert();
402 
403 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
404 	if (nargs == NULL) {
405 		gctl_error(req, "No '%s' argument", "nargs");
406 		return;
407 	}
408 	if (*nargs <= 0) {
409 		gctl_error(req, "Missing device(s).");
410 		return;
411 	}
412 	error = gctl_get_paraml(req, "error", sizeof(*error));
413 	if (error == NULL) {
414 		gctl_error(req, "No '%s' argument", "error");
415 		return;
416 	}
417 	rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
418 	if (rfailprob == NULL) {
419 		gctl_error(req, "No '%s' argument", "rfailprob");
420 		return;
421 	}
422 	if (*rfailprob < -1 || *rfailprob > 100) {
423 		gctl_error(req, "Invalid '%s' argument", "rfailprob");
424 		return;
425 	}
426 	wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
427 	if (wfailprob == NULL) {
428 		gctl_error(req, "No '%s' argument", "wfailprob");
429 		return;
430 	}
431 	if (*wfailprob < -1 || *wfailprob > 100) {
432 		gctl_error(req, "Invalid '%s' argument", "wfailprob");
433 		return;
434 	}
435 
436 	for (i = 0; i < *nargs; i++) {
437 		snprintf(param, sizeof(param), "arg%d", i);
438 		name = gctl_get_asciiparam(req, param);
439 		if (name == NULL) {
440 			gctl_error(req, "No 'arg%d' argument", i);
441 			return;
442 		}
443 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
444 			name += strlen("/dev/");
445 		pp = g_provider_by_name(name);
446 		if (pp == NULL || pp->geom->class != mp) {
447 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
448 			gctl_error(req, "Provider %s is invalid.", name);
449 			return;
450 		}
451 		sc = pp->geom->softc;
452 		if (*error != -1)
453 			sc->sc_error = (int)*error;
454 		if (*rfailprob != -1)
455 			sc->sc_rfailprob = (u_int)*rfailprob;
456 		if (*wfailprob != -1)
457 			sc->sc_wfailprob = (u_int)*wfailprob;
458 	}
459 }
460 
461 static struct g_geom *
462 g_nop_find_geom(struct g_class *mp, const char *name)
463 {
464 	struct g_geom *gp;
465 
466 	LIST_FOREACH(gp, &mp->geom, geom) {
467 		if (strcmp(gp->name, name) == 0)
468 			return (gp);
469 	}
470 	return (NULL);
471 }
472 
473 static void
474 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
475 {
476 	int *nargs, *force, error, i;
477 	struct g_geom *gp;
478 	const char *name;
479 	char param[16];
480 
481 	g_topology_assert();
482 
483 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
484 	if (nargs == NULL) {
485 		gctl_error(req, "No '%s' argument", "nargs");
486 		return;
487 	}
488 	if (*nargs <= 0) {
489 		gctl_error(req, "Missing device(s).");
490 		return;
491 	}
492 	force = gctl_get_paraml(req, "force", sizeof(*force));
493 	if (force == NULL) {
494 		gctl_error(req, "No 'force' argument");
495 		return;
496 	}
497 
498 	for (i = 0; i < *nargs; i++) {
499 		snprintf(param, sizeof(param), "arg%d", i);
500 		name = gctl_get_asciiparam(req, param);
501 		if (name == NULL) {
502 			gctl_error(req, "No 'arg%d' argument", i);
503 			return;
504 		}
505 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
506 			name += strlen("/dev/");
507 		gp = g_nop_find_geom(mp, name);
508 		if (gp == NULL) {
509 			G_NOP_DEBUG(1, "Device %s is invalid.", name);
510 			gctl_error(req, "Device %s is invalid.", name);
511 			return;
512 		}
513 		error = g_nop_destroy(gp, *force);
514 		if (error != 0) {
515 			gctl_error(req, "Cannot destroy device %s (error=%d).",
516 			    gp->name, error);
517 			return;
518 		}
519 	}
520 }
521 
522 static void
523 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
524 {
525 	struct g_nop_softc *sc;
526 	struct g_provider *pp;
527 	const char *name;
528 	char param[16];
529 	int i, *nargs;
530 
531 	g_topology_assert();
532 
533 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
534 	if (nargs == NULL) {
535 		gctl_error(req, "No '%s' argument", "nargs");
536 		return;
537 	}
538 	if (*nargs <= 0) {
539 		gctl_error(req, "Missing device(s).");
540 		return;
541 	}
542 
543 	for (i = 0; i < *nargs; i++) {
544 		snprintf(param, sizeof(param), "arg%d", i);
545 		name = gctl_get_asciiparam(req, param);
546 		if (name == NULL) {
547 			gctl_error(req, "No 'arg%d' argument", i);
548 			return;
549 		}
550 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
551 			name += strlen("/dev/");
552 		pp = g_provider_by_name(name);
553 		if (pp == NULL || pp->geom->class != mp) {
554 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
555 			gctl_error(req, "Provider %s is invalid.", name);
556 			return;
557 		}
558 		sc = pp->geom->softc;
559 		sc->sc_reads = 0;
560 		sc->sc_writes = 0;
561 		sc->sc_readbytes = 0;
562 		sc->sc_wrotebytes = 0;
563 	}
564 }
565 
566 static void
567 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
568 {
569 	uint32_t *version;
570 
571 	g_topology_assert();
572 
573 	version = gctl_get_paraml(req, "version", sizeof(*version));
574 	if (version == NULL) {
575 		gctl_error(req, "No '%s' argument.", "version");
576 		return;
577 	}
578 	if (*version != G_NOP_VERSION) {
579 		gctl_error(req, "Userland and kernel parts are out of sync.");
580 		return;
581 	}
582 
583 	if (strcmp(verb, "create") == 0) {
584 		g_nop_ctl_create(req, mp);
585 		return;
586 	} else if (strcmp(verb, "configure") == 0) {
587 		g_nop_ctl_configure(req, mp);
588 		return;
589 	} else if (strcmp(verb, "destroy") == 0) {
590 		g_nop_ctl_destroy(req, mp);
591 		return;
592 	} else if (strcmp(verb, "reset") == 0) {
593 		g_nop_ctl_reset(req, mp);
594 		return;
595 	}
596 
597 	gctl_error(req, "Unknown verb.");
598 }
599 
600 static void
601 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
602     struct g_consumer *cp, struct g_provider *pp)
603 {
604 	struct g_nop_softc *sc;
605 
606 	if (pp != NULL || cp != NULL)
607 		return;
608 	sc = gp->softc;
609 	sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
610 	    (intmax_t)sc->sc_offset);
611 	sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
612 	    sc->sc_rfailprob);
613 	sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
614 	    sc->sc_wfailprob);
615 	sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
616 	sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
617 	sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
618 	sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
619 	    sc->sc_readbytes);
620 	sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
621 	    sc->sc_wrotebytes);
622 }
623 
624 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
625