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