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