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