xref: /freebsd/sys/geom/nop/g_nop.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*-
2  * Copyright (c) 2004 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/sysctl.h>
38 #include <sys/malloc.h>
39 #include <geom/geom.h>
40 #include <geom/nop/g_nop.h>
41 
42 
43 SYSCTL_DECL(_kern_geom);
44 SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff");
45 static u_int g_nop_debug = 0;
46 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0,
47     "Debug level");
48 
49 static int g_nop_destroy(struct g_geom *gp, boolean_t force);
50 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp,
51     struct g_geom *gp);
52 static void g_nop_config(struct gctl_req *req, struct g_class *mp,
53     const char *verb);
54 static void g_nop_dumpconf(struct sbuf *sb, const char *indent,
55     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
56 
57 struct g_class g_nop_class = {
58 	.name = G_NOP_CLASS_NAME,
59 	.ctlreq = g_nop_config,
60 	.destroy_geom = g_nop_destroy_geom
61 };
62 
63 
64 static void
65 g_nop_orphan(struct g_consumer *cp)
66 {
67 
68 	g_topology_assert();
69 	g_nop_destroy(cp->geom, 1);
70 }
71 
72 static void
73 g_nop_start(struct bio *bp)
74 {
75 	struct g_nop_softc *sc;
76 	struct g_geom *gp;
77 	struct g_provider *pp;
78 	struct bio *cbp;
79 
80 	gp = bp->bio_to->geom;
81 	sc = gp->softc;
82 	G_NOP_LOGREQ(bp, "Request received.");
83 	cbp = g_clone_bio(bp);
84 	if (cbp == NULL) {
85 		g_io_deliver(bp, ENOMEM);
86 		return;
87 	}
88 	pp = LIST_FIRST(&gp->provider);
89 	KASSERT(pp != NULL, ("NULL pp"));
90 	if (sc->sc_failprob > 0) {
91 		u_int rval;
92 
93 		rval = arc4random() % 100;
94 		if (rval < sc->sc_failprob) {
95 			g_io_deliver(bp, EIO);
96 			return;
97 		}
98 	}
99 	cbp->bio_done = g_std_done;
100 	cbp->bio_offset = bp->bio_offset + sc->sc_offset;
101 	cbp->bio_data = bp->bio_data;
102 	cbp->bio_length = bp->bio_length;
103 	cbp->bio_to = LIST_FIRST(&gp->provider);
104 	G_NOP_LOGREQ(cbp, "Sending request.");
105 	g_io_request(cbp, LIST_FIRST(&gp->consumer));
106 }
107 
108 static int
109 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
110 {
111 	struct g_geom *gp;
112 	struct g_consumer *cp;
113 	int error;
114 
115 	gp = pp->geom;
116 	cp = LIST_FIRST(&gp->consumer);
117 	error = g_access(cp, dr, dw, de);
118 
119 	return (error);
120 }
121 
122 static int
123 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
124     u_int failprob, off_t offset, off_t size, u_int secsize)
125 {
126 	struct g_nop_softc *sc;
127 	struct g_geom *gp;
128 	struct g_provider *newpp;
129 	struct g_consumer *cp;
130 	char name[64];
131 	int error;
132 
133 	g_topology_assert();
134 
135 	gp = NULL;
136 	newpp = NULL;
137 	cp = NULL;
138 
139 	if ((offset % pp->sectorsize) != 0) {
140 		gctl_error(req, "Invalid offset for provider %s.", pp->name);
141 		return (EINVAL);
142 	}
143 	if ((size % pp->sectorsize) != 0) {
144 		gctl_error(req, "Invalid size for provider %s.", pp->name);
145 		return (EINVAL);
146 	}
147 	if (offset >= pp->mediasize) {
148 		gctl_error(req, "Invalid offset for provider %s.", pp->name);
149 		return (EINVAL);
150 	}
151 	if (size == 0)
152 		size = pp->mediasize - offset;
153 	if (offset + size > pp->mediasize) {
154 		gctl_error(req, "Invalid size for provider %s.", pp->name);
155 		return (EINVAL);
156 	}
157 	if (secsize == 0)
158 		secsize = pp->sectorsize;
159 	else if ((secsize % pp->sectorsize) != 0) {
160 		gctl_error(req, "Invalid secsize for provider %s.", pp->name);
161 		return (EINVAL);
162 	}
163 	snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
164 	LIST_FOREACH(gp, &mp->geom, geom) {
165 		if (strcmp(gp->name, name) == 0) {
166 			gctl_error(req, "Provider %s already exists.", name);
167 			return (EEXIST);
168 		}
169 	}
170 	gp = g_new_geomf(mp, name);
171 	if (gp == NULL) {
172 		gctl_error(req, "Cannot create geom %s.", name);
173 		return (ENOMEM);
174 	}
175 	sc = g_malloc(sizeof(*sc), M_WAITOK);
176 	sc->sc_offset = offset;
177 	sc->sc_failprob = failprob;
178 	gp->softc = sc;
179 	gp->start = g_nop_start;
180 	gp->spoiled = g_nop_orphan;
181 	gp->orphan = g_nop_orphan;
182 	gp->access = g_nop_access;
183 	gp->dumpconf = g_nop_dumpconf;
184 
185 	newpp = g_new_providerf(gp, gp->name);
186 	if (newpp == NULL) {
187 		gctl_error(req, "Cannot create provider %s.", name);
188 		error = ENOMEM;
189 		goto fail;
190 	}
191 	newpp->mediasize = size;
192 	newpp->sectorsize = secsize;
193 
194 	cp = g_new_consumer(gp);
195 	if (cp == NULL) {
196 		gctl_error(req, "Cannot create consumer for %s.", gp->name);
197 		error = ENOMEM;
198 		goto fail;
199 	}
200 	error = g_attach(cp, pp);
201 	if (error != 0) {
202 		gctl_error(req, "Cannot attach to provider %s.", pp->name);
203 		goto fail;
204 	}
205 
206 	g_error_provider(newpp, 0);
207 	G_NOP_DEBUG(0, "Device %s created.", gp->name);
208 	return (0);
209 fail:
210 	if (cp != NULL) {
211 		if (cp->provider != NULL)
212 			g_detach(cp);
213 		g_destroy_consumer(cp);
214 	}
215 	if (newpp != NULL)
216 		g_destroy_provider(pp);
217 	if (gp != NULL) {
218 		if (gp->softc != NULL)
219 			g_free(gp->softc);
220 		g_destroy_geom(gp);
221 	}
222 	return (error);
223 }
224 
225 static int
226 g_nop_destroy(struct g_geom *gp, boolean_t force)
227 {
228 	struct g_provider *pp;
229 
230 	g_topology_assert();
231 	pp = LIST_FIRST(&gp->provider);
232 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
233 		if (force) {
234 			G_NOP_DEBUG(0, "Device %s is still open, so it "
235 			    "can't be definitely removed.", pp->name);
236 		} else {
237 			G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
238 			    pp->name, pp->acr, pp->acw, pp->ace);
239 			return (EBUSY);
240 		}
241 	} else {
242 		G_NOP_DEBUG(0, "Device %s removed.", gp->name);
243 	}
244 	g_free(gp->softc);
245 	gp->softc = NULL;
246 	g_wither_geom(gp, ENXIO);
247 
248 	return (0);
249 }
250 
251 static int
252 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
253 {
254 
255 	return (g_nop_destroy(gp, 0));
256 }
257 
258 static void
259 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
260 {
261 	struct g_provider *pp;
262 	intmax_t *failprob, *offset, *secsize, *size;
263 	const char *name;
264 	char param[16];
265 	int i, *nargs;
266 
267 	g_topology_assert();
268 
269 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
270 	if (nargs == NULL) {
271 		gctl_error(req, "No '%s' argument", "nargs");
272 		return;
273 	}
274 	if (*nargs <= 0) {
275 		gctl_error(req, "Missing device(s).");
276 		return;
277 	}
278 	failprob = gctl_get_paraml(req, "failprob", sizeof(*failprob));
279 	if (failprob == NULL) {
280 		gctl_error(req, "No '%s' argument", "failprob");
281 		return;
282 	}
283 	if (*failprob < 0 || *failprob > 100) {
284 		gctl_error(req, "Invalid '%s' argument", "failprob");
285 		return;
286 	}
287 	offset = gctl_get_paraml(req, "offset", sizeof(*offset));
288 	if (offset == NULL) {
289 		gctl_error(req, "No '%s' argument", "offset");
290 		return;
291 	}
292 	if (*offset < 0) {
293 		gctl_error(req, "Invalid '%s' argument", "offset");
294 		return;
295 	}
296 	size = gctl_get_paraml(req, "size", sizeof(*size));
297 	if (size == NULL) {
298 		gctl_error(req, "No '%s' argument", "size");
299 		return;
300 	}
301 	if (*size < 0) {
302 		gctl_error(req, "Invalid '%s' argument", "size");
303 		return;
304 	}
305 	secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
306 	if (secsize == NULL) {
307 		gctl_error(req, "No '%s' argument", "secsize");
308 		return;
309 	}
310 	if (*secsize < 0) {
311 		gctl_error(req, "Invalid '%s' argument", "secsize");
312 		return;
313 	}
314 
315 	for (i = 0; i < *nargs; i++) {
316 		snprintf(param, sizeof(param), "arg%d", i);
317 		name = gctl_get_asciiparam(req, param);
318 		if (name == NULL) {
319 			gctl_error(req, "No 'arg%d' argument", i);
320 			return;
321 		}
322 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
323 			name += strlen("/dev/");
324 		pp = g_provider_by_name(name);
325 		if (pp == NULL) {
326 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
327 			gctl_error(req, "Provider %s is invalid.", name);
328 			return;
329 		}
330 		if (g_nop_create(req, mp, pp, (u_int)*failprob, (off_t)*offset,
331 		    (off_t)*size, (u_int)*secsize) != 0) {
332 			return;
333 		}
334 	}
335 }
336 
337 static void
338 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
339 {
340 	struct g_nop_softc *sc;
341 	struct g_provider *pp;
342 	intmax_t *failprob;
343 	const char *name;
344 	char param[16];
345 	int i, *nargs;
346 
347 	g_topology_assert();
348 
349 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
350 	if (nargs == NULL) {
351 		gctl_error(req, "No '%s' argument", "nargs");
352 		return;
353 	}
354 	if (*nargs <= 0) {
355 		gctl_error(req, "Missing device(s).");
356 		return;
357 	}
358 	failprob = gctl_get_paraml(req, "failprob", sizeof(*failprob));
359 	if (failprob == NULL) {
360 		gctl_error(req, "No '%s' argument", "failprob");
361 		return;
362 	}
363 	if (*failprob < 0 || *failprob > 100) {
364 		gctl_error(req, "Invalid '%s' argument", "failprob");
365 		return;
366 	}
367 
368 	for (i = 0; i < *nargs; i++) {
369 		snprintf(param, sizeof(param), "arg%d", i);
370 		name = gctl_get_asciiparam(req, param);
371 		if (name == NULL) {
372 			gctl_error(req, "No 'arg%d' argument", i);
373 			return;
374 		}
375 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
376 			name += strlen("/dev/");
377 		pp = g_provider_by_name(name);
378 		if (pp == NULL || pp->geom->class != mp) {
379 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
380 			gctl_error(req, "Provider %s is invalid.", name);
381 			return;
382 		}
383 		sc = pp->geom->softc;
384 		sc->sc_failprob = (u_int)*failprob;
385 	}
386 }
387 
388 static struct g_geom *
389 g_nop_find_geom(struct g_class *mp, const char *name)
390 {
391 	struct g_geom *gp;
392 
393 	LIST_FOREACH(gp, &mp->geom, geom) {
394 		if (strcmp(gp->name, name) == 0)
395 			return (gp);
396 	}
397 	return (NULL);
398 }
399 
400 static void
401 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
402 {
403 	int *nargs, *force, error, i;
404 	struct g_geom *gp;
405 	const char *name;
406 	char param[16];
407 
408 	g_topology_assert();
409 
410 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
411 	if (nargs == NULL) {
412 		gctl_error(req, "No '%s' argument", "nargs");
413 		return;
414 	}
415 	if (*nargs <= 0) {
416 		gctl_error(req, "Missing device(s).");
417 		return;
418 	}
419 	force = gctl_get_paraml(req, "force", sizeof(*force));
420 	if (force == NULL) {
421 		gctl_error(req, "No 'force' argument");
422 		return;
423 	}
424 
425 	for (i = 0; i < *nargs; i++) {
426 		snprintf(param, sizeof(param), "arg%d", i);
427 		name = gctl_get_asciiparam(req, param);
428 		if (name == NULL) {
429 			gctl_error(req, "No 'arg%d' argument", i);
430 			return;
431 		}
432 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
433 			name += strlen("/dev/");
434 		gp = g_nop_find_geom(mp, name);
435 		if (gp == NULL) {
436 			G_NOP_DEBUG(1, "Device %s is invalid.", name);
437 			gctl_error(req, "Device %s is invalid.", name);
438 			return;
439 		}
440 		error = g_nop_destroy(gp, *force);
441 		if (error != 0) {
442 			gctl_error(req, "Cannot destroy device %s (error=%d).",
443 			    gp->name, error);
444 			return;
445 		}
446 	}
447 }
448 
449 static void
450 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
451 {
452 	uint32_t *version;
453 
454 	g_topology_assert();
455 
456 	version = gctl_get_paraml(req, "version", sizeof(*version));
457 	if (version == NULL) {
458 		gctl_error(req, "No '%s' argument.", "version");
459 		return;
460 	}
461 	if (*version != G_NOP_VERSION) {
462 		gctl_error(req, "Userland and kernel parts are out of sync.");
463 		return;
464 	}
465 
466 	if (strcmp(verb, "create") == 0) {
467 		g_nop_ctl_create(req, mp);
468 		return;
469 	} else if (strcmp(verb, "configure") == 0) {
470 		g_nop_ctl_configure(req, mp);
471 		return;
472 	} else if (strcmp(verb, "destroy") == 0) {
473 		g_nop_ctl_destroy(req, mp);
474 		return;
475 	}
476 
477 	gctl_error(req, "Unknown verb.");
478 }
479 
480 static void
481 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
482     struct g_consumer *cp, struct g_provider *pp)
483 {
484 	struct g_nop_softc *sc;
485 
486 	if (pp != NULL || cp != NULL)
487 		return;
488 	sc = gp->softc;
489 	sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
490 	    (intmax_t)sc->sc_offset);
491 	sbuf_printf(sb, "%s<Failprob>%u</Failprob>\n", indent, sc->sc_failprob);
492 }
493 
494 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
495