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/part/g_part.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, 2005-2009 Marcel Moolenaar
    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  *
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/8.0/sys/geom/part/g_part.c 198003 2009-10-12 21:08:06Z pjd $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/bio.h>
   32 #include <sys/diskmbr.h>
   33 #include <sys/endian.h>
   34 #include <sys/kernel.h>
   35 #include <sys/kobj.h>
   36 #include <sys/limits.h>
   37 #include <sys/lock.h>
   38 #include <sys/malloc.h>
   39 #include <sys/mutex.h>
   40 #include <sys/queue.h>
   41 #include <sys/sbuf.h>
   42 #include <sys/systm.h>
   43 #include <sys/uuid.h>
   44 #include <geom/geom.h>
   45 #include <geom/geom_ctl.h>
   46 #include <geom/geom_int.h>
   47 #include <geom/part/g_part.h>
   48 
   49 #include "g_part_if.h"
   50 
   51 #ifndef _PATH_DEV
   52 #define _PATH_DEV "/dev/"
   53 #endif
   54 
   55 static kobj_method_t g_part_null_methods[] = {
   56         { 0, 0 }
   57 };
   58 
   59 static struct g_part_scheme g_part_null_scheme = {
   60         "(none)",
   61         g_part_null_methods,
   62         sizeof(struct g_part_table),
   63 };
   64 
   65 TAILQ_HEAD(, g_part_scheme) g_part_schemes =
   66     TAILQ_HEAD_INITIALIZER(g_part_schemes);
   67 
   68 struct g_part_alias_list {
   69         const char *lexeme;
   70         enum g_part_alias alias;
   71 } g_part_alias_list[G_PART_ALIAS_COUNT] = {
   72         { "apple-hfs", G_PART_ALIAS_APPLE_HFS },
   73         { "efi", G_PART_ALIAS_EFI },
   74         { "freebsd", G_PART_ALIAS_FREEBSD },
   75         { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
   76         { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
   77         { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
   78         { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
   79         { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
   80         { "mbr", G_PART_ALIAS_MBR }
   81 };
   82 
   83 /*
   84  * The GEOM partitioning class.
   85  */
   86 static g_ctl_req_t g_part_ctlreq;
   87 static g_ctl_destroy_geom_t g_part_destroy_geom;
   88 static g_fini_t g_part_fini;
   89 static g_init_t g_part_init;
   90 static g_taste_t g_part_taste;
   91 
   92 static g_access_t g_part_access;
   93 static g_dumpconf_t g_part_dumpconf;
   94 static g_orphan_t g_part_orphan;
   95 static g_spoiled_t g_part_spoiled;
   96 static g_start_t g_part_start;
   97 
   98 static struct g_class g_part_class = {
   99         .name = "PART",
  100         .version = G_VERSION,
  101         /* Class methods. */
  102         .ctlreq = g_part_ctlreq,
  103         .destroy_geom = g_part_destroy_geom,
  104         .fini = g_part_fini,
  105         .init = g_part_init,
  106         .taste = g_part_taste,
  107         /* Geom methods. */
  108         .access = g_part_access,
  109         .dumpconf = g_part_dumpconf,
  110         .orphan = g_part_orphan,
  111         .spoiled = g_part_spoiled,
  112         .start = g_part_start,
  113 };
  114 
  115 DECLARE_GEOM_CLASS(g_part_class, g_part);
  116 
  117 /*
  118  * Support functions.
  119  */
  120 
  121 static void g_part_wither(struct g_geom *, int);
  122 
  123 const char *
  124 g_part_alias_name(enum g_part_alias alias)
  125 {
  126         int i;
  127 
  128         for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
  129                 if (g_part_alias_list[i].alias != alias)
  130                         continue;
  131                 return (g_part_alias_list[i].lexeme);
  132         }
  133 
  134         return (NULL);
  135 }
  136 
  137 void
  138 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
  139     u_int *bestheads)
  140 {
  141         static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
  142         off_t chs, cylinders;
  143         u_int heads;
  144         int idx;
  145 
  146         *bestchs = 0;
  147         *bestheads = 0;
  148         for (idx = 0; candidate_heads[idx] != 0; idx++) {
  149                 heads = candidate_heads[idx];
  150                 cylinders = blocks / heads / sectors;
  151                 if (cylinders < heads || cylinders < sectors)
  152                         break;
  153                 if (cylinders > 1023)
  154                         continue;
  155                 chs = cylinders * heads * sectors;
  156                 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
  157                         *bestchs = chs;
  158                         *bestheads = heads;
  159                 }
  160         }
  161 }
  162 
  163 static void
  164 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
  165     off_t blocks)
  166 {
  167         static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
  168         off_t chs, bestchs;
  169         u_int heads, sectors;
  170         int idx;
  171 
  172         if (g_getattr("GEOM::fwsectors", cp, &sectors) != 0 || sectors == 0 ||
  173             g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
  174                 table->gpt_fixgeom = 0;
  175                 table->gpt_heads = 0;
  176                 table->gpt_sectors = 0;
  177                 bestchs = 0;
  178                 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
  179                         sectors = candidate_sectors[idx];
  180                         g_part_geometry_heads(blocks, sectors, &chs, &heads);
  181                         if (chs == 0)
  182                                 continue;
  183                         /*
  184                          * Prefer a geometry with sectors > 1, but only if
  185                          * it doesn't bump down the numbver of heads to 1.
  186                          */
  187                         if (chs > bestchs || (chs == bestchs && heads > 1 &&
  188                             table->gpt_sectors == 1)) {
  189                                 bestchs = chs;
  190                                 table->gpt_heads = heads;
  191                                 table->gpt_sectors = sectors;
  192                         }
  193                 }
  194                 /*
  195                  * If we didn't find a geometry at all, then the disk is
  196                  * too big. This means we can use the maximum number of
  197                  * heads and sectors.
  198                  */
  199                 if (bestchs == 0) {
  200                         table->gpt_heads = 255;
  201                         table->gpt_sectors = 63;
  202                 }
  203         } else {
  204                 table->gpt_fixgeom = 1;
  205                 table->gpt_heads = heads;
  206                 table->gpt_sectors = sectors;
  207         }
  208 }
  209 
  210 struct g_part_entry *
  211 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
  212     quad_t end)
  213 {
  214         struct g_part_entry *entry, *last;
  215 
  216         last = NULL;
  217         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
  218                 if (entry->gpe_index == index)
  219                         break;
  220                 if (entry->gpe_index > index) {
  221                         entry = NULL;
  222                         break;
  223                 }
  224                 last = entry;
  225         }
  226         if (entry == NULL) {
  227                 entry = g_malloc(table->gpt_scheme->gps_entrysz,
  228                     M_WAITOK | M_ZERO);
  229                 entry->gpe_index = index;
  230                 if (last == NULL)
  231                         LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
  232                 else
  233                         LIST_INSERT_AFTER(last, entry, gpe_entry);
  234         } else
  235                 entry->gpe_offset = 0;
  236         entry->gpe_start = start;
  237         entry->gpe_end = end;
  238         return (entry);
  239 }
  240 
  241 static void
  242 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
  243     struct g_part_entry *entry)
  244 {
  245         struct g_consumer *cp;
  246         struct g_provider *pp;
  247         struct sbuf *sb;
  248         off_t offset;
  249 
  250         cp = LIST_FIRST(&gp->consumer);
  251         pp = cp->provider;
  252 
  253         offset = entry->gpe_start * pp->sectorsize;
  254         if (entry->gpe_offset < offset)
  255                 entry->gpe_offset = offset;
  256 
  257         if (entry->gpe_pp == NULL) {
  258                 sb = sbuf_new_auto();
  259                 G_PART_FULLNAME(table, entry, sb, gp->name);
  260                 sbuf_finish(sb);
  261                 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
  262                 sbuf_delete(sb);
  263                 entry->gpe_pp->private = entry;         /* Close the circle. */
  264         }
  265         entry->gpe_pp->index = entry->gpe_index - 1;    /* index is 1-based. */
  266         entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
  267             pp->sectorsize;
  268         entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
  269         entry->gpe_pp->sectorsize = pp->sectorsize;
  270         entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE;
  271         if (pp->stripesize > 0) {
  272                 entry->gpe_pp->stripesize = pp->stripesize;
  273                 entry->gpe_pp->stripeoffset = (pp->stripeoffset +
  274                     entry->gpe_offset) % pp->stripesize;
  275         }
  276         g_error_provider(entry->gpe_pp, 0);
  277 }
  278 
  279 static int
  280 g_part_parm_geom(const char *rawname, struct g_geom **v)
  281 {
  282         struct g_geom *gp;
  283         const char *pname;
  284 
  285         if (strncmp(rawname, _PATH_DEV, strlen(_PATH_DEV)) == 0)
  286                 pname = rawname + strlen(_PATH_DEV);
  287         else
  288                 pname = rawname;
  289         LIST_FOREACH(gp, &g_part_class.geom, geom) {
  290                 if (!strcmp(pname, gp->name))
  291                         break;
  292         }
  293         if (gp == NULL)
  294                 return (EINVAL);
  295         *v = gp;
  296         return (0);
  297 }
  298 
  299 static int
  300 g_part_parm_provider(const char *pname, struct g_provider **v)
  301 {
  302         struct g_provider *pp;
  303 
  304         if (strncmp(pname, _PATH_DEV, strlen(_PATH_DEV)) == 0)
  305                 pp = g_provider_by_name(pname + strlen(_PATH_DEV));
  306         else
  307                 pp = g_provider_by_name(pname);
  308         if (pp == NULL)
  309                 return (EINVAL);
  310         *v = pp;
  311         return (0);
  312 }
  313 
  314 static int
  315 g_part_parm_quad(const char *p, quad_t *v)
  316 {
  317         char *x;
  318         quad_t q;
  319 
  320         q = strtoq(p, &x, 0);
  321         if (*x != '\0' || q < 0)
  322                 return (EINVAL);
  323         *v = q;
  324         return (0);
  325 }
  326 
  327 static int
  328 g_part_parm_scheme(const char *p, struct g_part_scheme **v)
  329 {
  330         struct g_part_scheme *s;
  331 
  332         TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
  333                 if (s == &g_part_null_scheme)
  334                         continue;
  335                 if (!strcasecmp(s->name, p))
  336                         break;
  337         }
  338         if (s == NULL)
  339                 return (EINVAL);
  340         *v = s;
  341         return (0);
  342 }
  343 
  344 static int
  345 g_part_parm_str(const char *p, const char **v)
  346 {
  347 
  348         if (p[0] == '\0')
  349                 return (EINVAL);
  350         *v = p;
  351         return (0);
  352 }
  353 
  354 static int
  355 g_part_parm_uint(const char *p, u_int *v)
  356 {
  357         char *x;
  358         long l;
  359 
  360         l = strtol(p, &x, 0);
  361         if (*x != '\0' || l < 0 || l > INT_MAX)
  362                 return (EINVAL);
  363         *v = (unsigned int)l;
  364         return (0);
  365 }
  366 
  367 static int
  368 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
  369 {
  370         struct g_part_scheme *iter, *scheme;
  371         struct g_part_table *table;
  372         int pri, probe;
  373 
  374         table = gp->softc;
  375         scheme = (table != NULL) ? table->gpt_scheme : NULL;
  376         pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
  377         if (pri == 0)
  378                 goto done;
  379         if (pri > 0) {  /* error */
  380                 scheme = NULL;
  381                 pri = INT_MIN;
  382         }
  383 
  384         TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
  385                 if (iter == &g_part_null_scheme)
  386                         continue;
  387                 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
  388                     M_WAITOK);
  389                 table->gpt_gp = gp;
  390                 table->gpt_scheme = iter;
  391                 table->gpt_depth = depth;
  392                 probe = G_PART_PROBE(table, cp);
  393                 if (probe <= 0 && probe > pri) {
  394                         pri = probe;
  395                         scheme = iter;
  396                         if (gp->softc != NULL)
  397                                 kobj_delete((kobj_t)gp->softc, M_GEOM);
  398                         gp->softc = table;
  399                         if (pri == 0)
  400                                 goto done;
  401                 } else
  402                         kobj_delete((kobj_t)table, M_GEOM);
  403         }
  404 
  405 done:
  406         return ((scheme == NULL) ? ENXIO : 0);
  407 }
  408 
  409 /*
  410  * Control request functions.
  411  */
  412 
  413 static int
  414 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
  415 {
  416         struct g_geom *gp;
  417         struct g_provider *pp;
  418         struct g_part_entry *delent, *last, *entry;
  419         struct g_part_table *table;
  420         struct sbuf *sb;
  421         quad_t end;
  422         unsigned int index;
  423         int error;
  424 
  425         gp = gpp->gpp_geom;
  426         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
  427         g_topology_assert();
  428 
  429         pp = LIST_FIRST(&gp->consumer)->provider;
  430         table = gp->softc;
  431         end = gpp->gpp_start + gpp->gpp_size - 1;
  432 
  433         if (gpp->gpp_start < table->gpt_first ||
  434             gpp->gpp_start > table->gpt_last) {
  435                 gctl_error(req, "%d start '%jd'", EINVAL,
  436                     (intmax_t)gpp->gpp_start);
  437                 return (EINVAL);
  438         }
  439         if (end < gpp->gpp_start || end > table->gpt_last) {
  440                 gctl_error(req, "%d size '%jd'", EINVAL,
  441                     (intmax_t)gpp->gpp_size);
  442                 return (EINVAL);
  443         }
  444         if (gpp->gpp_index > table->gpt_entries) {
  445                 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
  446                 return (EINVAL);
  447         }
  448 
  449         delent = last = NULL;
  450         index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
  451         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
  452                 if (entry->gpe_deleted) {
  453                         if (entry->gpe_index == index)
  454                                 delent = entry;
  455                         continue;
  456                 }
  457                 if (entry->gpe_index == index)
  458                         index = entry->gpe_index + 1;
  459                 if (entry->gpe_index < index)
  460                         last = entry;
  461                 if (entry->gpe_internal)
  462                         continue;
  463                 if (gpp->gpp_start >= entry->gpe_start &&
  464                     gpp->gpp_start <= entry->gpe_end) {
  465                         gctl_error(req, "%d start '%jd'", ENOSPC,
  466                             (intmax_t)gpp->gpp_start);
  467                         return (ENOSPC);
  468                 }
  469                 if (end >= entry->gpe_start && end <= entry->gpe_end) {
  470                         gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
  471                         return (ENOSPC);
  472                 }
  473                 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
  474                         gctl_error(req, "%d size '%jd'", ENOSPC,
  475                             (intmax_t)gpp->gpp_size);
  476                         return (ENOSPC);
  477                 }
  478         }
  479         if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
  480                 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
  481                 return (EEXIST);
  482         }
  483         if (index > table->gpt_entries) {
  484                 gctl_error(req, "%d index '%d'", ENOSPC, index);
  485                 return (ENOSPC);
  486         }
  487 
  488         entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
  489             M_WAITOK | M_ZERO) : delent;
  490         entry->gpe_index = index;
  491         entry->gpe_start = gpp->gpp_start;
  492         entry->gpe_end = end;
  493         error = G_PART_ADD(table, entry, gpp);
  494         if (error) {
  495                 gctl_error(req, "%d", error);
  496                 if (delent == NULL)
  497                         g_free(entry);
  498                 return (error);
  499         }
  500         if (delent == NULL) {
  501                 if (last == NULL)
  502                         LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
  503                 else
  504                         LIST_INSERT_AFTER(last, entry, gpe_entry);
  505                 entry->gpe_created = 1;
  506         } else {
  507                 entry->gpe_deleted = 0;
  508                 entry->gpe_modified = 1;
  509         }
  510         g_part_new_provider(gp, table, entry);
  511 
  512         /* Provide feedback if so requested. */
  513         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
  514                 sb = sbuf_new_auto();
  515                 G_PART_FULLNAME(table, entry, sb, gp->name);
  516                 sbuf_cat(sb, " added\n");
  517                 sbuf_finish(sb);
  518                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
  519                 sbuf_delete(sb);
  520         }
  521         return (0);
  522 }
  523 
  524 static int
  525 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
  526 {
  527         struct g_geom *gp;
  528         struct g_part_table *table;
  529         struct sbuf *sb;
  530         int error, sz;
  531 
  532         gp = gpp->gpp_geom;
  533         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
  534         g_topology_assert();
  535 
  536         table = gp->softc;
  537         sz = table->gpt_scheme->gps_bootcodesz;
  538         if (sz == 0) {
  539                 error = ENODEV;
  540                 goto fail;
  541         }
  542         if (gpp->gpp_codesize > sz) {
  543                 error = EFBIG;
  544                 goto fail;
  545         }
  546 
  547         error = G_PART_BOOTCODE(table, gpp);
  548         if (error)
  549                 goto fail;
  550 
  551         /* Provide feedback if so requested. */
  552         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
  553                 sb = sbuf_new_auto();
  554                 sbuf_printf(sb, "%s has bootcode\n", gp->name);
  555                 sbuf_finish(sb);
  556                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
  557                 sbuf_delete(sb);
  558         }
  559         return (0);
  560 
  561  fail:
  562         gctl_error(req, "%d", error);
  563         return (error);
  564 }
  565 
  566 static int
  567 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
  568 {
  569         struct g_consumer *cp;
  570         struct g_geom *gp;
  571         struct g_provider *pp;
  572         struct g_part_entry *entry, *tmp;
  573         struct g_part_table *table;
  574         char *buf;
  575         int error, i;
  576 
  577         gp = gpp->gpp_geom;
  578         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
  579         g_topology_assert();
  580 
  581         table = gp->softc;
  582         if (!table->gpt_opened) {
  583                 gctl_error(req, "%d", EPERM);
  584                 return (EPERM);
  585         }
  586 
  587         g_topology_unlock();
  588 
  589         cp = LIST_FIRST(&gp->consumer);
  590         if ((table->gpt_smhead | table->gpt_smtail) != 0) {
  591                 pp = cp->provider;
  592                 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
  593                 while (table->gpt_smhead != 0) {
  594                         i = ffs(table->gpt_smhead) - 1;
  595                         error = g_write_data(cp, i * pp->sectorsize, buf,
  596                             pp->sectorsize);
  597                         if (error) {
  598                                 g_free(buf);
  599                                 goto fail;
  600                         }
  601                         table->gpt_smhead &= ~(1 << i);
  602                 }
  603                 while (table->gpt_smtail != 0) {
  604                         i = ffs(table->gpt_smtail) - 1;
  605                         error = g_write_data(cp, pp->mediasize - (i + 1) *
  606                             pp->sectorsize, buf, pp->sectorsize);
  607                         if (error) {
  608                                 g_free(buf);
  609                                 goto fail;
  610                         }
  611                         table->gpt_smtail &= ~(1 << i);
  612                 }
  613                 g_free(buf);
  614         }
  615 
  616         if (table->gpt_scheme == &g_part_null_scheme) {
  617                 g_topology_lock();
  618                 g_access(cp, -1, -1, -1);
  619                 g_part_wither(gp, ENXIO);
  620                 return (0);
  621         }
  622 
  623         error = G_PART_WRITE(table, cp);
  624         if (error)
  625                 goto fail;
  626 
  627         LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
  628                 if (!entry->gpe_deleted) {
  629                         entry->gpe_created = 0;
  630                         entry->gpe_modified = 0;
  631                         continue;
  632                 }
  633                 LIST_REMOVE(entry, gpe_entry);
  634                 g_free(entry);
  635         }
  636         table->gpt_created = 0;
  637         table->gpt_opened = 0;
  638 
  639         g_topology_lock();
  640         g_access(cp, -1, -1, -1);
  641         return (0);
  642 
  643 fail:
  644         g_topology_lock();
  645         gctl_error(req, "%d", error);
  646         return (error);
  647 }
  648 
  649 static int
  650 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
  651 {
  652         struct g_consumer *cp;
  653         struct g_geom *gp;
  654         struct g_provider *pp;
  655         struct g_part_scheme *scheme;
  656         struct g_part_table *null, *table;
  657         struct sbuf *sb;
  658         int attr, error;
  659 
  660         pp = gpp->gpp_provider;
  661         scheme = gpp->gpp_scheme;
  662         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
  663         g_topology_assert();
  664 
  665         /* Check that there isn't already a g_part geom on the provider. */
  666         error = g_part_parm_geom(pp->name, &gp);
  667         if (!error) {
  668                 null = gp->softc;
  669                 if (null->gpt_scheme != &g_part_null_scheme) {
  670                         gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
  671                         return (EEXIST);
  672                 }
  673         } else
  674                 null = NULL;
  675 
  676         if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
  677             (gpp->gpp_entries < scheme->gps_minent ||
  678              gpp->gpp_entries > scheme->gps_maxent)) {
  679                 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
  680                 return (EINVAL);
  681         }
  682 
  683         if (null == NULL)
  684                 gp = g_new_geomf(&g_part_class, "%s", pp->name);
  685         gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
  686             M_WAITOK);
  687         table = gp->softc;
  688         table->gpt_gp = gp;
  689         table->gpt_scheme = gpp->gpp_scheme;
  690         table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
  691             gpp->gpp_entries : scheme->gps_minent;
  692         LIST_INIT(&table->gpt_entry);
  693         if (null == NULL) {
  694                 cp = g_new_consumer(gp);
  695                 error = g_attach(cp, pp);
  696                 if (error == 0)
  697                         error = g_access(cp, 1, 1, 1);
  698                 if (error != 0) {
  699                         g_part_wither(gp, error);
  700                         gctl_error(req, "%d geom '%s'", error, pp->name);
  701                         return (error);
  702                 }
  703                 table->gpt_opened = 1;
  704         } else {
  705                 cp = LIST_FIRST(&gp->consumer);
  706                 table->gpt_opened = null->gpt_opened;
  707                 table->gpt_smhead = null->gpt_smhead;
  708                 table->gpt_smtail = null->gpt_smtail;
  709         }
  710 
  711         g_topology_unlock();
  712 
  713         /* Make sure the provider has media. */
  714         if (pp->mediasize == 0 || pp->sectorsize == 0) {
  715                 error = ENODEV;
  716                 goto fail;
  717         }
  718 
  719         /* Make sure we can nest and if so, determine our depth. */
  720         error = g_getattr("PART::isleaf", cp, &attr);
  721         if (!error && attr) {
  722                 error = ENODEV;
  723                 goto fail;
  724         }
  725         error = g_getattr("PART::depth", cp, &attr);
  726         table->gpt_depth = (!error) ? attr + 1 : 0;
  727 
  728         /*
  729          * Synthesize a disk geometry. Some partitioning schemes
  730          * depend on it and since some file systems need it even
  731          * when the partitition scheme doesn't, we do it here in
  732          * scheme-independent code.
  733          */
  734         g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
  735 
  736         error = G_PART_CREATE(table, gpp);
  737         if (error)
  738                 goto fail;
  739 
  740         g_topology_lock();
  741 
  742         table->gpt_created = 1;
  743         if (null != NULL)
  744                 kobj_delete((kobj_t)null, M_GEOM);
  745 
  746         /*
  747          * Support automatic commit by filling in the gpp_geom
  748          * parameter.
  749          */
  750         gpp->gpp_parms |= G_PART_PARM_GEOM;
  751         gpp->gpp_geom = gp;
  752 
  753         /* Provide feedback if so requested. */
  754         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
  755                 sb = sbuf_new_auto();
  756                 sbuf_printf(sb, "%s created\n", gp->name);
  757                 sbuf_finish(sb);
  758                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
  759                 sbuf_delete(sb);
  760         }
  761         return (0);
  762 
  763 fail:
  764         g_topology_lock();
  765         if (null == NULL) {
  766                 g_access(cp, -1, -1, -1);
  767                 g_part_wither(gp, error);
  768         } else {
  769                 kobj_delete((kobj_t)gp->softc, M_GEOM);
  770                 gp->softc = null;
  771         }
  772         gctl_error(req, "%d provider", error);
  773         return (error);
  774 }
  775 
  776 static int
  777 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
  778 {
  779         struct g_geom *gp;
  780         struct g_provider *pp;
  781         struct g_part_entry *entry;
  782         struct g_part_table *table;
  783         struct sbuf *sb;
  784 
  785         gp = gpp->gpp_geom;
  786         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
  787         g_topology_assert();
  788 
  789         table = gp->softc;
  790 
  791         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
  792                 if (entry->gpe_deleted || entry->gpe_internal)
  793                         continue;
  794                 if (entry->gpe_index == gpp->gpp_index)
  795                         break;
  796         }
  797         if (entry == NULL) {
  798                 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
  799                 return (ENOENT);
  800         }
  801 
  802         pp = entry->gpe_pp;
  803         if (pp != NULL) {
  804                 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
  805                         gctl_error(req, "%d", EBUSY);
  806                         return (EBUSY);
  807                 }
  808 
  809                 pp->private = NULL;
  810                 entry->gpe_pp = NULL;
  811         }
  812 
  813         if (entry->gpe_created) {
  814                 LIST_REMOVE(entry, gpe_entry);
  815                 g_free(entry);
  816         } else {
  817                 entry->gpe_modified = 0;
  818                 entry->gpe_deleted = 1;
  819         }
  820 
  821         if (pp != NULL)
  822                 g_wither_provider(pp, ENXIO);
  823 
  824         /* Provide feedback if so requested. */
  825         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
  826                 sb = sbuf_new_auto();
  827                 G_PART_FULLNAME(table, entry, sb, gp->name);
  828                 sbuf_cat(sb, " deleted\n");
  829                 sbuf_finish(sb);
  830                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
  831                 sbuf_delete(sb);
  832         }
  833         return (0);
  834 }
  835 
  836 static int
  837 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
  838 {
  839         struct g_geom *gp;
  840         struct g_part_entry *entry;
  841         struct g_part_table *null, *table;
  842         struct sbuf *sb;
  843         int error;
  844 
  845         gp = gpp->gpp_geom;
  846         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
  847         g_topology_assert();
  848 
  849         table = gp->softc;
  850         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
  851                 if (entry->gpe_deleted || entry->gpe_internal)
  852                         continue;
  853                 gctl_error(req, "%d", EBUSY);
  854                 return (EBUSY);
  855         }
  856 
  857         error = G_PART_DESTROY(table, gpp);
  858         if (error) {
  859                 gctl_error(req, "%d", error);
  860                 return (error);
  861         }
  862 
  863         gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
  864             M_WAITOK);
  865         null = gp->softc;
  866         null->gpt_gp = gp;
  867         null->gpt_scheme = &g_part_null_scheme;
  868         LIST_INIT(&null->gpt_entry);
  869         null->gpt_depth = table->gpt_depth;
  870         null->gpt_opened = table->gpt_opened;
  871         null->gpt_smhead = table->gpt_smhead;
  872         null->gpt_smtail = table->gpt_smtail;
  873 
  874         while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
  875                 LIST_REMOVE(entry, gpe_entry);
  876                 g_free(entry);
  877         }
  878         kobj_delete((kobj_t)table, M_GEOM);
  879 
  880         /* Provide feedback if so requested. */
  881         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
  882                 sb = sbuf_new_auto();
  883                 sbuf_printf(sb, "%s destroyed\n", gp->name);
  884                 sbuf_finish(sb);
  885                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
  886                 sbuf_delete(sb);
  887         }
  888         return (0);
  889 }
  890 
  891 static int
  892 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
  893 {
  894         struct g_geom *gp;
  895         struct g_part_entry *entry;
  896         struct g_part_table *table;
  897         struct sbuf *sb;
  898         int error;
  899 
  900         gp = gpp->gpp_geom;
  901         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
  902         g_topology_assert();
  903 
  904         table = gp->softc;
  905 
  906         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
  907                 if (entry->gpe_deleted || entry->gpe_internal)
  908                         continue;
  909                 if (entry->gpe_index == gpp->gpp_index)
  910                         break;
  911         }
  912         if (entry == NULL) {
  913                 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
  914                 return (ENOENT);
  915         }
  916 
  917         error = G_PART_MODIFY(table, entry, gpp);
  918         if (error) {
  919                 gctl_error(req, "%d", error);
  920                 return (error);
  921         }
  922 
  923         if (!entry->gpe_created)
  924                 entry->gpe_modified = 1;
  925 
  926         /* Provide feedback if so requested. */
  927         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
  928                 sb = sbuf_new_auto();
  929                 G_PART_FULLNAME(table, entry, sb, gp->name);
  930                 sbuf_cat(sb, " modified\n");
  931                 sbuf_finish(sb);
  932                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
  933                 sbuf_delete(sb);
  934         }
  935         return (0);
  936 }
  937 
  938 static int
  939 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
  940 {
  941         gctl_error(req, "%d verb 'move'", ENOSYS);
  942         return (ENOSYS);
  943 } 
  944 
  945 static int
  946 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
  947 {
  948         gctl_error(req, "%d verb 'recover'", ENOSYS);
  949         return (ENOSYS);
  950 }
  951 
  952 static int
  953 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
  954 {
  955         gctl_error(req, "%d verb 'resize'", ENOSYS);
  956         return (ENOSYS);
  957 } 
  958 
  959 static int
  960 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
  961     unsigned int set)
  962 {
  963         struct g_geom *gp;
  964         struct g_part_entry *entry;
  965         struct g_part_table *table;
  966         struct sbuf *sb;
  967         int error;
  968 
  969         gp = gpp->gpp_geom;
  970         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
  971         g_topology_assert();
  972 
  973         table = gp->softc;
  974 
  975         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
  976                 if (entry->gpe_deleted || entry->gpe_internal)
  977                         continue;
  978                 if (entry->gpe_index == gpp->gpp_index)
  979                         break;
  980         }
  981         if (entry == NULL) {
  982                 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
  983                 return (ENOENT);
  984         }
  985 
  986         error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
  987         if (error) {
  988                 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
  989                 return (error);
  990         }
  991 
  992         /* Provide feedback if so requested. */
  993         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
  994                 sb = sbuf_new_auto();
  995                 G_PART_FULLNAME(table, entry, sb, gp->name);
  996                 sbuf_printf(sb, " has %s %sset\n", gpp->gpp_attrib,
  997                     (set) ? "" : "un");
  998                 sbuf_finish(sb);
  999                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
 1000                 sbuf_delete(sb);
 1001         }
 1002         return (0);
 1003 }
 1004 
 1005 static int
 1006 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
 1007 {
 1008         struct g_consumer *cp;
 1009         struct g_provider *pp;
 1010         struct g_geom *gp;
 1011         struct g_part_entry *entry, *tmp;
 1012         struct g_part_table *table;
 1013         int error, reprobe;
 1014 
 1015         gp = gpp->gpp_geom;
 1016         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
 1017         g_topology_assert();
 1018 
 1019         table = gp->softc;
 1020         if (!table->gpt_opened) {
 1021                 gctl_error(req, "%d", EPERM);
 1022                 return (EPERM);
 1023         }
 1024 
 1025         cp = LIST_FIRST(&gp->consumer);
 1026         LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
 1027                 entry->gpe_modified = 0;
 1028                 if (entry->gpe_created) {
 1029                         pp = entry->gpe_pp;
 1030                         if (pp != NULL) {
 1031                                 pp->private = NULL;
 1032                                 entry->gpe_pp = NULL;
 1033                                 g_wither_provider(pp, ENXIO);
 1034                         }
 1035                         entry->gpe_deleted = 1;
 1036                 }
 1037                 if (entry->gpe_deleted) {
 1038                         LIST_REMOVE(entry, gpe_entry);
 1039                         g_free(entry);
 1040                 }
 1041         }
 1042 
 1043         g_topology_unlock();
 1044 
 1045         reprobe = (table->gpt_scheme == &g_part_null_scheme ||
 1046             table->gpt_created) ? 1 : 0;
 1047 
 1048         if (reprobe) {
 1049                 if (!LIST_EMPTY(&table->gpt_entry)) {
 1050                         error = EBUSY;
 1051                         goto fail;
 1052                 }
 1053                 error = g_part_probe(gp, cp, table->gpt_depth);
 1054                 if (error) {
 1055                         g_topology_lock();
 1056                         g_access(cp, -1, -1, -1);
 1057                         g_part_wither(gp, error);
 1058                         return (0);
 1059                 }
 1060                 table = gp->softc;
 1061         }
 1062 
 1063         error = G_PART_READ(table, cp);
 1064         if (error)
 1065                 goto fail;
 1066 
 1067         g_topology_lock();
 1068 
 1069         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
 1070                 if (!entry->gpe_internal)
 1071                         g_part_new_provider(gp, table, entry);
 1072         }
 1073 
 1074         table->gpt_opened = 0;
 1075         g_access(cp, -1, -1, -1);
 1076         return (0);
 1077 
 1078 fail:
 1079         g_topology_lock();
 1080         gctl_error(req, "%d", error);
 1081         return (error);
 1082 }
 1083 
 1084 static void
 1085 g_part_wither(struct g_geom *gp, int error)
 1086 {
 1087         struct g_part_entry *entry;
 1088         struct g_part_table *table;
 1089 
 1090         table = gp->softc;
 1091         if (table != NULL) {
 1092                 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
 1093                         LIST_REMOVE(entry, gpe_entry);
 1094                         g_free(entry);
 1095                 }
 1096                 if (gp->softc != NULL) {
 1097                         kobj_delete((kobj_t)gp->softc, M_GEOM);
 1098                         gp->softc = NULL;
 1099                 }
 1100         }
 1101         g_wither_geom(gp, error);
 1102 }
 1103 
 1104 /*
 1105  * Class methods.
 1106  */
 1107 
 1108 static void
 1109 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
 1110 {
 1111         struct g_part_parms gpp;
 1112         struct g_part_table *table;
 1113         struct gctl_req_arg *ap;
 1114         const char *p;
 1115         enum g_part_ctl ctlreq;
 1116         unsigned int i, mparms, oparms, parm;
 1117         int auto_commit, close_on_error;
 1118         int error, len, modifies;
 1119 
 1120         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
 1121         g_topology_assert();
 1122 
 1123         ctlreq = G_PART_CTL_NONE;
 1124         modifies = 1;
 1125         mparms = 0;
 1126         oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
 1127         switch (*verb) {
 1128         case 'a':
 1129                 if (!strcmp(verb, "add")) {
 1130                         ctlreq = G_PART_CTL_ADD;
 1131                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
 1132                             G_PART_PARM_START | G_PART_PARM_TYPE;
 1133                         oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
 1134                 }
 1135                 break;
 1136         case 'b':
 1137                 if (!strcmp(verb, "bootcode")) {
 1138                         ctlreq = G_PART_CTL_BOOTCODE;
 1139                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
 1140                 }
 1141                 break;
 1142         case 'c':
 1143                 if (!strcmp(verb, "commit")) {
 1144                         ctlreq = G_PART_CTL_COMMIT;
 1145                         mparms |= G_PART_PARM_GEOM;
 1146                         modifies = 0;
 1147                 } else if (!strcmp(verb, "create")) {
 1148                         ctlreq = G_PART_CTL_CREATE;
 1149                         mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
 1150                         oparms |= G_PART_PARM_ENTRIES;
 1151                 }
 1152                 break;
 1153         case 'd':
 1154                 if (!strcmp(verb, "delete")) {
 1155                         ctlreq = G_PART_CTL_DELETE;
 1156                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
 1157                 } else if (!strcmp(verb, "destroy")) {
 1158                         ctlreq = G_PART_CTL_DESTROY;
 1159                         mparms |= G_PART_PARM_GEOM;
 1160                 }
 1161                 break;
 1162         case 'm':
 1163                 if (!strcmp(verb, "modify")) {
 1164                         ctlreq = G_PART_CTL_MODIFY;
 1165                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
 1166                         oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
 1167                 } else if (!strcmp(verb, "move")) {
 1168                         ctlreq = G_PART_CTL_MOVE;
 1169                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
 1170                 }
 1171                 break;
 1172         case 'r':
 1173                 if (!strcmp(verb, "recover")) {
 1174                         ctlreq = G_PART_CTL_RECOVER;
 1175                         mparms |= G_PART_PARM_GEOM;
 1176                 } else if (!strcmp(verb, "resize")) {
 1177                         ctlreq = G_PART_CTL_RESIZE;
 1178                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
 1179                 }
 1180                 break;
 1181         case 's':
 1182                 if (!strcmp(verb, "set")) {
 1183                         ctlreq = G_PART_CTL_SET;
 1184                         mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
 1185                             G_PART_PARM_INDEX;
 1186                 }
 1187                 break;
 1188         case 'u':
 1189                 if (!strcmp(verb, "undo")) {
 1190                         ctlreq = G_PART_CTL_UNDO;
 1191                         mparms |= G_PART_PARM_GEOM;
 1192                         modifies = 0;
 1193                 } else if (!strcmp(verb, "unset")) {
 1194                         ctlreq = G_PART_CTL_UNSET;
 1195                         mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
 1196                             G_PART_PARM_INDEX;
 1197                 }
 1198                 break;
 1199         }
 1200         if (ctlreq == G_PART_CTL_NONE) {
 1201                 gctl_error(req, "%d verb '%s'", EINVAL, verb);
 1202                 return;
 1203         }
 1204 
 1205         bzero(&gpp, sizeof(gpp));
 1206         for (i = 0; i < req->narg; i++) {
 1207                 ap = &req->arg[i];
 1208                 parm = 0;
 1209                 switch (ap->name[0]) {
 1210                 case 'a':
 1211                         if (!strcmp(ap->name, "attrib"))
 1212                                 parm = G_PART_PARM_ATTRIB;
 1213                         break;
 1214                 case 'b':
 1215                         if (!strcmp(ap->name, "bootcode"))
 1216                                 parm = G_PART_PARM_BOOTCODE;
 1217                         break;
 1218                 case 'c':
 1219                         if (!strcmp(ap->name, "class"))
 1220                                 continue;
 1221                         break;
 1222                 case 'e':
 1223                         if (!strcmp(ap->name, "entries"))
 1224                                 parm = G_PART_PARM_ENTRIES;
 1225                         break;
 1226                 case 'f':
 1227                         if (!strcmp(ap->name, "flags"))
 1228                                 parm = G_PART_PARM_FLAGS;
 1229                         break;
 1230                 case 'g':
 1231                         if (!strcmp(ap->name, "geom"))
 1232                                 parm = G_PART_PARM_GEOM;
 1233                         break;
 1234                 case 'i':
 1235                         if (!strcmp(ap->name, "index"))
 1236                                 parm = G_PART_PARM_INDEX;
 1237                         break;
 1238                 case 'l':
 1239                         if (!strcmp(ap->name, "label"))
 1240                                 parm = G_PART_PARM_LABEL;
 1241                         break;
 1242                 case 'o':
 1243                         if (!strcmp(ap->name, "output"))
 1244                                 parm = G_PART_PARM_OUTPUT;
 1245                         break;
 1246                 case 'p':
 1247                         if (!strcmp(ap->name, "provider"))
 1248                                 parm = G_PART_PARM_PROVIDER;
 1249                         break;
 1250                 case 's':
 1251                         if (!strcmp(ap->name, "scheme"))
 1252                                 parm = G_PART_PARM_SCHEME;
 1253                         else if (!strcmp(ap->name, "size"))
 1254                                 parm = G_PART_PARM_SIZE;
 1255                         else if (!strcmp(ap->name, "start"))
 1256                                 parm = G_PART_PARM_START;
 1257                         break;
 1258                 case 't':
 1259                         if (!strcmp(ap->name, "type"))
 1260                                 parm = G_PART_PARM_TYPE;
 1261                         break;
 1262                 case 'v':
 1263                         if (!strcmp(ap->name, "verb"))
 1264                                 continue;
 1265                         else if (!strcmp(ap->name, "version"))
 1266                                 parm = G_PART_PARM_VERSION;
 1267                         break;
 1268                 }
 1269                 if ((parm & (mparms | oparms)) == 0) {
 1270                         gctl_error(req, "%d param '%s'", EINVAL, ap->name);
 1271                         return;
 1272                 }
 1273                 if (parm == G_PART_PARM_BOOTCODE)
 1274                         p = gctl_get_param(req, ap->name, &len);
 1275                 else
 1276                         p = gctl_get_asciiparam(req, ap->name);
 1277                 if (p == NULL) {
 1278                         gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
 1279                         return;
 1280                 }
 1281                 switch (parm) {
 1282                 case G_PART_PARM_ATTRIB:
 1283                         error = g_part_parm_str(p, &gpp.gpp_attrib);
 1284                         break;
 1285                 case G_PART_PARM_BOOTCODE:
 1286                         gpp.gpp_codeptr = p;
 1287                         gpp.gpp_codesize = len;
 1288                         error = 0;
 1289                         break;
 1290                 case G_PART_PARM_ENTRIES:
 1291                         error = g_part_parm_uint(p, &gpp.gpp_entries);
 1292                         break;
 1293                 case G_PART_PARM_FLAGS:
 1294                         if (p[0] == '\0')
 1295                                 continue;
 1296                         error = g_part_parm_str(p, &gpp.gpp_flags);
 1297                         break;
 1298                 case G_PART_PARM_GEOM:
 1299                         error = g_part_parm_geom(p, &gpp.gpp_geom);
 1300                         break;
 1301                 case G_PART_PARM_INDEX:
 1302                         error = g_part_parm_uint(p, &gpp.gpp_index);
 1303                         break;
 1304                 case G_PART_PARM_LABEL:
 1305                         /* An empty label is always valid. */
 1306                         gpp.gpp_label = p;
 1307                         error = 0;
 1308                         break;
 1309                 case G_PART_PARM_OUTPUT:
 1310                         error = 0;      /* Write-only parameter */
 1311                         break;
 1312                 case G_PART_PARM_PROVIDER:
 1313                         error = g_part_parm_provider(p, &gpp.gpp_provider);
 1314                         break;
 1315                 case G_PART_PARM_SCHEME:
 1316                         error = g_part_parm_scheme(p, &gpp.gpp_scheme);
 1317                         break;
 1318                 case G_PART_PARM_SIZE:
 1319                         error = g_part_parm_quad(p, &gpp.gpp_size);
 1320                         break;
 1321                 case G_PART_PARM_START:
 1322                         error = g_part_parm_quad(p, &gpp.gpp_start);
 1323                         break;
 1324                 case G_PART_PARM_TYPE:
 1325                         error = g_part_parm_str(p, &gpp.gpp_type);
 1326                         break;
 1327                 case G_PART_PARM_VERSION:
 1328                         error = g_part_parm_uint(p, &gpp.gpp_version);
 1329                         break;
 1330                 default:
 1331                         error = EDOOFUS;
 1332                         break;
 1333                 }
 1334                 if (error) {
 1335                         gctl_error(req, "%d %s '%s'", error, ap->name, p);
 1336                         return;
 1337                 }
 1338                 gpp.gpp_parms |= parm;
 1339         }
 1340         if ((gpp.gpp_parms & mparms) != mparms) {
 1341                 parm = mparms - (gpp.gpp_parms & mparms);
 1342                 gctl_error(req, "%d param '%x'", ENOATTR, parm);
 1343                 return;
 1344         }
 1345 
 1346         /* Obtain permissions if possible/necessary. */
 1347         close_on_error = 0;
 1348         table = NULL;
 1349         if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
 1350                 table = gpp.gpp_geom->softc;
 1351                 if (table != NULL && !table->gpt_opened) {
 1352                         error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
 1353                             1, 1, 1);
 1354                         if (error) {
 1355                                 gctl_error(req, "%d geom '%s'", error,
 1356                                     gpp.gpp_geom->name);
 1357                                 return;
 1358                         }
 1359                         table->gpt_opened = 1;
 1360                         close_on_error = 1;
 1361                 }
 1362         }
 1363 
 1364         /* Allow the scheme to check or modify the parameters. */
 1365         if (table != NULL) {
 1366                 error = G_PART_PRECHECK(table, ctlreq, &gpp);
 1367                 if (error) {
 1368                         gctl_error(req, "%d pre-check failed", error);
 1369                         goto out;
 1370                 }
 1371         } else
 1372                 error = EDOOFUS;        /* Prevent bogus uninit. warning. */
 1373 
 1374         switch (ctlreq) {
 1375         case G_PART_CTL_NONE:
 1376                 panic("%s", __func__);
 1377         case G_PART_CTL_ADD:
 1378                 error = g_part_ctl_add(req, &gpp);
 1379                 break;
 1380         case G_PART_CTL_BOOTCODE:
 1381                 error = g_part_ctl_bootcode(req, &gpp);
 1382                 break;
 1383         case G_PART_CTL_COMMIT:
 1384                 error = g_part_ctl_commit(req, &gpp);
 1385                 break;
 1386         case G_PART_CTL_CREATE:
 1387                 error = g_part_ctl_create(req, &gpp);
 1388                 break;
 1389         case G_PART_CTL_DELETE:
 1390                 error = g_part_ctl_delete(req, &gpp);
 1391                 break;
 1392         case G_PART_CTL_DESTROY:
 1393                 error = g_part_ctl_destroy(req, &gpp);
 1394                 break;
 1395         case G_PART_CTL_MODIFY:
 1396                 error = g_part_ctl_modify(req, &gpp);
 1397                 break;
 1398         case G_PART_CTL_MOVE:
 1399                 error = g_part_ctl_move(req, &gpp);
 1400                 break;
 1401         case G_PART_CTL_RECOVER:
 1402                 error = g_part_ctl_recover(req, &gpp);
 1403                 break;
 1404         case G_PART_CTL_RESIZE:
 1405                 error = g_part_ctl_resize(req, &gpp);
 1406                 break;
 1407         case G_PART_CTL_SET:
 1408                 error = g_part_ctl_setunset(req, &gpp, 1);
 1409                 break;
 1410         case G_PART_CTL_UNDO:
 1411                 error = g_part_ctl_undo(req, &gpp);
 1412                 break;
 1413         case G_PART_CTL_UNSET:
 1414                 error = g_part_ctl_setunset(req, &gpp, 0);
 1415                 break;
 1416         }
 1417 
 1418         /* Implement automatic commit. */
 1419         if (!error) {
 1420                 auto_commit = (modifies &&
 1421                     (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
 1422                     strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
 1423                 if (auto_commit) {
 1424                         KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
 1425                         error = g_part_ctl_commit(req, &gpp);
 1426                 }
 1427         }
 1428 
 1429  out:
 1430         if (error && close_on_error) {
 1431                 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
 1432                 table->gpt_opened = 0;
 1433         }
 1434 }
 1435 
 1436 static int
 1437 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
 1438     struct g_geom *gp)
 1439 {
 1440 
 1441         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
 1442         g_topology_assert();
 1443 
 1444         g_part_wither(gp, EINVAL);
 1445         return (0);
 1446 }
 1447 
 1448 static struct g_geom *
 1449 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
 1450 {
 1451         struct g_consumer *cp;
 1452         struct g_geom *gp;
 1453         struct g_part_entry *entry;
 1454         struct g_part_table *table;
 1455         struct root_hold_token *rht;
 1456         int attr, depth;
 1457         int error;
 1458 
 1459         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
 1460         g_topology_assert();
 1461 
 1462         /* Skip providers that are already open for writing. */
 1463         if (pp->acw > 0)
 1464                 return (NULL);
 1465 
 1466         /*
 1467          * Create a GEOM with consumer and hook it up to the provider.
 1468          * With that we become part of the topology. Optain read access
 1469          * to the provider.
 1470          */
 1471         gp = g_new_geomf(mp, "%s", pp->name);
 1472         cp = g_new_consumer(gp);
 1473         error = g_attach(cp, pp);
 1474         if (error == 0)
 1475                 error = g_access(cp, 1, 0, 0);
 1476         if (error != 0) {
 1477                 g_part_wither(gp, error);
 1478                 return (NULL);
 1479         }
 1480 
 1481         rht = root_mount_hold(mp->name);
 1482         g_topology_unlock();
 1483 
 1484         /*
 1485          * Short-circuit the whole probing galore when there's no
 1486          * media present.
 1487          */
 1488         if (pp->mediasize == 0 || pp->sectorsize == 0) {
 1489                 error = ENODEV;
 1490                 goto fail;
 1491         }
 1492 
 1493         /* Make sure we can nest and if so, determine our depth. */
 1494         error = g_getattr("PART::isleaf", cp, &attr);
 1495         if (!error && attr) {
 1496                 error = ENODEV;
 1497                 goto fail;
 1498         }
 1499         error = g_getattr("PART::depth", cp, &attr);
 1500         depth = (!error) ? attr + 1 : 0;
 1501 
 1502         error = g_part_probe(gp, cp, depth);
 1503         if (error)
 1504                 goto fail;
 1505 
 1506         table = gp->softc;
 1507 
 1508         /*
 1509          * Synthesize a disk geometry. Some partitioning schemes
 1510          * depend on it and since some file systems need it even
 1511          * when the partitition scheme doesn't, we do it here in
 1512          * scheme-independent code.
 1513          */
 1514         g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
 1515 
 1516         error = G_PART_READ(table, cp);
 1517         if (error)
 1518                 goto fail;
 1519 
 1520         g_topology_lock();
 1521         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
 1522                 if (!entry->gpe_internal)
 1523                         g_part_new_provider(gp, table, entry);
 1524         }
 1525 
 1526         root_mount_rel(rht);
 1527         g_access(cp, -1, 0, 0);
 1528         return (gp);
 1529 
 1530  fail:
 1531         g_topology_lock();
 1532         root_mount_rel(rht);
 1533         g_access(cp, -1, 0, 0);
 1534         g_part_wither(gp, error);
 1535         return (NULL);
 1536 }
 1537 
 1538 /*
 1539  * Geom methods.
 1540  */
 1541 
 1542 static int
 1543 g_part_access(struct g_provider *pp, int dr, int dw, int de)
 1544 {
 1545         struct g_consumer *cp;
 1546 
 1547         G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
 1548             dw, de));
 1549 
 1550         cp = LIST_FIRST(&pp->geom->consumer);
 1551 
 1552         /* We always gain write-exclusive access. */
 1553         return (g_access(cp, dr, dw, dw + de));
 1554 }
 1555 
 1556 static void
 1557 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
 1558     struct g_consumer *cp, struct g_provider *pp)
 1559 {
 1560         char buf[64];
 1561         struct g_part_entry *entry;
 1562         struct g_part_table *table;
 1563 
 1564         KASSERT(sb != NULL && gp != NULL, (__func__));
 1565         table = gp->softc;
 1566 
 1567         if (indent == NULL) {
 1568                 KASSERT(cp == NULL && pp != NULL, (__func__));
 1569                 entry = pp->private;
 1570                 if (entry == NULL)
 1571                         return;
 1572                 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
 1573                     (uintmax_t)entry->gpe_offset,
 1574                     G_PART_TYPE(table, entry, buf, sizeof(buf)));
 1575                 /*
 1576                  * libdisk compatibility quirk - the scheme dumps the
 1577                  * slicer name and partition type in a way that is
 1578                  * compatible with libdisk. When libdisk is not used
 1579                  * anymore, this should go away.
 1580                  */
 1581                 G_PART_DUMPCONF(table, entry, sb, indent);
 1582         } else if (cp != NULL) {        /* Consumer configuration. */
 1583                 KASSERT(pp == NULL, (__func__));
 1584                 /* none */
 1585         } else if (pp != NULL) {        /* Provider configuration. */
 1586                 entry = pp->private;
 1587                 if (entry == NULL)
 1588                         return;
 1589                 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
 1590                     (uintmax_t)entry->gpe_start);
 1591                 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
 1592                     (uintmax_t)entry->gpe_end);
 1593                 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
 1594                     entry->gpe_index);
 1595                 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
 1596                     G_PART_TYPE(table, entry, buf, sizeof(buf)));
 1597                 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
 1598                     (uintmax_t)entry->gpe_offset);
 1599                 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
 1600                     (uintmax_t)pp->mediasize);
 1601                 G_PART_DUMPCONF(table, entry, sb, indent);
 1602         } else {                        /* Geom configuration. */
 1603                 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
 1604                     table->gpt_scheme->name);
 1605                 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
 1606                     table->gpt_entries);
 1607                 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
 1608                     (uintmax_t)table->gpt_first);
 1609                 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
 1610                     (uintmax_t)table->gpt_last);
 1611                 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
 1612                     table->gpt_sectors);
 1613                 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
 1614                     table->gpt_heads);
 1615                 G_PART_DUMPCONF(table, NULL, sb, indent);
 1616         }
 1617 }
 1618 
 1619 static void
 1620 g_part_orphan(struct g_consumer *cp)
 1621 {
 1622         struct g_provider *pp;
 1623 
 1624         pp = cp->provider;
 1625         KASSERT(pp != NULL, (__func__));
 1626         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
 1627         g_topology_assert();
 1628 
 1629         KASSERT(pp->error != 0, (__func__));
 1630         g_part_wither(cp->geom, pp->error);
 1631 }
 1632 
 1633 static void
 1634 g_part_spoiled(struct g_consumer *cp)
 1635 {
 1636 
 1637         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
 1638         g_topology_assert();
 1639 
 1640         g_part_wither(cp->geom, ENXIO);
 1641 }
 1642 
 1643 static void
 1644 g_part_start(struct bio *bp)
 1645 {
 1646         struct bio *bp2;
 1647         struct g_consumer *cp;
 1648         struct g_geom *gp;
 1649         struct g_part_entry *entry;
 1650         struct g_part_table *table;
 1651         struct g_kerneldump *gkd;
 1652         struct g_provider *pp;
 1653 
 1654         pp = bp->bio_to;
 1655         gp = pp->geom;
 1656         table = gp->softc;
 1657         cp = LIST_FIRST(&gp->consumer);
 1658 
 1659         G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
 1660             pp->name));
 1661 
 1662         entry = pp->private;
 1663         if (entry == NULL) {
 1664                 g_io_deliver(bp, ENXIO);
 1665                 return;
 1666         }
 1667 
 1668         switch(bp->bio_cmd) {
 1669         case BIO_DELETE:
 1670         case BIO_READ:
 1671         case BIO_WRITE:
 1672                 if (bp->bio_offset >= pp->mediasize) {
 1673                         g_io_deliver(bp, EIO);
 1674                         return;
 1675                 }
 1676                 bp2 = g_clone_bio(bp);
 1677                 if (bp2 == NULL) {
 1678                         g_io_deliver(bp, ENOMEM);
 1679                         return;
 1680                 }
 1681                 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
 1682                         bp2->bio_length = pp->mediasize - bp2->bio_offset;
 1683                 bp2->bio_done = g_std_done;
 1684                 bp2->bio_offset += entry->gpe_offset;
 1685                 g_io_request(bp2, cp);
 1686                 return;
 1687         case BIO_FLUSH:
 1688                 break;
 1689         case BIO_GETATTR:
 1690                 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
 1691                         return;
 1692                 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
 1693                         return;
 1694                 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
 1695                         return;
 1696                 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
 1697                         return;
 1698                 if (g_handleattr_str(bp, "PART::scheme",
 1699                     table->gpt_scheme->name))
 1700                         return;
 1701                 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
 1702                         /*
 1703                          * Check that the partition is suitable for kernel
 1704                          * dumps. Typically only swap partitions should be
 1705                          * used.
 1706                          */
 1707                         if (!G_PART_DUMPTO(table, entry)) {
 1708                                 g_io_deliver(bp, ENODEV);
 1709                                 printf("GEOM_PART: Partition '%s' not suitable"
 1710                                     " for kernel dumps (wrong type?)\n",
 1711                                     pp->name);
 1712                                 return;
 1713                         }
 1714                         gkd = (struct g_kerneldump *)bp->bio_data;
 1715                         if (gkd->offset >= pp->mediasize) {
 1716                                 g_io_deliver(bp, EIO);
 1717                                 return;
 1718                         }
 1719                         if (gkd->offset + gkd->length > pp->mediasize)
 1720                                 gkd->length = pp->mediasize - gkd->offset;
 1721                         gkd->offset += entry->gpe_offset;
 1722                 }
 1723                 break;
 1724         default:
 1725                 g_io_deliver(bp, EOPNOTSUPP);
 1726                 return;
 1727         }
 1728 
 1729         bp2 = g_clone_bio(bp);
 1730         if (bp2 == NULL) {
 1731                 g_io_deliver(bp, ENOMEM);
 1732                 return;
 1733         }
 1734         bp2->bio_done = g_std_done;
 1735         g_io_request(bp2, cp);
 1736 }
 1737 
 1738 static void
 1739 g_part_init(struct g_class *mp)
 1740 {
 1741 
 1742         TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
 1743 }
 1744 
 1745 static void
 1746 g_part_fini(struct g_class *mp)
 1747 {
 1748 
 1749         TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
 1750 }
 1751 
 1752 static void
 1753 g_part_unload_event(void *arg, int flag)
 1754 {
 1755         struct g_consumer *cp;
 1756         struct g_geom *gp;
 1757         struct g_provider *pp;
 1758         struct g_part_scheme *scheme;
 1759         struct g_part_table *table;
 1760         uintptr_t *xchg;
 1761         int acc, error;
 1762 
 1763         if (flag == EV_CANCEL)
 1764                 return;
 1765 
 1766         xchg = arg;
 1767         error = 0;
 1768         scheme = (void *)(*xchg);
 1769 
 1770         g_topology_assert();
 1771 
 1772         LIST_FOREACH(gp, &g_part_class.geom, geom) {
 1773                 table = gp->softc;
 1774                 if (table->gpt_scheme != scheme)
 1775                         continue;
 1776 
 1777                 acc = 0;
 1778                 LIST_FOREACH(pp, &gp->provider, provider)
 1779                         acc += pp->acr + pp->acw + pp->ace;
 1780                 LIST_FOREACH(cp, &gp->consumer, consumer)
 1781                         acc += cp->acr + cp->acw + cp->ace;
 1782 
 1783                 if (!acc)
 1784                         g_part_wither(gp, ENOSYS);
 1785                 else
 1786                         error = EBUSY;
 1787         }
 1788 
 1789         if (!error)
 1790                 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
 1791 
 1792         *xchg = error;
 1793 }
 1794 
 1795 int
 1796 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
 1797 {
 1798         uintptr_t arg;
 1799         int error;
 1800 
 1801         switch (type) {
 1802         case MOD_LOAD:
 1803                 TAILQ_INSERT_TAIL(&g_part_schemes, scheme, scheme_list);
 1804 
 1805                 error = g_retaste(&g_part_class);
 1806                 if (error)
 1807                         TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
 1808                 break;
 1809         case MOD_UNLOAD:
 1810                 arg = (uintptr_t)scheme;
 1811                 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
 1812                     NULL);
 1813                 if (!error)
 1814                         error = (arg == (uintptr_t)scheme) ? EDOOFUS : arg;
 1815                 break;
 1816         default:
 1817                 error = EOPNOTSUPP;
 1818                 break;
 1819         }
 1820 
 1821         return (error);
 1822 }

Cache object: 13f3985d1c20265bebd7dbabe9f96386


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