]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/geom/part/g_part_bsd.c
MFC r207094 (by marcel):
[FreeBSD/stable/8.git] / sys / geom / part / g_part_bsd.c
1 /*-
2  * Copyright (c) 2007 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/disklabel.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 <geom/geom.h>
44 #include <geom/part/g_part.h>
45
46 #include "g_part_if.h"
47
48 struct g_part_bsd_table {
49         struct g_part_table     base;
50         u_char                  *bbarea;
51         uint32_t                offset;
52 };
53
54 struct g_part_bsd_entry {
55         struct g_part_entry     base;
56         struct partition        part;
57 };
58
59 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
60     struct g_part_parms *);
61 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
62 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
63 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
64 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
65     struct sbuf *, const char *);
66 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
67 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,  
68     struct g_part_parms *);
69 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
70     char *, size_t);
71 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
72 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
73 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
74     char *, size_t);
75 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
76 static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
77     struct g_part_parms *);
78
79 static kobj_method_t g_part_bsd_methods[] = {
80         KOBJMETHOD(g_part_add,          g_part_bsd_add),
81         KOBJMETHOD(g_part_bootcode,     g_part_bsd_bootcode),
82         KOBJMETHOD(g_part_create,       g_part_bsd_create),
83         KOBJMETHOD(g_part_destroy,      g_part_bsd_destroy),
84         KOBJMETHOD(g_part_dumpconf,     g_part_bsd_dumpconf),
85         KOBJMETHOD(g_part_dumpto,       g_part_bsd_dumpto),
86         KOBJMETHOD(g_part_modify,       g_part_bsd_modify),
87         KOBJMETHOD(g_part_resize,       g_part_bsd_resize),
88         KOBJMETHOD(g_part_name,         g_part_bsd_name),
89         KOBJMETHOD(g_part_probe,        g_part_bsd_probe),
90         KOBJMETHOD(g_part_read,         g_part_bsd_read),
91         KOBJMETHOD(g_part_type,         g_part_bsd_type),
92         KOBJMETHOD(g_part_write,        g_part_bsd_write),
93         { 0, 0 }
94 };
95
96 static struct g_part_scheme g_part_bsd_scheme = {
97         "BSD",
98         g_part_bsd_methods,
99         sizeof(struct g_part_bsd_table),
100         .gps_entrysz = sizeof(struct g_part_bsd_entry),
101         .gps_minent = 8,
102         .gps_maxent = 20,
103         .gps_bootcodesz = BBSIZE,
104 };
105 G_PART_SCHEME_DECLARE(g_part_bsd);
106
107 static int
108 bsd_parse_type(const char *type, uint8_t *fstype)
109 {
110         const char *alias;
111         char *endp;
112         long lt;
113
114         if (type[0] == '!') {
115                 lt = strtol(type + 1, &endp, 0);
116                 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
117                         return (EINVAL);
118                 *fstype = (u_int)lt;
119                 return (0);
120         }
121         alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
122         if (!strcasecmp(type, alias)) {
123                 *fstype = FS_SWAP;
124                 return (0);
125         }
126         alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
127         if (!strcasecmp(type, alias)) {
128                 *fstype = FS_BSDFFS;
129                 return (0);
130         }
131         alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
132         if (!strcasecmp(type, alias)) {
133                 *fstype = FS_VINUM;
134                 return (0);
135         }
136         alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
137         if (!strcasecmp(type, alias)) {
138                 *fstype = FS_ZFS;
139                 return (0);
140         }
141         return (EINVAL);
142 }
143
144 static int
145 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
146     struct g_part_parms *gpp)
147 {
148         struct g_part_bsd_entry *entry;
149         struct g_part_bsd_table *table;
150
151         if (gpp->gpp_parms & G_PART_PARM_LABEL)
152                 return (EINVAL);
153
154         entry = (struct g_part_bsd_entry *)baseentry;
155         table = (struct g_part_bsd_table *)basetable;
156
157         entry->part.p_size = gpp->gpp_size;
158         entry->part.p_offset = gpp->gpp_start + table->offset;
159         entry->part.p_fsize = 0;
160         entry->part.p_frag = 0;
161         entry->part.p_cpg = 0;
162         return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
163 }
164
165 static int
166 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
167 {
168         struct g_part_bsd_table *table;
169         const u_char *codeptr;
170         size_t hdsz, tlsz;
171         size_t codesz, tlofs;
172
173         hdsz = 512;
174         tlofs = hdsz + 148 + basetable->gpt_entries * 16;
175         tlsz = BBSIZE - tlofs;
176         table = (struct g_part_bsd_table *)basetable;
177         bzero(table->bbarea, hdsz);
178         bzero(table->bbarea + tlofs, tlsz);
179         codeptr = gpp->gpp_codeptr;
180         codesz = MIN(hdsz, gpp->gpp_codesize);
181         if (codesz > 0)
182                 bcopy(codeptr, table->bbarea, codesz);
183         codesz = MIN(tlsz, gpp->gpp_codesize - tlofs);
184         if (codesz > 0)
185                 bcopy(codeptr + tlofs, table->bbarea + tlofs, codesz);
186         return (0);
187 }
188
189 static int
190 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
191 {
192         struct g_consumer *cp;
193         struct g_provider *pp;
194         struct g_part_entry *baseentry;
195         struct g_part_bsd_entry *entry;
196         struct g_part_bsd_table *table;
197         u_char *ptr;
198         uint32_t msize, ncyls, secpercyl;
199
200         pp = gpp->gpp_provider;
201         cp = LIST_FIRST(&pp->consumers);
202
203         if (pp->sectorsize < sizeof(struct disklabel))
204                 return (ENOSPC);
205         if (BBSIZE % pp->sectorsize)
206                 return (ENOTBLK);
207
208         msize = MIN(pp->mediasize / pp->sectorsize, 0xffffffff);
209         secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
210         ncyls = msize / secpercyl;
211
212         table = (struct g_part_bsd_table *)basetable;
213         table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
214         ptr = table->bbarea + pp->sectorsize;
215
216         le32enc(ptr + 0, DISKMAGIC);                    /* d_magic */
217         le32enc(ptr + 40, pp->sectorsize);              /* d_secsize */
218         le32enc(ptr + 44, basetable->gpt_sectors);      /* d_nsectors */
219         le32enc(ptr + 48, basetable->gpt_heads);        /* d_ntracks */
220         le32enc(ptr + 52, ncyls);                       /* d_ncylinders */
221         le32enc(ptr + 56, secpercyl);                   /* d_secpercyl */
222         le32enc(ptr + 60, msize);                       /* d_secperunit */
223         le16enc(ptr + 72, 3600);                        /* d_rpm */
224         le32enc(ptr + 132, DISKMAGIC);                  /* d_magic2 */
225         le16enc(ptr + 138, basetable->gpt_entries);     /* d_npartitions */
226         le32enc(ptr + 140, BBSIZE);                     /* d_bbsize */
227
228         basetable->gpt_first = 0;
229         basetable->gpt_last = msize - 1;
230         basetable->gpt_isleaf = 1;
231
232         baseentry = g_part_new_entry(basetable, RAW_PART + 1,
233             basetable->gpt_first, basetable->gpt_last);
234         baseentry->gpe_internal = 1;
235         entry = (struct g_part_bsd_entry *)baseentry;
236         entry->part.p_size = basetable->gpt_last + 1;
237         entry->part.p_offset = table->offset;
238
239         return (0);
240 }
241
242 static int
243 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
244 {
245         struct g_part_bsd_table *table;
246
247         table = (struct g_part_bsd_table *)basetable;
248         if (table->bbarea != NULL)
249                 g_free(table->bbarea);
250         table->bbarea = NULL;
251
252         /* Wipe the second sector to clear the partitioning. */
253         basetable->gpt_smhead |= 2;
254         return (0);
255 }
256
257 static void
258 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 
259     struct sbuf *sb, const char *indent)
260 {
261         struct g_part_bsd_entry *entry;
262
263         entry = (struct g_part_bsd_entry *)baseentry;
264         if (indent == NULL) {
265                 /* conftxt: libdisk compatibility */
266                 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
267         } else if (entry != NULL) {
268                 /* confxml: partition entry information */
269                 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
270                     entry->part.p_fstype);
271         } else {
272                 /* confxml: scheme information */
273         }
274 }
275
276 static int
277 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)  
278 {
279         struct g_part_bsd_entry *entry;
280
281         /* Allow dumping to a swap partition or an unused partition. */
282         entry = (struct g_part_bsd_entry *)baseentry;
283         return ((entry->part.p_fstype == FS_UNUSED ||
284             entry->part.p_fstype == FS_SWAP) ? 1 : 0);
285 }
286
287 static int
288 g_part_bsd_modify(struct g_part_table *basetable,
289     struct g_part_entry *baseentry, struct g_part_parms *gpp)
290 {
291         struct g_part_bsd_entry *entry;
292
293         if (gpp->gpp_parms & G_PART_PARM_LABEL)
294                 return (EINVAL);
295
296         entry = (struct g_part_bsd_entry *)baseentry;
297         if (gpp->gpp_parms & G_PART_PARM_TYPE)
298                 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
299         return (0);
300 }
301
302 static int
303 g_part_bsd_resize(struct g_part_table *basetable,
304     struct g_part_entry *baseentry, struct g_part_parms *gpp)
305 {
306         struct g_part_bsd_entry *entry;
307
308         entry = (struct g_part_bsd_entry *)baseentry;
309         baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
310         entry->part.p_size = gpp->gpp_size;
311
312         return (0);
313 }
314
315 static const char *
316 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
317     char *buf, size_t bufsz)
318 {
319
320         snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
321         return (buf);
322 }
323
324 static int
325 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
326 {
327         struct g_provider *pp;
328         u_char *buf;
329         uint32_t magic1, magic2;
330         int error;
331
332         pp = cp->provider;
333
334         /* Sanity-check the provider. */
335         if (pp->sectorsize < sizeof(struct disklabel) ||
336             pp->mediasize < BBSIZE)
337                 return (ENOSPC);
338         if (BBSIZE % pp->sectorsize)
339                 return (ENOTBLK);
340
341         /* Check that there's a disklabel. */
342         buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
343         if (buf == NULL)
344                 return (error);
345         magic1 = le32dec(buf + 0);
346         magic2 = le32dec(buf + 132);
347         g_free(buf);
348         return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
349             ? G_PART_PROBE_PRI_HIGH : ENXIO);
350 }
351
352 static int
353 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
354 {
355         struct g_provider *pp;
356         struct g_part_bsd_table *table;
357         struct g_part_entry *baseentry;
358         struct g_part_bsd_entry *entry;
359         struct partition part;
360         u_char *buf, *p;
361         off_t chs, msize;
362         u_int sectors, heads;
363         int error, index;
364
365         pp = cp->provider;
366         table = (struct g_part_bsd_table *)basetable;
367         msize = pp->mediasize / pp->sectorsize;
368
369         table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
370         if (table->bbarea == NULL)
371                 return (error);
372
373         buf = table->bbarea + pp->sectorsize;
374
375         if (le32dec(buf + 40) != pp->sectorsize)
376                 goto invalid_label;
377         sectors = le32dec(buf + 44);
378         if (sectors < 1 || sectors > 255)
379                 goto invalid_label;
380         if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
381                 g_part_geometry_heads(msize, sectors, &chs, &heads);
382                 if (chs != 0) {
383                         basetable->gpt_sectors = sectors;
384                         basetable->gpt_heads = heads;
385                 }
386         }
387         heads = le32dec(buf + 48);
388         if (heads < 1 || heads > 255)
389                 goto invalid_label;
390         if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
391                 basetable->gpt_heads = heads;
392         if (sectors != basetable->gpt_sectors || heads != basetable->gpt_heads)
393                 printf("GEOM: %s: geometry does not match label"
394                     " (%uh,%us != %uh,%us).\n", pp->name, heads, sectors,
395                     basetable->gpt_heads, basetable->gpt_sectors);
396
397         chs = le32dec(buf + 60);
398         if (chs < 1)
399                 goto invalid_label;
400         /* Fix-up a sysinstall bug. */
401         if (chs > msize) {
402                 chs = msize;
403                 le32enc(buf + 60, msize);
404         }
405         if (chs != msize)
406                 printf("GEOM: %s: media size does not match label.\n",
407                     pp->name);
408
409         basetable->gpt_first = 0;
410         basetable->gpt_last = msize - 1;
411         basetable->gpt_isleaf = 1;
412
413         basetable->gpt_entries = le16dec(buf + 138);
414         if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
415             basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
416                 goto invalid_label;
417
418         table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
419         for (index = basetable->gpt_entries - 1; index >= 0; index--) {
420                 p = buf + 148 + index * 16;
421                 part.p_size = le32dec(p + 0);
422                 part.p_offset = le32dec(p + 4);
423                 part.p_fsize = le32dec(p + 8);
424                 part.p_fstype = p[12];
425                 part.p_frag = p[13];
426                 part.p_cpg = le16dec(p + 14);
427                 if (part.p_size == 0)
428                         continue;
429                 if (part.p_offset < table->offset)
430                         continue;
431                 baseentry = g_part_new_entry(basetable, index + 1,
432                     part.p_offset - table->offset,
433                     part.p_offset - table->offset + part.p_size - 1);
434                 entry = (struct g_part_bsd_entry *)baseentry;
435                 entry->part = part;
436                 if (index == RAW_PART)
437                         baseentry->gpe_internal = 1;
438         }
439
440         return (0);
441
442  invalid_label:
443         printf("GEOM: %s: invalid disklabel.\n", pp->name);
444         g_free(table->bbarea);
445         return (EINVAL);
446 }
447
448 static const char *
449 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 
450     char *buf, size_t bufsz)
451 {
452         struct g_part_bsd_entry *entry;
453         int type;
454
455         entry = (struct g_part_bsd_entry *)baseentry;
456         type = entry->part.p_fstype;
457         if (type == FS_SWAP)
458                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
459         if (type == FS_BSDFFS)
460                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
461         if (type == FS_VINUM)
462                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
463         if (type == FS_ZFS)
464                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
465         snprintf(buf, bufsz, "!%d", type);
466         return (buf);
467 }
468
469 static int
470 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
471 {
472         struct g_provider *pp;
473         struct g_part_entry *baseentry;
474         struct g_part_bsd_entry *entry;
475         struct g_part_bsd_table *table;
476         uint16_t sum;
477         u_char *label, *p, *pe;
478         int error, index;
479
480         pp = cp->provider;
481         table = (struct g_part_bsd_table *)basetable;
482         baseentry = LIST_FIRST(&basetable->gpt_entry);
483         label = table->bbarea + pp->sectorsize;
484         for (index = 1; index <= basetable->gpt_entries; index++) {
485                 p = label + 148 + (index - 1) * 16;
486                 entry = (baseentry != NULL && index == baseentry->gpe_index)
487                     ? (struct g_part_bsd_entry *)baseentry : NULL;
488                 if (entry != NULL && !baseentry->gpe_deleted) {
489                         le32enc(p + 0, entry->part.p_size);
490                         le32enc(p + 4, entry->part.p_offset);
491                         le32enc(p + 8, entry->part.p_fsize);
492                         p[12] = entry->part.p_fstype;
493                         p[13] = entry->part.p_frag;
494                         le16enc(p + 14, entry->part.p_cpg);
495                 } else
496                         bzero(p, 16);
497
498                 if (entry != NULL)
499                         baseentry = LIST_NEXT(baseentry, gpe_entry);
500         }
501
502         /* Calculate checksum. */
503         le16enc(label + 136, 0);
504         pe = label + 148 + basetable->gpt_entries * 16;
505         sum = 0;
506         for (p = label; p < pe; p += 2)
507                 sum ^= le16dec(p);
508         le16enc(label + 136, sum);
509
510         error = g_write_data(cp, 0, table->bbarea, BBSIZE);
511         return (error);
512 }