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