]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/geom/part/g_part.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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_geom *gp;
849         struct g_part_entry *entry;
850         struct g_part_table *null, *table;
851         struct sbuf *sb;
852         int error;
853
854         gp = gpp->gpp_geom;
855         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
856         g_topology_assert();
857
858         table = gp->softc;
859         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
860                 if (entry->gpe_deleted || entry->gpe_internal)
861                         continue;
862                 gctl_error(req, "%d", EBUSY);
863                 return (EBUSY);
864         }
865
866         error = G_PART_DESTROY(table, gpp);
867         if (error) {
868                 gctl_error(req, "%d", error);
869                 return (error);
870         }
871
872         gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
873             M_WAITOK);
874         null = gp->softc;
875         null->gpt_gp = gp;
876         null->gpt_scheme = &g_part_null_scheme;
877         LIST_INIT(&null->gpt_entry);
878         null->gpt_depth = table->gpt_depth;
879         null->gpt_opened = table->gpt_opened;
880         null->gpt_smhead = table->gpt_smhead;
881         null->gpt_smtail = table->gpt_smtail;
882
883         while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
884                 LIST_REMOVE(entry, gpe_entry);
885                 g_free(entry);
886         }
887         kobj_delete((kobj_t)table, M_GEOM);
888
889         /* Provide feedback if so requested. */
890         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
891                 sb = sbuf_new_auto();
892                 sbuf_printf(sb, "%s destroyed\n", gp->name);
893                 sbuf_finish(sb);
894                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
895                 sbuf_delete(sb);
896         }
897         return (0);
898 }
899
900 static int
901 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
902 {
903         struct g_geom *gp;
904         struct g_part_entry *entry;
905         struct g_part_table *table;
906         struct sbuf *sb;
907         int error;
908
909         gp = gpp->gpp_geom;
910         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
911         g_topology_assert();
912
913         table = gp->softc;
914
915         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
916                 if (entry->gpe_deleted || entry->gpe_internal)
917                         continue;
918                 if (entry->gpe_index == gpp->gpp_index)
919                         break;
920         }
921         if (entry == NULL) {
922                 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
923                 return (ENOENT);
924         }
925
926         error = G_PART_MODIFY(table, entry, gpp);
927         if (error) {
928                 gctl_error(req, "%d", error);
929                 return (error);
930         }
931
932         if (!entry->gpe_created)
933                 entry->gpe_modified = 1;
934
935         /* Provide feedback if so requested. */
936         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
937                 sb = sbuf_new_auto();
938                 G_PART_FULLNAME(table, entry, sb, gp->name);
939                 sbuf_cat(sb, " modified\n");
940                 sbuf_finish(sb);
941                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
942                 sbuf_delete(sb);
943         }
944         return (0);
945 }
946
947 static int
948 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
949 {
950         gctl_error(req, "%d verb 'move'", ENOSYS);
951         return (ENOSYS);
952
953
954 static int
955 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
956 {
957         gctl_error(req, "%d verb 'recover'", ENOSYS);
958         return (ENOSYS);
959 }
960
961 static int
962 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
963 {
964         gctl_error(req, "%d verb 'resize'", ENOSYS);
965         return (ENOSYS);
966
967
968 static int
969 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
970     unsigned int set)
971 {
972         struct g_geom *gp;
973         struct g_part_entry *entry;
974         struct g_part_table *table;
975         struct sbuf *sb;
976         int error;
977
978         gp = gpp->gpp_geom;
979         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
980         g_topology_assert();
981
982         table = gp->softc;
983
984         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
985                 if (entry->gpe_deleted || entry->gpe_internal)
986                         continue;
987                 if (entry->gpe_index == gpp->gpp_index)
988                         break;
989         }
990         if (entry == NULL) {
991                 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
992                 return (ENOENT);
993         }
994
995         error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
996         if (error) {
997                 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
998                 return (error);
999         }
1000
1001         /* Provide feedback if so requested. */
1002         if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1003                 sb = sbuf_new_auto();
1004                 G_PART_FULLNAME(table, entry, sb, gp->name);
1005                 sbuf_printf(sb, " has %s %sset\n", gpp->gpp_attrib,
1006                     (set) ? "" : "un");
1007                 sbuf_finish(sb);
1008                 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1009                 sbuf_delete(sb);
1010         }
1011         return (0);
1012 }
1013
1014 static int
1015 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1016 {
1017         struct g_consumer *cp;
1018         struct g_provider *pp;
1019         struct g_geom *gp;
1020         struct g_part_entry *entry, *tmp;
1021         struct g_part_table *table;
1022         int error, reprobe;
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         if (!table->gpt_opened) {
1030                 gctl_error(req, "%d", EPERM);
1031                 return (EPERM);
1032         }
1033
1034         cp = LIST_FIRST(&gp->consumer);
1035         LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1036                 entry->gpe_modified = 0;
1037                 if (entry->gpe_created) {
1038                         pp = entry->gpe_pp;
1039                         if (pp != NULL) {
1040                                 pp->private = NULL;
1041                                 entry->gpe_pp = NULL;
1042                                 g_wither_provider(pp, ENXIO);
1043                         }
1044                         entry->gpe_deleted = 1;
1045                 }
1046                 if (entry->gpe_deleted) {
1047                         LIST_REMOVE(entry, gpe_entry);
1048                         g_free(entry);
1049                 }
1050         }
1051
1052         g_topology_unlock();
1053
1054         reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1055             table->gpt_created) ? 1 : 0;
1056
1057         if (reprobe) {
1058                 if (!LIST_EMPTY(&table->gpt_entry)) {
1059                         error = EBUSY;
1060                         goto fail;
1061                 }
1062                 error = g_part_probe(gp, cp, table->gpt_depth);
1063                 if (error) {
1064                         g_topology_lock();
1065                         g_access(cp, -1, -1, -1);
1066                         g_part_wither(gp, error);
1067                         return (0);
1068                 }
1069                 table = gp->softc;
1070
1071                 /*
1072                  * Synthesize a disk geometry. Some partitioning schemes
1073                  * depend on it and since some file systems need it even
1074                  * when the partitition scheme doesn't, we do it here in
1075                  * scheme-independent code.
1076                  */
1077                 pp = cp->provider;
1078                 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1079         }
1080
1081         error = G_PART_READ(table, cp);
1082         if (error)
1083                 goto fail;
1084
1085         g_topology_lock();
1086
1087         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1088                 if (!entry->gpe_internal)
1089                         g_part_new_provider(gp, table, entry);
1090         }
1091
1092         table->gpt_opened = 0;
1093         g_access(cp, -1, -1, -1);
1094         return (0);
1095
1096 fail:
1097         g_topology_lock();
1098         gctl_error(req, "%d", error);
1099         return (error);
1100 }
1101
1102 static void
1103 g_part_wither(struct g_geom *gp, int error)
1104 {
1105         struct g_part_entry *entry;
1106         struct g_part_table *table;
1107
1108         table = gp->softc;
1109         if (table != NULL) {
1110                 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1111                         LIST_REMOVE(entry, gpe_entry);
1112                         g_free(entry);
1113                 }
1114                 if (gp->softc != NULL) {
1115                         kobj_delete((kobj_t)gp->softc, M_GEOM);
1116                         gp->softc = NULL;
1117                 }
1118         }
1119         g_wither_geom(gp, error);
1120 }
1121
1122 /*
1123  * Class methods.
1124  */
1125
1126 static void
1127 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1128 {
1129         struct g_part_parms gpp;
1130         struct g_part_table *table;
1131         struct gctl_req_arg *ap;
1132         const char *p;
1133         enum g_part_ctl ctlreq;
1134         unsigned int i, mparms, oparms, parm;
1135         int auto_commit, close_on_error;
1136         int error, len, modifies;
1137
1138         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1139         g_topology_assert();
1140
1141         ctlreq = G_PART_CTL_NONE;
1142         modifies = 1;
1143         mparms = 0;
1144         oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1145         switch (*verb) {
1146         case 'a':
1147                 if (!strcmp(verb, "add")) {
1148                         ctlreq = G_PART_CTL_ADD;
1149                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1150                             G_PART_PARM_START | G_PART_PARM_TYPE;
1151                         oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1152                 }
1153                 break;
1154         case 'b':
1155                 if (!strcmp(verb, "bootcode")) {
1156                         ctlreq = G_PART_CTL_BOOTCODE;
1157                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1158                 }
1159                 break;
1160         case 'c':
1161                 if (!strcmp(verb, "commit")) {
1162                         ctlreq = G_PART_CTL_COMMIT;
1163                         mparms |= G_PART_PARM_GEOM;
1164                         modifies = 0;
1165                 } else if (!strcmp(verb, "create")) {
1166                         ctlreq = G_PART_CTL_CREATE;
1167                         mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1168                         oparms |= G_PART_PARM_ENTRIES;
1169                 }
1170                 break;
1171         case 'd':
1172                 if (!strcmp(verb, "delete")) {
1173                         ctlreq = G_PART_CTL_DELETE;
1174                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1175                 } else if (!strcmp(verb, "destroy")) {
1176                         ctlreq = G_PART_CTL_DESTROY;
1177                         mparms |= G_PART_PARM_GEOM;
1178                 }
1179                 break;
1180         case 'm':
1181                 if (!strcmp(verb, "modify")) {
1182                         ctlreq = G_PART_CTL_MODIFY;
1183                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1184                         oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1185                 } else if (!strcmp(verb, "move")) {
1186                         ctlreq = G_PART_CTL_MOVE;
1187                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1188                 }
1189                 break;
1190         case 'r':
1191                 if (!strcmp(verb, "recover")) {
1192                         ctlreq = G_PART_CTL_RECOVER;
1193                         mparms |= G_PART_PARM_GEOM;
1194                 } else if (!strcmp(verb, "resize")) {
1195                         ctlreq = G_PART_CTL_RESIZE;
1196                         mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1197                 }
1198                 break;
1199         case 's':
1200                 if (!strcmp(verb, "set")) {
1201                         ctlreq = G_PART_CTL_SET;
1202                         mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1203                             G_PART_PARM_INDEX;
1204                 }
1205                 break;
1206         case 'u':
1207                 if (!strcmp(verb, "undo")) {
1208                         ctlreq = G_PART_CTL_UNDO;
1209                         mparms |= G_PART_PARM_GEOM;
1210                         modifies = 0;
1211                 } else if (!strcmp(verb, "unset")) {
1212                         ctlreq = G_PART_CTL_UNSET;
1213                         mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1214                             G_PART_PARM_INDEX;
1215                 }
1216                 break;
1217         }
1218         if (ctlreq == G_PART_CTL_NONE) {
1219                 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1220                 return;
1221         }
1222
1223         bzero(&gpp, sizeof(gpp));
1224         for (i = 0; i < req->narg; i++) {
1225                 ap = &req->arg[i];
1226                 parm = 0;
1227                 switch (ap->name[0]) {
1228                 case 'a':
1229                         if (!strcmp(ap->name, "attrib"))
1230                                 parm = G_PART_PARM_ATTRIB;
1231                         break;
1232                 case 'b':
1233                         if (!strcmp(ap->name, "bootcode"))
1234                                 parm = G_PART_PARM_BOOTCODE;
1235                         break;
1236                 case 'c':
1237                         if (!strcmp(ap->name, "class"))
1238                                 continue;
1239                         break;
1240                 case 'e':
1241                         if (!strcmp(ap->name, "entries"))
1242                                 parm = G_PART_PARM_ENTRIES;
1243                         break;
1244                 case 'f':
1245                         if (!strcmp(ap->name, "flags"))
1246                                 parm = G_PART_PARM_FLAGS;
1247                         break;
1248                 case 'g':
1249                         if (!strcmp(ap->name, "geom"))
1250                                 parm = G_PART_PARM_GEOM;
1251                         break;
1252                 case 'i':
1253                         if (!strcmp(ap->name, "index"))
1254                                 parm = G_PART_PARM_INDEX;
1255                         break;
1256                 case 'l':
1257                         if (!strcmp(ap->name, "label"))
1258                                 parm = G_PART_PARM_LABEL;
1259                         break;
1260                 case 'o':
1261                         if (!strcmp(ap->name, "output"))
1262                                 parm = G_PART_PARM_OUTPUT;
1263                         break;
1264                 case 'p':
1265                         if (!strcmp(ap->name, "provider"))
1266                                 parm = G_PART_PARM_PROVIDER;
1267                         break;
1268                 case 's':
1269                         if (!strcmp(ap->name, "scheme"))
1270                                 parm = G_PART_PARM_SCHEME;
1271                         else if (!strcmp(ap->name, "size"))
1272                                 parm = G_PART_PARM_SIZE;
1273                         else if (!strcmp(ap->name, "start"))
1274                                 parm = G_PART_PARM_START;
1275                         break;
1276                 case 't':
1277                         if (!strcmp(ap->name, "type"))
1278                                 parm = G_PART_PARM_TYPE;
1279                         break;
1280                 case 'v':
1281                         if (!strcmp(ap->name, "verb"))
1282                                 continue;
1283                         else if (!strcmp(ap->name, "version"))
1284                                 parm = G_PART_PARM_VERSION;
1285                         break;
1286                 }
1287                 if ((parm & (mparms | oparms)) == 0) {
1288                         gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1289                         return;
1290                 }
1291                 if (parm == G_PART_PARM_BOOTCODE)
1292                         p = gctl_get_param(req, ap->name, &len);
1293                 else
1294                         p = gctl_get_asciiparam(req, ap->name);
1295                 if (p == NULL) {
1296                         gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
1297                         return;
1298                 }
1299                 switch (parm) {
1300                 case G_PART_PARM_ATTRIB:
1301                         error = g_part_parm_str(p, &gpp.gpp_attrib);
1302                         break;
1303                 case G_PART_PARM_BOOTCODE:
1304                         gpp.gpp_codeptr = p;
1305                         gpp.gpp_codesize = len;
1306                         error = 0;
1307                         break;
1308                 case G_PART_PARM_ENTRIES:
1309                         error = g_part_parm_uint(p, &gpp.gpp_entries);
1310                         break;
1311                 case G_PART_PARM_FLAGS:
1312                         if (p[0] == '\0')
1313                                 continue;
1314                         error = g_part_parm_str(p, &gpp.gpp_flags);
1315                         break;
1316                 case G_PART_PARM_GEOM:
1317                         error = g_part_parm_geom(p, &gpp.gpp_geom);
1318                         break;
1319                 case G_PART_PARM_INDEX:
1320                         error = g_part_parm_uint(p, &gpp.gpp_index);
1321                         break;
1322                 case G_PART_PARM_LABEL:
1323                         /* An empty label is always valid. */
1324                         gpp.gpp_label = p;
1325                         error = 0;
1326                         break;
1327                 case G_PART_PARM_OUTPUT:
1328                         error = 0;      /* Write-only parameter */
1329                         break;
1330                 case G_PART_PARM_PROVIDER:
1331                         error = g_part_parm_provider(p, &gpp.gpp_provider);
1332                         break;
1333                 case G_PART_PARM_SCHEME:
1334                         error = g_part_parm_scheme(p, &gpp.gpp_scheme);
1335                         break;
1336                 case G_PART_PARM_SIZE:
1337                         error = g_part_parm_quad(p, &gpp.gpp_size);
1338                         break;
1339                 case G_PART_PARM_START:
1340                         error = g_part_parm_quad(p, &gpp.gpp_start);
1341                         break;
1342                 case G_PART_PARM_TYPE:
1343                         error = g_part_parm_str(p, &gpp.gpp_type);
1344                         break;
1345                 case G_PART_PARM_VERSION:
1346                         error = g_part_parm_uint(p, &gpp.gpp_version);
1347                         break;
1348                 default:
1349                         error = EDOOFUS;
1350                         break;
1351                 }
1352                 if (error) {
1353                         gctl_error(req, "%d %s '%s'", error, ap->name, p);
1354                         return;
1355                 }
1356                 gpp.gpp_parms |= parm;
1357         }
1358         if ((gpp.gpp_parms & mparms) != mparms) {
1359                 parm = mparms - (gpp.gpp_parms & mparms);
1360                 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1361                 return;
1362         }
1363
1364         /* Obtain permissions if possible/necessary. */
1365         close_on_error = 0;
1366         table = NULL;
1367         if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1368                 table = gpp.gpp_geom->softc;
1369                 if (table != NULL && !table->gpt_opened) {
1370                         error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1371                             1, 1, 1);
1372                         if (error) {
1373                                 gctl_error(req, "%d geom '%s'", error,
1374                                     gpp.gpp_geom->name);
1375                                 return;
1376                         }
1377                         table->gpt_opened = 1;
1378                         close_on_error = 1;
1379                 }
1380         }
1381
1382         /* Allow the scheme to check or modify the parameters. */
1383         if (table != NULL) {
1384                 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1385                 if (error) {
1386                         gctl_error(req, "%d pre-check failed", error);
1387                         goto out;
1388                 }
1389         } else
1390                 error = EDOOFUS;        /* Prevent bogus uninit. warning. */
1391
1392         switch (ctlreq) {
1393         case G_PART_CTL_NONE:
1394                 panic("%s", __func__);
1395         case G_PART_CTL_ADD:
1396                 error = g_part_ctl_add(req, &gpp);
1397                 break;
1398         case G_PART_CTL_BOOTCODE:
1399                 error = g_part_ctl_bootcode(req, &gpp);
1400                 break;
1401         case G_PART_CTL_COMMIT:
1402                 error = g_part_ctl_commit(req, &gpp);
1403                 break;
1404         case G_PART_CTL_CREATE:
1405                 error = g_part_ctl_create(req, &gpp);
1406                 break;
1407         case G_PART_CTL_DELETE:
1408                 error = g_part_ctl_delete(req, &gpp);
1409                 break;
1410         case G_PART_CTL_DESTROY:
1411                 error = g_part_ctl_destroy(req, &gpp);
1412                 break;
1413         case G_PART_CTL_MODIFY:
1414                 error = g_part_ctl_modify(req, &gpp);
1415                 break;
1416         case G_PART_CTL_MOVE:
1417                 error = g_part_ctl_move(req, &gpp);
1418                 break;
1419         case G_PART_CTL_RECOVER:
1420                 error = g_part_ctl_recover(req, &gpp);
1421                 break;
1422         case G_PART_CTL_RESIZE:
1423                 error = g_part_ctl_resize(req, &gpp);
1424                 break;
1425         case G_PART_CTL_SET:
1426                 error = g_part_ctl_setunset(req, &gpp, 1);
1427                 break;
1428         case G_PART_CTL_UNDO:
1429                 error = g_part_ctl_undo(req, &gpp);
1430                 break;
1431         case G_PART_CTL_UNSET:
1432                 error = g_part_ctl_setunset(req, &gpp, 0);
1433                 break;
1434         }
1435
1436         /* Implement automatic commit. */
1437         if (!error) {
1438                 auto_commit = (modifies &&
1439                     (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1440                     strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1441                 if (auto_commit) {
1442                         KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
1443                         error = g_part_ctl_commit(req, &gpp);
1444                 }
1445         }
1446
1447  out:
1448         if (error && close_on_error) {
1449                 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1450                 table->gpt_opened = 0;
1451         }
1452 }
1453
1454 static int
1455 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1456     struct g_geom *gp)
1457 {
1458
1459         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1460         g_topology_assert();
1461
1462         g_part_wither(gp, EINVAL);
1463         return (0);
1464 }
1465
1466 static struct g_geom *
1467 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1468 {
1469         struct g_consumer *cp;
1470         struct g_geom *gp;
1471         struct g_part_entry *entry;
1472         struct g_part_table *table;
1473         struct root_hold_token *rht;
1474         int attr, depth;
1475         int error;
1476
1477         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1478         g_topology_assert();
1479
1480         /* Skip providers that are already open for writing. */
1481         if (pp->acw > 0)
1482                 return (NULL);
1483
1484         /*
1485          * Create a GEOM with consumer and hook it up to the provider.
1486          * With that we become part of the topology. Optain read access
1487          * to the provider.
1488          */
1489         gp = g_new_geomf(mp, "%s", pp->name);
1490         cp = g_new_consumer(gp);
1491         error = g_attach(cp, pp);
1492         if (error == 0)
1493                 error = g_access(cp, 1, 0, 0);
1494         if (error != 0) {
1495                 g_part_wither(gp, error);
1496                 return (NULL);
1497         }
1498
1499         rht = root_mount_hold(mp->name);
1500         g_topology_unlock();
1501
1502         /*
1503          * Short-circuit the whole probing galore when there's no
1504          * media present.
1505          */
1506         if (pp->mediasize == 0 || pp->sectorsize == 0) {
1507                 error = ENODEV;
1508                 goto fail;
1509         }
1510
1511         /* Make sure we can nest and if so, determine our depth. */
1512         error = g_getattr("PART::isleaf", cp, &attr);
1513         if (!error && attr) {
1514                 error = ENODEV;
1515                 goto fail;
1516         }
1517         error = g_getattr("PART::depth", cp, &attr);
1518         depth = (!error) ? attr + 1 : 0;
1519
1520         error = g_part_probe(gp, cp, depth);
1521         if (error)
1522                 goto fail;
1523
1524         table = gp->softc;
1525
1526         /*
1527          * Synthesize a disk geometry. Some partitioning schemes
1528          * depend on it and since some file systems need it even
1529          * when the partitition scheme doesn't, we do it here in
1530          * scheme-independent code.
1531          */
1532         g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1533
1534         error = G_PART_READ(table, cp);
1535         if (error)
1536                 goto fail;
1537
1538         g_topology_lock();
1539         LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1540                 if (!entry->gpe_internal)
1541                         g_part_new_provider(gp, table, entry);
1542         }
1543
1544         root_mount_rel(rht);
1545         g_access(cp, -1, 0, 0);
1546         return (gp);
1547
1548  fail:
1549         g_topology_lock();
1550         root_mount_rel(rht);
1551         g_access(cp, -1, 0, 0);
1552         g_part_wither(gp, error);
1553         return (NULL);
1554 }
1555
1556 /*
1557  * Geom methods.
1558  */
1559
1560 static int
1561 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1562 {
1563         struct g_consumer *cp;
1564
1565         G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1566             dw, de));
1567
1568         cp = LIST_FIRST(&pp->geom->consumer);
1569
1570         /* We always gain write-exclusive access. */
1571         return (g_access(cp, dr, dw, dw + de));
1572 }
1573
1574 static void
1575 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1576     struct g_consumer *cp, struct g_provider *pp)
1577 {
1578         char buf[64];
1579         struct g_part_entry *entry;
1580         struct g_part_table *table;
1581
1582         KASSERT(sb != NULL && gp != NULL, (__func__));
1583         table = gp->softc;
1584
1585         if (indent == NULL) {
1586                 KASSERT(cp == NULL && pp != NULL, (__func__));
1587                 entry = pp->private;
1588                 if (entry == NULL)
1589                         return;
1590                 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1591                     (uintmax_t)entry->gpe_offset,
1592                     G_PART_TYPE(table, entry, buf, sizeof(buf)));
1593                 /*
1594                  * libdisk compatibility quirk - the scheme dumps the
1595                  * slicer name and partition type in a way that is
1596                  * compatible with libdisk. When libdisk is not used
1597                  * anymore, this should go away.
1598                  */
1599                 G_PART_DUMPCONF(table, entry, sb, indent);
1600         } else if (cp != NULL) {        /* Consumer configuration. */
1601                 KASSERT(pp == NULL, (__func__));
1602                 /* none */
1603         } else if (pp != NULL) {        /* Provider configuration. */
1604                 entry = pp->private;
1605                 if (entry == NULL)
1606                         return;
1607                 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
1608                     (uintmax_t)entry->gpe_start);
1609                 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
1610                     (uintmax_t)entry->gpe_end);
1611                 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1612                     entry->gpe_index);
1613                 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1614                     G_PART_TYPE(table, entry, buf, sizeof(buf)));
1615                 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1616                     (uintmax_t)entry->gpe_offset);
1617                 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1618                     (uintmax_t)pp->mediasize);
1619                 G_PART_DUMPCONF(table, entry, sb, indent);
1620         } else {                        /* Geom configuration. */
1621                 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1622                     table->gpt_scheme->name);
1623                 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
1624                     table->gpt_entries);
1625                 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
1626                     (uintmax_t)table->gpt_first);
1627                 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
1628                     (uintmax_t)table->gpt_last);
1629                 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
1630                     table->gpt_sectors);
1631                 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
1632                     table->gpt_heads);
1633                 G_PART_DUMPCONF(table, NULL, sb, indent);
1634         }
1635 }
1636
1637 static void
1638 g_part_orphan(struct g_consumer *cp)
1639 {
1640         struct g_provider *pp;
1641
1642         pp = cp->provider;
1643         KASSERT(pp != NULL, (__func__));
1644         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
1645         g_topology_assert();
1646
1647         KASSERT(pp->error != 0, (__func__));
1648         g_part_wither(cp->geom, pp->error);
1649 }
1650
1651 static void
1652 g_part_spoiled(struct g_consumer *cp)
1653 {
1654
1655         G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
1656         g_topology_assert();
1657
1658         g_part_wither(cp->geom, ENXIO);
1659 }
1660
1661 static void
1662 g_part_start(struct bio *bp)
1663 {
1664         struct bio *bp2;
1665         struct g_consumer *cp;
1666         struct g_geom *gp;
1667         struct g_part_entry *entry;
1668         struct g_part_table *table;
1669         struct g_kerneldump *gkd;
1670         struct g_provider *pp;
1671
1672         pp = bp->bio_to;
1673         gp = pp->geom;
1674         table = gp->softc;
1675         cp = LIST_FIRST(&gp->consumer);
1676
1677         G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
1678             pp->name));
1679
1680         entry = pp->private;
1681         if (entry == NULL) {
1682                 g_io_deliver(bp, ENXIO);
1683                 return;
1684         }
1685
1686         switch(bp->bio_cmd) {
1687         case BIO_DELETE:
1688         case BIO_READ:
1689         case BIO_WRITE:
1690                 if (bp->bio_offset >= pp->mediasize) {
1691                         g_io_deliver(bp, EIO);
1692                         return;
1693                 }
1694                 bp2 = g_clone_bio(bp);
1695                 if (bp2 == NULL) {
1696                         g_io_deliver(bp, ENOMEM);
1697                         return;
1698                 }
1699                 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
1700                         bp2->bio_length = pp->mediasize - bp2->bio_offset;
1701                 bp2->bio_done = g_std_done;
1702                 bp2->bio_offset += entry->gpe_offset;
1703                 g_io_request(bp2, cp);
1704                 return;
1705         case BIO_FLUSH:
1706                 break;
1707         case BIO_GETATTR:
1708                 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
1709                         return;
1710                 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
1711                         return;
1712                 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
1713                         return;
1714                 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
1715                         return;
1716                 if (g_handleattr_str(bp, "PART::scheme",
1717                     table->gpt_scheme->name))
1718                         return;
1719                 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
1720                         /*
1721                          * Check that the partition is suitable for kernel
1722                          * dumps. Typically only swap partitions should be
1723                          * used.
1724                          */
1725                         if (!G_PART_DUMPTO(table, entry)) {
1726                                 g_io_deliver(bp, ENODEV);
1727                                 printf("GEOM_PART: Partition '%s' not suitable"
1728                                     " for kernel dumps (wrong type?)\n",
1729                                     pp->name);
1730                                 return;
1731                         }
1732                         gkd = (struct g_kerneldump *)bp->bio_data;
1733                         if (gkd->offset >= pp->mediasize) {
1734                                 g_io_deliver(bp, EIO);
1735                                 return;
1736                         }
1737                         if (gkd->offset + gkd->length > pp->mediasize)
1738                                 gkd->length = pp->mediasize - gkd->offset;
1739                         gkd->offset += entry->gpe_offset;
1740                 }
1741                 break;
1742         default:
1743                 g_io_deliver(bp, EOPNOTSUPP);
1744                 return;
1745         }
1746
1747         bp2 = g_clone_bio(bp);
1748         if (bp2 == NULL) {
1749                 g_io_deliver(bp, ENOMEM);
1750                 return;
1751         }
1752         bp2->bio_done = g_std_done;
1753         g_io_request(bp2, cp);
1754 }
1755
1756 static void
1757 g_part_init(struct g_class *mp)
1758 {
1759
1760         TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
1761 }
1762
1763 static void
1764 g_part_fini(struct g_class *mp)
1765 {
1766
1767         TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
1768 }
1769
1770 static void
1771 g_part_unload_event(void *arg, int flag)
1772 {
1773         struct g_consumer *cp;
1774         struct g_geom *gp;
1775         struct g_provider *pp;
1776         struct g_part_scheme *scheme;
1777         struct g_part_table *table;
1778         uintptr_t *xchg;
1779         int acc, error;
1780
1781         if (flag == EV_CANCEL)
1782                 return;
1783
1784         xchg = arg;
1785         error = 0;
1786         scheme = (void *)(*xchg);
1787
1788         g_topology_assert();
1789
1790         LIST_FOREACH(gp, &g_part_class.geom, geom) {
1791                 table = gp->softc;
1792                 if (table->gpt_scheme != scheme)
1793                         continue;
1794
1795                 acc = 0;
1796                 LIST_FOREACH(pp, &gp->provider, provider)
1797                         acc += pp->acr + pp->acw + pp->ace;
1798                 LIST_FOREACH(cp, &gp->consumer, consumer)
1799                         acc += cp->acr + cp->acw + cp->ace;
1800
1801                 if (!acc)
1802                         g_part_wither(gp, ENOSYS);
1803                 else
1804                         error = EBUSY;
1805         }
1806
1807         if (!error)
1808                 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1809
1810         *xchg = error;
1811 }
1812
1813 int
1814 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
1815 {
1816         uintptr_t arg;
1817         int error;
1818
1819         switch (type) {
1820         case MOD_LOAD:
1821                 TAILQ_INSERT_TAIL(&g_part_schemes, scheme, scheme_list);
1822
1823                 error = g_retaste(&g_part_class);
1824                 if (error)
1825                         TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1826                 break;
1827         case MOD_UNLOAD:
1828                 arg = (uintptr_t)scheme;
1829                 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
1830                     NULL);
1831                 if (!error)
1832                         error = (arg == (uintptr_t)scheme) ? EDOOFUS : arg;
1833                 break;
1834         default:
1835                 error = EOPNOTSUPP;
1836                 break;
1837         }
1838
1839         return (error);
1840 }