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