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