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/label/g_label.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2004-2005 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: src/sys/geom/label/g_label.c,v 1.7.2.4 2005/03/01 14:22:30 pjd Exp $");
   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/geom_slice.h>
   41 #include <geom/label/g_label.h>
   42 
   43 
   44 SYSCTL_DECL(_kern_geom);
   45 SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW, 0, "GEOM_LABEL stuff");
   46 u_int g_label_debug = 0;
   47 TUNABLE_INT("kern.geom.label.debug", &g_label_debug);
   48 SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RW, &g_label_debug, 0,
   49     "Debug level");
   50 
   51 static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
   52     struct g_geom *gp);
   53 static int g_label_destroy(struct g_geom *gp, boolean_t force);
   54 static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
   55     int flags __unused);
   56 static void g_label_config(struct gctl_req *req, struct g_class *mp,
   57     const char *verb);
   58 
   59 struct g_class g_label_class = {
   60         .name = G_LABEL_CLASS_NAME,
   61         .version = G_VERSION,
   62         .ctlreq = g_label_config,
   63         .taste = g_label_taste,
   64         .destroy_geom = g_label_destroy_geom
   65 };
   66 
   67 /*
   68  * To add a new file system where you want to look for volume labels,
   69  * you have to:
   70  * 1. Add a file which implements looking for volume labels.
   71  * 2. Add an 'extern const struct g_label_desc g_label_<your file system>;'.
   72  * 3. Add an element to the table below '&g_label_<your_file_system>,'.
   73  */
   74 const struct g_label_desc *g_labels[] = {
   75         &g_label_ufs,
   76         &g_label_iso9660,
   77         &g_label_msdosfs,
   78         NULL
   79 };
   80 
   81 
   82 static int
   83 g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
   84     struct g_geom *gp __unused)
   85 {
   86 
   87         /*
   88          * XXX: Unloading a class which is using geom_slice:1.56 is currently
   89          * XXX: broken, so we deny unloading when we have geoms.
   90          */
   91         return (EOPNOTSUPP);
   92 }
   93 
   94 static void
   95 g_label_orphan(struct g_consumer *cp __unused)
   96 {
   97 
   98         KASSERT(1 == 0, ("%s called?", __func__));
   99 }
  100 
  101 static void
  102 g_label_start(struct bio *bp __unused)
  103 {
  104 
  105         KASSERT(1 == 0, ("%s called?", __func__));
  106 }
  107 
  108 static int
  109 g_label_access(struct g_provider *pp __unused, int dr __unused, int dw __unused,
  110     int de __unused)
  111 {
  112 
  113         KASSERT(1 == 0, ("%s called", __func__));
  114         return (EOPNOTSUPP);
  115 }
  116 
  117 static struct g_geom *
  118 g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
  119     const char *label, const char *dir, off_t mediasize)
  120 {
  121         struct g_geom *gp;
  122         struct g_provider *pp2;
  123         struct g_consumer *cp;
  124         char name[64];
  125 
  126         g_topology_assert();
  127 
  128         gp = NULL;
  129         cp = NULL;
  130         snprintf(name, sizeof(name), "%s/%s", dir, label);
  131         LIST_FOREACH(gp, &mp->geom, geom) {
  132                 pp2 = LIST_FIRST(&gp->provider);
  133                 if (pp2 == NULL)
  134                         continue;
  135                 if (strcmp(pp2->name, name) == 0) {
  136                         G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
  137                             label, name, pp->name);
  138                         if (req != NULL) {
  139                                 gctl_error(req, "Provider %s already exists.",
  140                                     name);
  141                         }
  142                         return (NULL);
  143                 }
  144         }
  145         gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL);
  146         if (gp == NULL) {
  147                 G_LABEL_DEBUG(0, "Cannot create slice %s.", label);
  148                 if (req != NULL)
  149                         gctl_error(req, "Cannot create slice %s.", label);
  150                 return (NULL);
  151         }
  152         g_access(cp, -1, 0, 0);
  153         g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize,
  154             pp->sectorsize, name);
  155         G_LABEL_DEBUG(0, "Label for provider %s is %s.", pp->name, name);
  156         return (gp);
  157 }
  158 
  159 static int
  160 g_label_destroy(struct g_geom *gp, boolean_t force)
  161 {
  162         struct g_provider *pp;
  163 
  164         g_topology_assert();
  165         pp = LIST_FIRST(&gp->provider);
  166         if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
  167                 if (force) {
  168                         G_LABEL_DEBUG(0, "Provider %s is still open, so it "
  169                             "can't be definitely removed.", pp->name);
  170                 } else {
  171                         G_LABEL_DEBUG(1,
  172                             "Provider %s is still open (r%dw%de%d).", pp->name,
  173                             pp->acr, pp->acw, pp->ace);
  174                         return (EBUSY);
  175                 }
  176         } else {
  177                 G_LABEL_DEBUG(0, "Label %s removed.",
  178                     LIST_FIRST(&gp->provider)->name);
  179         }
  180         g_slice_spoiled(LIST_FIRST(&gp->consumer));
  181         return (0);
  182 }
  183 
  184 static int
  185 g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md)
  186 {
  187         struct g_provider *pp;
  188         u_char *buf;
  189         int error;
  190 
  191         g_topology_assert();
  192 
  193         pp = cp->provider;
  194         g_topology_unlock();
  195         buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
  196             &error);
  197         g_topology_lock();
  198         if (buf == NULL)
  199                 return (error);
  200         /* Decode metadata. */
  201         label_metadata_decode(buf, md);
  202         g_free(buf);
  203 
  204         return (0);
  205 }
  206 
  207 static struct g_geom *
  208 g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
  209 {
  210         struct g_label_metadata md;
  211         struct g_consumer *cp;
  212         struct g_geom *gp;
  213         int i;
  214 
  215         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
  216         g_topology_assert();
  217 
  218         G_LABEL_DEBUG(2, "Tasting %s.", pp->name);
  219 
  220         if (strcmp(pp->geom->class->name, mp->name) == 0)
  221                 return (NULL);
  222 
  223         gp = g_new_geomf(mp, "label:taste");
  224         gp->start = g_label_start;
  225         gp->access = g_label_access;
  226         gp->orphan = g_label_orphan;
  227         cp = g_new_consumer(gp);
  228         g_attach(cp, pp);
  229         if (g_access(cp, 1, 0, 0) != 0)
  230                 goto end;
  231         do {
  232                 if (g_label_read_metadata(cp, &md) != 0)
  233                         break;
  234                 if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0)
  235                         break;
  236                 if (md.md_version > G_LABEL_VERSION) {
  237                         printf("geom_label.ko module is too old to handle %s.\n",
  238                             pp->name);
  239                         break;
  240                 }
  241 
  242                 /*
  243                  * Backward compatibility:
  244                  */
  245                 /*
  246                  * There was no md_provsize field in earlier versions of
  247                  * metadata.
  248                  */
  249                 if (md.md_version < 2)
  250                         md.md_provsize = pp->mediasize;
  251 
  252                 if (md.md_provsize != pp->mediasize)
  253                         break;
  254 
  255                 g_label_create(NULL, mp, pp, md.md_label, G_LABEL_DIR,
  256                     pp->mediasize - pp->sectorsize);
  257         } while (0);
  258         for (i = 0; g_labels[i] != NULL; i++) {
  259                 char label[64];
  260 
  261                 g_topology_unlock();
  262                 g_labels[i]->ld_taste(cp, label, sizeof(label));
  263                 g_topology_lock();
  264                 if (label[0] == '\0')
  265                         continue;
  266                 g_label_create(NULL, mp, pp, label, g_labels[i]->ld_dir,
  267                     pp->mediasize);
  268         }
  269         g_access(cp, -1, 0, 0);
  270 end:
  271         g_detach(cp);
  272         g_destroy_consumer(cp);
  273         g_destroy_geom(gp);
  274         return (NULL);
  275 }
  276 
  277 static void
  278 g_label_ctl_create(struct gctl_req *req, struct g_class *mp)
  279 {
  280         struct g_provider *pp;
  281         const char *name;
  282         int *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 != 2) {
  292                 gctl_error(req, "Invalid number of argument.");
  293                 return;
  294         }
  295         /*
  296          * arg1 is the name of provider.
  297          */
  298         name = gctl_get_asciiparam(req, "arg1"); 
  299         if (name == NULL) {
  300                 gctl_error(req, "No 'arg%d' argument", 1);
  301                 return;
  302         }
  303         if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
  304                 name += strlen("/dev/");
  305         pp = g_provider_by_name(name);
  306         if (pp == NULL) {
  307                 G_LABEL_DEBUG(1, "Provider %s is invalid.", name);
  308                 gctl_error(req, "Provider %s is invalid.", name);
  309                 return; 
  310         }
  311         /*
  312          * arg0 is the label.
  313          */
  314         name = gctl_get_asciiparam(req, "arg0"); 
  315         if (name == NULL) {
  316                 gctl_error(req, "No 'arg%d' argument", 0);
  317                 return;
  318         }
  319         g_label_create(req, mp, pp, name, G_LABEL_DIR, pp->mediasize);
  320 }
  321 
  322 static const char *
  323 g_label_skip_dir(const char *name)
  324 {
  325         char path[64];
  326         u_int i;
  327 
  328         if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
  329                 name += strlen("/dev/");
  330         if (strncmp(name, G_LABEL_DIR "/", strlen(G_LABEL_DIR "/")) == 0)
  331                 name += strlen(G_LABEL_DIR "/");
  332         for (i = 0; g_labels[i] != NULL; i++) {
  333                 snprintf(path, sizeof(path), "%s/", g_labels[i]->ld_dir);
  334                 if (strncmp(name, path, strlen(path)) == 0) {
  335                         name += strlen(path);
  336                         break;
  337                 }
  338         }
  339         return (name);
  340 }
  341 
  342 static struct g_geom *
  343 g_label_find_geom(struct g_class *mp, const char *name)
  344 {
  345         struct g_geom *gp;
  346         struct g_provider *pp;
  347         const char *pname;
  348 
  349         name = g_label_skip_dir(name);
  350         LIST_FOREACH(gp, &mp->geom, geom) {
  351                 pp = LIST_FIRST(&gp->provider);
  352                 pname = g_label_skip_dir(pp->name);
  353                 if (strcmp(pname, name) == 0)
  354                         return (gp);
  355         }
  356         return (NULL);
  357 }
  358 
  359 static void
  360 g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp)
  361 {
  362         int *nargs, *force, error, i;
  363         struct g_geom *gp;
  364         const char *name;
  365         char param[16];
  366 
  367         g_topology_assert();
  368 
  369         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
  370         if (nargs == NULL) {
  371                 gctl_error(req, "No '%s' argument", "nargs");
  372                 return;
  373         }
  374         if (*nargs <= 0) {
  375                 gctl_error(req, "Missing device(s).");
  376                 return;
  377         }
  378         force = gctl_get_paraml(req, "force", sizeof(*force));
  379         if (force == NULL) {
  380                 gctl_error(req, "No 'force' argument");
  381                 return;
  382         }
  383 
  384         for (i = 0; i < *nargs; i++) {
  385                 snprintf(param, sizeof(param), "arg%d", i);
  386                 name = gctl_get_asciiparam(req, param); 
  387                 if (name == NULL) {
  388                         gctl_error(req, "No 'arg%d' argument", i);
  389                         return;
  390                 }
  391                 gp = g_label_find_geom(mp, name);
  392                 if (gp == NULL) {
  393                         G_LABEL_DEBUG(1, "Label %s is invalid.", name);
  394                         gctl_error(req, "Label %s is invalid.", name);
  395                         return; 
  396                 }
  397                 error = g_label_destroy(gp, *force);
  398                 if (error != 0) {
  399                         gctl_error(req, "Cannot destroy label %s (error=%d).",
  400                             LIST_FIRST(&gp->provider)->name, error);
  401                         return;
  402                 }
  403         }
  404 }
  405 
  406 static void
  407 g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb)
  408 {
  409         uint32_t *version;
  410 
  411         g_topology_assert();
  412 
  413         version = gctl_get_paraml(req, "version", sizeof(*version));
  414         if (version == NULL) {
  415                 gctl_error(req, "No '%s' argument.", "version");
  416                 return;
  417         }
  418         if (*version != G_LABEL_VERSION) {
  419                 gctl_error(req, "Userland and kernel parts are out of sync.");
  420                 return;
  421         }
  422 
  423         if (strcmp(verb, "create") == 0) {
  424                 g_label_ctl_create(req, mp);
  425                 return;
  426         } else if (strcmp(verb, "destroy") == 0 ||
  427             strcmp(verb, "stop") == 0) {
  428                 g_label_ctl_destroy(req, mp);
  429                 return;
  430         }
  431 
  432         gctl_error(req, "Unknown verb.");
  433 }
  434 
  435 DECLARE_GEOM_CLASS(g_label_class, g_label);

Cache object: ea4c155bbe83a6f69491d7a9c1c1b63a


[ 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.