The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_ctl.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2002 Poul-Henning Kamp
    3  * Copyright (c) 2002 Networks Associates Technology, Inc.
    4  * All rights reserved.
    5  *
    6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
    7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
    8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
    9  * DARPA CHATS research program.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. The names of the authors may not be used to endorse or promote
   20  *    products derived from this software without specific prior written
   21  *    permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD: releng/5.2/sys/geom/geom_ctl.c 120506 2003-09-27 12:01:01Z phk $");
   38 
   39 #include "opt_geom.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/kernel.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/bio.h>
   46 #include <sys/conf.h>
   47 #include <sys/disk.h>
   48 #include <sys/malloc.h>
   49 #include <sys/sysctl.h>
   50 #include <sys/sbuf.h>
   51 
   52 #include <sys/lock.h>
   53 #include <sys/mutex.h>
   54 
   55 #include <vm/vm.h>
   56 #include <vm/vm_extern.h>
   57 
   58 #include <geom/geom.h>
   59 #include <geom/geom_int.h>
   60 #define GCTL_TABLE 1
   61 #include <geom/geom_ctl.h>
   62 
   63 #include <machine/stdarg.h>
   64 
   65 static d_ioctl_t g_ctl_ioctl;
   66 
   67 static struct cdevsw g_ctl_cdevsw = {
   68         .d_ioctl =      g_ctl_ioctl,
   69         .d_name =       "g_ctl",
   70 };
   71 
   72 void
   73 g_ctl_init(void)
   74 {
   75 
   76         make_dev(&g_ctl_cdevsw, 0,
   77             UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
   78         KASSERT(GCTL_PARAM_RD == VM_PROT_READ,
   79                 ("GCTL_PARAM_RD != VM_PROT_READ"));
   80         KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE,
   81                 ("GCTL_PARAM_WR != VM_PROT_WRITE"));
   82 }
   83 
   84 /*
   85  * Report an error back to the user in ascii format.  Return whatever copyout
   86  * returned, or EINVAL if it succeeded.
   87  * XXX: should not be static.
   88  * XXX: should take printf like args.
   89  */
   90 int
   91 gctl_error(struct gctl_req *req, const char *fmt, ...)
   92 {
   93         va_list ap;
   94 
   95         if (req == NULL)
   96                 return (EINVAL);
   97 
   98         /* We only record the first error */
   99         if (req->nerror)
  100                 return (req->nerror);
  101 
  102         va_start(ap, fmt);
  103         sbuf_vprintf(req->serror, fmt, ap);
  104         va_end(ap);
  105         sbuf_finish(req->serror);
  106         if (g_debugflags & G_F_CTLDUMP)
  107                 printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror));
  108         req->nerror = copyout(sbuf_data(req->serror), req->error,
  109             imin(req->lerror, sbuf_len(req->serror) + 1));
  110         if (!req->nerror)
  111                 req->nerror = EINVAL;
  112         return (req->nerror);
  113 }
  114 
  115 /*
  116  * Allocate space and copyin() something.
  117  * XXX: this should really be a standard function in the kernel.
  118  */
  119 static void *
  120 geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
  121 {
  122         void *ptr;
  123 
  124         ptr = g_malloc(len, M_WAITOK);
  125         if (ptr == NULL)
  126                 req->nerror = ENOMEM;
  127         else
  128                 req->nerror = copyin(uaddr, ptr, len);
  129         if (!req->nerror)
  130                 return (ptr);
  131         if (ptr != NULL)
  132                 g_free(ptr);
  133         return (NULL);
  134 }
  135 
  136 static void
  137 gctl_copyin(struct gctl_req *req)
  138 {
  139         int error, i;
  140         struct gctl_req_arg *ap;
  141         char *p;
  142 
  143         ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
  144         if (ap == NULL) {
  145                 req->nerror = ENOMEM;
  146                 req->arg = NULL;
  147                 return;
  148         }
  149 
  150         /* Nothing have been copyin()'ed yet */
  151         for (i = 0; i < req->narg; i++) {
  152                 ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
  153                 ap[i].flag &= ~GCTL_PARAM_CHANGED;
  154                 ap[i].kvalue = NULL;
  155         }
  156 
  157         error = 0;
  158         for (i = 0; i < req->narg; i++) {
  159                 if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
  160                         error = gctl_error(req,
  161                             "wrong param name length %d: %d", i, ap[i].nlen);
  162                         break;
  163                 }
  164                 p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
  165                 if (p == NULL)
  166                         break;
  167                 if (p[ap[i].nlen - 1] != '\0') {
  168                         error = gctl_error(req, "unterminated param name");
  169                         g_free(p);
  170                         break;
  171                 }
  172                 ap[i].name = p;
  173                 ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
  174                 if (ap[i].len < 0) {
  175                         error = gctl_error(req, "negative param length");
  176                         break;
  177                 }
  178                 if (ap[i].len == 0) {
  179                         ap[i].kvalue = ap[i].value;
  180                         ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
  181                         continue;
  182                 }
  183                 p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
  184                 if (p == NULL)
  185                         break;
  186                 if ((ap[i].flag & GCTL_PARAM_ASCII) &&
  187                     p[ap[i].len - 1] != '\0') {
  188                         error = gctl_error(req, "unterminated param value");
  189                         g_free(p);
  190                         break;
  191                 }
  192                 ap[i].kvalue = p;
  193                 ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
  194         }
  195         req->arg = ap;
  196         return;
  197 }
  198 
  199 static void
  200 gctl_copyout(struct gctl_req *req)
  201 {
  202         int error, i;
  203         struct gctl_req_arg *ap;
  204 
  205         if (req->nerror)
  206                 return;
  207         error = 0;
  208         ap = req->arg;
  209         for (i = 0; i < req->narg; i++, ap++) {
  210                 if (!(ap->flag & GCTL_PARAM_CHANGED))
  211                         continue;
  212                 error = copyout(ap->kvalue, ap->value, ap->len);
  213                 if (!error)
  214                         continue;
  215                 req->nerror = error;
  216                 return;
  217         }
  218         return;
  219 }
  220 
  221 static void
  222 gctl_free(struct gctl_req *req)
  223 {
  224         int i;
  225 
  226         if (req->arg == NULL)
  227                 return;
  228         for (i = 0; i < req->narg; i++) {
  229                 if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
  230                         g_free(req->arg[i].name);
  231                 if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
  232                     req->arg[i].len > 0)
  233                         g_free(req->arg[i].kvalue);
  234         }
  235         g_free(req->arg);
  236         sbuf_delete(req->serror);
  237 }
  238 
  239 static void
  240 gctl_dump(struct gctl_req *req)
  241 {
  242         u_int i;
  243         int j;
  244         struct gctl_req_arg *ap;
  245 
  246         printf("Dump of gctl request at %p:\n", req);
  247         if (req->nerror > 0) {
  248                 printf("  nerror:\t%d\n", req->nerror);
  249                 if (sbuf_len(req->serror) > 0)
  250                         printf("  error:\t\"%s\"\n", sbuf_data(req->serror));
  251         }
  252         for (i = 0; i < req->narg; i++) {
  253                 ap = &req->arg[i];
  254                 if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
  255                         printf("  param:\t%d@%p", ap->nlen, ap->name);
  256                 else
  257                         printf("  param:\t\"%s\"", ap->name);
  258                 printf(" [%s%s%d] = ",
  259                     ap->flag & GCTL_PARAM_RD ? "R" : "",
  260                     ap->flag & GCTL_PARAM_WR ? "W" : "",
  261                     ap->len);
  262                 if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
  263                         printf(" =@ %p", ap->value);
  264                 } else if (ap->flag & GCTL_PARAM_ASCII) {
  265                         printf("\"%s\"", (char *)ap->kvalue);
  266                 } else if (ap->len > 0) {
  267                         for (j = 0; j < ap->len && j < 512; j++)
  268                                 printf(" %02x", ((u_char *)ap->kvalue)[j]);
  269                 } else {
  270                         printf(" = %p", ap->kvalue);
  271                 }
  272                 printf("\n");
  273         }
  274 }
  275 
  276 void
  277 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len)
  278 {
  279         int i;
  280         struct gctl_req_arg *ap;
  281 
  282         for (i = 0; i < req->narg; i++) {
  283                 ap = &req->arg[i];
  284                 if (strcmp(param, ap->name))
  285                         continue;
  286                 if (!(ap->flag & GCTL_PARAM_WR)) {
  287                         gctl_error(req, "No write access %s argument", param);
  288                         return;
  289                 }
  290                 if (ap->len < len) {
  291                         gctl_error(req, "Wrong length %s argument", param);
  292                         return;
  293                 }
  294                 bcopy(ptr, ap->kvalue, len);
  295                 ap->flag |= GCTL_PARAM_CHANGED;
  296                 return;
  297         }
  298         gctl_error(req, "Missing %s argument", param);
  299         return;
  300 }
  301 
  302 void *
  303 gctl_get_param(struct gctl_req *req, const char *param, int *len)
  304 {
  305         int i;
  306         void *p;
  307         struct gctl_req_arg *ap;
  308 
  309         for (i = 0; i < req->narg; i++) {
  310                 ap = &req->arg[i];
  311                 if (strcmp(param, ap->name))
  312                         continue;
  313                 if (!(ap->flag & GCTL_PARAM_RD))
  314                         continue;
  315                 p = ap->kvalue;
  316                 if (len != NULL)
  317                         *len = ap->len;
  318                 return (p);
  319         }
  320         return (NULL);
  321 }
  322 
  323 char const *
  324 gctl_get_asciiparam(struct gctl_req *req, const char *param)
  325 {
  326         int i;
  327         char const *p;
  328         struct gctl_req_arg *ap;
  329 
  330         for (i = 0; i < req->narg; i++) {
  331                 ap = &req->arg[i];
  332                 if (strcmp(param, ap->name))
  333                         continue;
  334                 if (!(ap->flag & GCTL_PARAM_RD))
  335                         continue;
  336                 p = ap->kvalue;
  337                 if (ap->len < 1) {
  338                         gctl_error(req, "No length argument (%s)", param);
  339                         return (NULL);
  340                 }
  341                 if (p[ap->len - 1] != '\0') {
  342                         gctl_error(req, "Unterminated argument (%s)", param);
  343                         return (NULL);
  344                 }
  345                 return (p);
  346         }
  347         return (NULL);
  348 }
  349 
  350 void *
  351 gctl_get_paraml(struct gctl_req *req, const char *param, int len)
  352 {
  353         int i;
  354         void *p;
  355 
  356         p = gctl_get_param(req, param, &i);
  357         if (p == NULL)
  358                 gctl_error(req, "Missing %s argument", param);
  359         else if (i != len) {
  360                 p = NULL;
  361                 gctl_error(req, "Wrong length %s argument", param);
  362         }
  363         return (p);
  364 }
  365 
  366 struct g_class *
  367 gctl_get_class(struct gctl_req *req, char const *arg)
  368 {
  369         char const *p;
  370         struct g_class *cp;
  371 
  372         p = gctl_get_asciiparam(req, arg);
  373         if (p == NULL)
  374                 return (NULL);
  375         LIST_FOREACH(cp, &g_classes, class) {
  376                 if (!strcmp(p, cp->name))
  377                         return (cp);
  378         }
  379         gctl_error(req, "Class not found");
  380         return (NULL);
  381 }
  382 
  383 struct g_geom *
  384 gctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg)
  385 {
  386         char const *p;
  387         struct g_class *mp;
  388         struct g_geom *gp;
  389 
  390         p = gctl_get_asciiparam(req, arg);
  391         if (p != NULL) {
  392                 LIST_FOREACH(mp, &g_classes, class) {
  393                         if (mpr != NULL && mpr != mp)
  394                                 continue;
  395                         LIST_FOREACH(gp, &mp->geom, geom) {
  396                                 if (!strcmp(p, gp->name))
  397                                         return (gp);
  398                         }
  399                 }
  400         }
  401         gctl_error(req, "Geom not found");
  402         return (NULL);
  403 }
  404 
  405 struct g_provider *
  406 gctl_get_provider(struct gctl_req *req, char const *arg)
  407 {
  408         char const *p;
  409         struct g_provider *pp;
  410 
  411         p = gctl_get_asciiparam(req, arg);
  412         if (p == NULL)
  413                 return (NULL);
  414         pp = g_provider_by_name(p);
  415         if (pp != NULL)
  416                 return (pp);
  417         gctl_error(req, "Provider not found");
  418         return (NULL);
  419 }
  420 
  421 static void
  422 g_ctl_req(void *arg, int flag __unused)
  423 {
  424         struct g_class *mp;
  425         struct gctl_req *req;
  426         char const *verb;
  427 
  428         g_topology_assert();
  429         req = arg;
  430         mp = gctl_get_class(req, "class");
  431         if (mp == NULL) {
  432                 gctl_error(req, "Class not found");
  433                 return;
  434         }
  435         verb = gctl_get_param(req, "verb", NULL);
  436         if (mp->ctlreq == NULL)
  437                 gctl_error(req, "Class takes no requests");
  438         else
  439                 mp->ctlreq(req, mp, verb);
  440         g_topology_assert();
  441 }
  442 
  443 
  444 static int
  445 g_ctl_ioctl_ctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
  446 {
  447         struct gctl_req *req;
  448 
  449         req = (void *)data;
  450         req->nerror = 0;
  451         req->serror = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
  452         /* It is an error if we cannot return an error text */
  453         if (req->lerror < 2)
  454                 return (EINVAL);
  455         if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
  456                 return (EINVAL);
  457 
  458         /* Check the version */
  459         if (req->version != GCTL_VERSION)
  460                 return (gctl_error(req,
  461                     "kernel and libgeom version mismatch."));
  462         
  463         /* Get things on board */
  464         gctl_copyin(req);
  465 
  466         if (g_debugflags & G_F_CTLDUMP)
  467                 gctl_dump(req);
  468 
  469         if (!req->nerror) {
  470                 g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
  471                 gctl_copyout(req);
  472         }
  473 
  474         g_waitidle();
  475         gctl_free(req);
  476         return (req->nerror);
  477 }
  478 
  479 static int
  480 g_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
  481 {
  482         int error;
  483 
  484         switch(cmd) {
  485         case GEOM_CTL:
  486                 error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
  487                 break;
  488         default:
  489                 error = ENOIOCTL;
  490                 break;
  491         }
  492         return (error);
  493 
  494 }

Cache object: d46e3b2dac38e5aa226974db5b0f0d42


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.