]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_bsd.c
Eliminate some thread pointers which do not make sense anymore.
[FreeBSD/FreeBSD.git] / sys / geom / geom_bsd.c
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37
38
39 #include <sys/param.h>
40 #ifndef _KERNEL
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <signal.h>
45 #include <err.h>
46 #else
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/conf.h>
50 #include <sys/bio.h>
51 #include <sys/malloc.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #endif
55 #include <sys/errno.h>
56 #include <sys/disklabel.h>
57 #include <geom/geom.h>
58 #include <geom/geom_slice.h>
59
60 #define BSD_CLASS_NAME "BSD-class"
61
62 struct g_bsd_softc {
63         struct disklabel ondisk;
64         struct disklabel inram;
65 };
66
67 static void
68 g_bsd_ledec_partition(u_char *ptr, struct partition *d)
69 {
70         d->p_size = g_dec_le4(ptr + 0);
71         d->p_offset = g_dec_le4(ptr + 4);
72         d->p_fsize = g_dec_le4(ptr + 8);
73         d->p_fstype = ptr[12];
74         d->p_frag = ptr[13];
75         d->p_cpg = g_dec_le2(ptr + 14);
76 }
77
78 static void
79 g_bsd_ledec_disklabel(u_char *ptr, struct disklabel *d)
80 {
81         d->d_magic = g_dec_le4(ptr + 0);
82         d->d_type = g_dec_le2(ptr + 4);
83         d->d_subtype = g_dec_le2(ptr + 6);
84         bcopy(ptr + 8, d->d_typename, 16);
85         bcopy(d->d_packname, ptr + 24, 16);
86         d->d_secsize = g_dec_le4(ptr + 40);
87         d->d_nsectors = g_dec_le4(ptr + 44);
88         d->d_ntracks = g_dec_le4(ptr + 48);
89         d->d_ncylinders = g_dec_le4(ptr + 52);
90         d->d_secpercyl = g_dec_le4(ptr + 56);
91         d->d_secperunit = g_dec_le4(ptr + 60);
92         d->d_sparespertrack = g_dec_le2(ptr + 64);
93         d->d_sparespercyl = g_dec_le2(ptr + 66);
94         d->d_acylinders = g_dec_le4(ptr + 68);
95         d->d_rpm = g_dec_le2(ptr + 72);
96         d->d_interleave = g_dec_le2(ptr + 74);
97         d->d_trackskew = g_dec_le2(ptr + 76);
98         d->d_cylskew = g_dec_le2(ptr + 78);
99         d->d_headswitch = g_dec_le4(ptr + 80);
100         d->d_trkseek = g_dec_le4(ptr + 84);
101         d->d_flags = g_dec_le4(ptr + 88);
102         d->d_drivedata[0] = g_dec_le4(ptr + 92);
103         d->d_drivedata[1] = g_dec_le4(ptr + 96);
104         d->d_drivedata[2] = g_dec_le4(ptr + 100);
105         d->d_drivedata[3] = g_dec_le4(ptr + 104);
106         d->d_drivedata[4] = g_dec_le4(ptr + 108);
107         d->d_spare[0] = g_dec_le4(ptr + 112);
108         d->d_spare[1] = g_dec_le4(ptr + 116);
109         d->d_spare[2] = g_dec_le4(ptr + 120);
110         d->d_spare[3] = g_dec_le4(ptr + 124);
111         d->d_spare[4] = g_dec_le4(ptr + 128);
112         d->d_magic2 = g_dec_le4(ptr + 132);
113         d->d_checksum = g_dec_le2(ptr + 136);
114         d->d_npartitions = g_dec_le2(ptr + 138);
115         d->d_bbsize = g_dec_le4(ptr + 140);
116         d->d_sbsize = g_dec_le4(ptr + 144);
117         g_bsd_ledec_partition(ptr + 148, &d->d_partitions[0]);
118         g_bsd_ledec_partition(ptr + 164, &d->d_partitions[1]);
119         g_bsd_ledec_partition(ptr + 180, &d->d_partitions[2]);
120         g_bsd_ledec_partition(ptr + 196, &d->d_partitions[3]);
121         g_bsd_ledec_partition(ptr + 212, &d->d_partitions[4]);
122         g_bsd_ledec_partition(ptr + 228, &d->d_partitions[5]);
123         g_bsd_ledec_partition(ptr + 244, &d->d_partitions[6]);
124         g_bsd_ledec_partition(ptr + 260, &d->d_partitions[7]);
125 }
126
127 #if 0
128 static void
129 g_bsd_leenc_partition(u_char *ptr, struct partition *d)
130 {
131         g_enc_le4(ptr + 0, d->p_size);
132         g_enc_le4(ptr + 4, d->p_offset);
133         g_enc_le4(ptr + 8, d->p_fsize);
134         ptr[12] = d->p_fstype;
135         ptr[13] = d->p_frag;
136         g_enc_le2(ptr + 14, d->p_cpg);
137 }
138
139 static void
140 g_bsd_leenc_disklabel(u_char *ptr, struct disklabel *d)
141 {
142         g_enc_le4(ptr + 0, d->d_magic);
143         g_enc_le2(ptr + 4, d->d_type);
144         g_enc_le2(ptr + 6, d->d_subtype);
145         bcopy(d->d_typename, ptr + 8, 16);
146         bcopy(d->d_packname, ptr + 24, 16);
147         g_enc_le4(ptr + 40, d->d_secsize);
148         g_enc_le4(ptr + 44, d->d_nsectors);
149         g_enc_le4(ptr + 48, d->d_ntracks);
150         g_enc_le4(ptr + 52, d->d_ncylinders);
151         g_enc_le4(ptr + 56, d->d_secpercyl);
152         g_enc_le4(ptr + 60, d->d_secperunit);
153         g_enc_le2(ptr + 64, d->d_sparespertrack);
154         g_enc_le2(ptr + 66, d->d_sparespercyl);
155         g_enc_le4(ptr + 68, d->d_acylinders);
156         g_enc_le2(ptr + 72, d->d_rpm);
157         g_enc_le2(ptr + 74, d->d_interleave);
158         g_enc_le2(ptr + 76, d->d_trackskew);
159         g_enc_le2(ptr + 78, d->d_cylskew);
160         g_enc_le4(ptr + 80, d->d_headswitch);
161         g_enc_le4(ptr + 84, d->d_trkseek);
162         g_enc_le4(ptr + 88, d->d_flags);
163         g_enc_le4(ptr + 92, d->d_drivedata[0]);
164         g_enc_le4(ptr + 96, d->d_drivedata[1]);
165         g_enc_le4(ptr + 100, d->d_drivedata[2]);
166         g_enc_le4(ptr + 104, d->d_drivedata[3]);
167         g_enc_le4(ptr + 108, d->d_drivedata[4]);
168         g_enc_le4(ptr + 112, d->d_spare[0]);
169         g_enc_le4(ptr + 116, d->d_spare[1]);
170         g_enc_le4(ptr + 120, d->d_spare[2]);
171         g_enc_le4(ptr + 124, d->d_spare[3]);
172         g_enc_le4(ptr + 128, d->d_spare[4]);
173         g_enc_le4(ptr + 132, d->d_magic2);
174         g_enc_le2(ptr + 136, d->d_checksum);
175         g_enc_le2(ptr + 138, d->d_npartitions);
176         g_enc_le4(ptr + 140, d->d_bbsize);
177         g_enc_le4(ptr + 144, d->d_sbsize);
178         g_bsd_leenc_partition(ptr + 148, &d->d_partitions[0]);
179         g_bsd_leenc_partition(ptr + 164, &d->d_partitions[1]);
180         g_bsd_leenc_partition(ptr + 180, &d->d_partitions[2]);
181         g_bsd_leenc_partition(ptr + 196, &d->d_partitions[3]);
182         g_bsd_leenc_partition(ptr + 212, &d->d_partitions[4]);
183         g_bsd_leenc_partition(ptr + 228, &d->d_partitions[5]);
184         g_bsd_leenc_partition(ptr + 244, &d->d_partitions[6]);
185         g_bsd_leenc_partition(ptr + 260, &d->d_partitions[7]);
186 }
187
188 #endif
189
190 static void
191 ondisk2inram(struct g_bsd_softc *sc)
192 {
193         struct partition *ppp;
194         unsigned offset;
195         int i;
196
197         sc->inram = sc->ondisk;
198         offset = sc->inram.d_partitions[RAW_PART].p_offset;
199         for (i = 0; i < 8; i++) {
200                 ppp = &sc->inram.d_partitions[i];
201                 if (ppp->p_offset >= offset)
202                         ppp->p_offset -= offset;
203         }
204         sc->inram.d_checksum = 0;
205         sc->inram.d_checksum = dkcksum(&sc->inram);
206 }
207
208 /*
209  * It is rather fortunate that this checksum only covers up to the
210  * actual end of actual data, otherwise the pointer-screwup in
211  * alpha architectures would have been much harder to handle.
212  */
213 static int
214 g_bsd_lesum(struct disklabel *dl, u_char *p)
215 {
216         u_char *pe;
217         uint16_t sum;
218
219         pe = p + 148 + 16 * dl->d_npartitions;
220         sum = 0;
221         while (p < pe) {
222                 sum ^= g_dec_le2(p);
223                 p += 2;
224         }
225         return (sum);
226 }
227
228 static int
229 g_bsd_i386(struct g_consumer *cp, int secsize, struct disklabel *dl)
230 {
231         int error;
232         u_char *buf;
233
234         buf = g_read_data(cp, secsize * 1, secsize, &error);
235         if (buf == NULL || error != 0)
236                 return(ENOENT);
237         g_bsd_ledec_disklabel(buf, dl);
238         if (dl->d_magic == DISKMAGIC &&
239             dl->d_magic2 == DISKMAGIC &&
240             g_bsd_lesum(dl, buf) == 0) 
241                 error = 0;
242         else
243                 error = ENOENT;
244         g_free(buf);
245         return(error);
246 }
247
248 static int
249 g_bsd_alpha(struct g_consumer *cp, int secsize, struct disklabel *dl)
250 {
251         int error;
252         u_char *buf;
253
254         buf = g_read_data(cp, 0, secsize, &error);
255         if (buf == NULL || error != 0)
256                 return(ENOENT);
257         g_bsd_ledec_disklabel(buf + 64, dl);
258         if (dl->d_magic == DISKMAGIC &&
259             dl->d_magic2 == DISKMAGIC &&
260             g_bsd_lesum(dl, buf) == 0) 
261                 error = 0;
262         else
263                 error = ENOENT;
264         g_free(buf);
265         return(error);
266 }
267
268 static int
269 g_bsd_start(struct bio *bp)
270 {
271         struct g_geom *gp;
272         struct g_bsd_softc *ms;
273         struct g_slicer *gsp;
274         struct g_ioctl *gio;
275
276         gp = bp->bio_to->geom;
277         gsp = gp->softc;
278         ms = gsp->softc;
279         if (strcmp(bp->bio_attribute, "GEOM::ioctl"))
280                 return(0);
281         else if (bp->bio_length != sizeof *gio)
282                 return(0);
283         gio = (struct g_ioctl *)bp->bio_data;
284         if (gio->cmd == DIOCGDINFO) {
285                 bcopy(&ms->inram, gio->data, sizeof ms->inram);
286                 bp->bio_error = 0;
287                 g_io_deliver(bp);
288                 return (1);
289         }
290 #ifdef _KERNEL
291         if (gio->cmd == DIOCGPART) {
292                 struct partinfo pi;
293                 pi.disklab = &ms->inram;
294                 pi.part = &ms->inram.d_partitions[bp->bio_to->index];
295                 bcopy(&pi, gio->data, sizeof pi);
296                 bp->bio_error = 0;
297                 g_io_deliver(bp);
298                 return (1);
299         }
300 #endif
301         return (0);
302 }
303
304 static void
305 g_bsd_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
306 {
307 #if 0
308         struct g_mbr_softc *ms;
309         struct g_slicer *gsp;
310
311         gsp = gp->softc;
312         ms = gsp->softc;
313         if (pp != NULL) {
314                 sbuf_printf(sb, "%s<type>%d</type>\n",
315                     indent, ms->type[pp->index]);
316         }
317 #endif
318         g_slice_dumpconf(sb, indent, gp, cp, pp);
319 }
320
321 static struct g_geom *
322 g_bsd_taste(struct g_class *mp, struct g_provider *pp, int flags)
323 {
324         struct g_geom *gp;
325         struct g_consumer *cp;
326         struct g_provider *pp2;
327         int error, i, j, npart;
328         struct g_bsd_softc *ms;
329         struct disklabel *dl;
330         u_int secsize;
331         u_int fwsectors, fwheads;
332         off_t mediasize;
333         struct partition *ppp, *ppr;
334
335         g_trace(G_T_TOPOLOGY, "bsd_taste(%s,%s)", mp->name, pp->name);
336         g_topology_assert();
337         if (flags == G_TF_NORMAL &&
338             !strcmp(pp->geom->class->name, BSD_CLASS_NAME))
339                 return (NULL);
340         gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_bsd_start);
341         if (gp == NULL)
342                 return (NULL);
343         g_topology_unlock();
344         gp->dumpconf = g_bsd_dumpconf;
345         npart = 0;
346         while (1) {     /* a trick to allow us to use break */
347                 j = sizeof i;
348                 error = g_io_getattr("MBR::type", cp, &j, &i);
349                 if (!error && i != 165 && flags == G_TF_NORMAL)
350                         break;
351                 j = sizeof secsize;
352                 error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize);
353                 if (error) {
354                         secsize = 512;
355                         printf("g_bsd_taste: error %d Sectors are %d bytes\n",
356                             error, secsize);
357                 }
358                 j = sizeof mediasize;
359                 error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize);
360                 if (error) {
361                         mediasize = 0;
362                         printf("g_error %d Mediasize is %lld bytes\n",
363                             error, mediasize);
364                 }
365                 error = g_bsd_i386(cp, secsize, &ms->ondisk);
366                 if (error)
367                         error = g_bsd_alpha(cp, secsize, &ms->ondisk);
368                 if (error)
369                         break;
370                 dl = &ms->ondisk;
371                 if (bootverbose)
372                         g_hexdump(dl, sizeof(*dl));
373                 if (dl->d_secsize < secsize)
374                         break;
375                 if (dl->d_secsize > secsize)
376                         secsize = dl->d_secsize;
377                 ppr = &dl->d_partitions[2];
378                 for (i = 0; i < 8; i++) {
379                         ppp = &dl->d_partitions[i];
380                         if (ppp->p_size == 0)
381                                 continue;
382                         npart++;
383                         pp2 = g_slice_addslice(gp, i,
384                             ((off_t)(ppp->p_offset - ppr->p_offset)) << 9ULL,
385                             ((off_t)ppp->p_size) << 9ULL,
386                             "%s%c", pp->name, 'a' + i);
387                         g_error_provider(pp2, 0);
388                 }
389                 ondisk2inram(ms);
390                 break;
391         }
392         if (npart == 0 && (
393             (flags == G_TF_INSIST && mediasize != 0) ||
394             (flags == G_TF_TRANSPARENT))) {
395                 dl = &ms->ondisk;
396                 bzero(dl, sizeof *dl);
397                 dl->d_magic = DISKMAGIC;
398                 dl->d_magic2 = DISKMAGIC;
399                 ppp = &dl->d_partitions[RAW_PART];
400                 ppp->p_offset = 0;
401                 ppp->p_size = mediasize / secsize;
402                 dl->d_npartitions = MAXPARTITIONS;
403                 dl->d_interleave = 1;
404                 dl->d_secsize = secsize;
405                 dl->d_rpm = 3600;
406                 j = sizeof fwsectors;
407                 error = g_io_getattr("GEOM::fwsectors", cp, &j, &fwsectors);
408                 if (error)
409                         dl->d_nsectors = 32;
410                 else
411                         dl->d_nsectors = fwsectors;
412                 error = g_io_getattr("GEOM::fwheads", cp, &j, &fwheads);
413                 if (error)
414                         dl->d_ntracks = 64;
415                 else
416                         dl->d_ntracks = fwheads;
417                 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
418                 dl->d_ncylinders = ppp->p_size / dl->d_secpercyl;
419                 dl->d_secperunit = ppp->p_size;
420                 dl->d_checksum = 0;
421                 dl->d_checksum = dkcksum(dl);
422                 ms->inram = ms->ondisk;
423                 pp2 = g_slice_addslice(gp, RAW_PART,
424                     0, mediasize, "%s%c", pp->name, 'a' + RAW_PART);
425                 g_error_provider(pp2, 0);
426                 npart = 1;
427         }
428         g_topology_lock();
429         error = g_access_rel(cp, -1, 0, 0);
430         if (npart > 0)
431                 return (gp);
432         g_std_spoiled(cp);
433         return (NULL);
434 }
435
436 static struct g_class g_bsd_class       = {
437         BSD_CLASS_NAME,
438         g_bsd_taste,
439         g_slice_access,
440         g_slice_orphan,
441         NULL,
442         G_CLASS_INITSTUFF
443 };
444
445 DECLARE_GEOM_CLASS(g_bsd_class, g_bsd);