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