sbin/fsck_hammer2: Add destroy.c to destroy ondisk inode/dirent
[dragonfly.git] / sys / kern / subr_diskmbr.c
1 /*-
2  * Copyright (c) 1994 Bruce D. Evans.
3  * All rights reserved.
4  *
5  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from: @(#)ufs_disksubr.c        7.16 (Berkeley) 5/4/91
33  *      from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $
34  * $FreeBSD: src/sys/kern/subr_diskmbr.c,v 1.45 2000/01/28 10:22:07 bde Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/buf.h>
40 #include <sys/conf.h>
41 #include <sys/diskslice.h>
42 #define DOSPTYP_EXTENDED        5
43 #define DOSPTYP_EXTENDEDX       15
44 #define DOSPTYP_ONTRACK         84
45 #include <sys/diskmbr.h>
46 #include <sys/disk.h>
47 #include <sys/malloc.h>
48 #include <sys/syslog.h>
49 #include <sys/device.h>
50
51 #define TRACE(str)      do { if (dsi_debug) kprintf str; } while (0)
52
53 static volatile u_char dsi_debug;
54
55 /*
56  * This is what we have embedded in every boot1 for supporting the bogus
57  * "Dangerously Dedicated" mode. However, the old table is broken because
58  * it has an illegal geometry in it - it specifies 256 heads (heads = end
59  * head + 1) which causes nasty stuff when that wraps to zero in bios code.
60  * eg: divide by zero etc. This caused the dead-thinkpad problem, numerous
61  * SCSI bios crashes, EFI to crash, etc.
62  *
63  * We still have to recognize the old table though, even though we stopped
64  * inflicting it upon the world.
65  */
66 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
67         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
68         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
69         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
70         { 0x80, 0, 1, 0, DOSPTYP_DFLYBSD, 255, 255, 255, 0, 50000, },
71 };
72 static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = {
73         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
74         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
75         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
76         { 0x80, 0, 1, 0, DOSPTYP_DFLYBSD, 254, 255, 255, 0, 50000, },
77 };
78
79 static int check_part (char *sname, struct dos_partition *dp,
80                            u_int64_t offset, int nsectors, int ntracks,
81                            u_int64_t mbr_offset);
82 static void mbr_extended (cdev_t dev, struct disk_info *info,
83                               struct diskslices *ssp, u_int64_t ext_offset,
84                               u_int64_t ext_size, u_int64_t base_ext_offset,
85                               int nsectors, int ntracks, u_int64_t mbr_offset,
86                               int level);
87 static int mbr_setslice (char *sname, struct disk_info *info,
88                              struct diskslice *sp, struct dos_partition *dp,
89                              u_int64_t br_offset);
90
91
92 int
93 mbrinit(cdev_t dev, struct disk_info *info, struct diskslices **sspp)
94 {
95         struct buf *bp;
96         u_char  *cp;
97         int     dospart;
98         struct dos_partition *dp;
99         struct dos_partition *dp0;
100         struct dos_partition dpcopy[NDOSPART];
101         int     error;
102         int     max_ncyls;
103         int     max_nsectors;
104         int     max_ntracks;
105         u_int64_t mbr_offset;
106         char    partname[2];
107         u_long  secpercyl;
108         char    *sname = "tempname";
109         struct diskslice *sp;
110         struct diskslices *ssp;
111         cdev_t wdev;
112
113         mbr_offset = DOSBBSECTOR;
114 reread_mbr:
115         /*
116          * Don't bother if the block size is weird or the
117          * media size is 0 (probably means no media present).
118          */
119         if (info->d_media_blksize & DEV_BMASK)
120                 return (EIO);
121         if (info->d_media_size == 0)
122                 return (EIO);
123
124         /*
125          * Read master boot record.
126          */
127         wdev = dev;
128         bp = getpbuf_mem(NULL);
129         KKASSERT((int)info->d_media_blksize <= bp->b_bufsize);
130         bp->b_bio1.bio_offset = (off_t)mbr_offset * info->d_media_blksize;
131         bp->b_bio1.bio_done = biodone_sync;
132         bp->b_bio1.bio_flags |= BIO_SYNC;
133         bp->b_bcount = info->d_media_blksize;
134         bp->b_cmd = BUF_CMD_READ;
135         bp->b_flags |= B_FAILONDIS;
136         dev_dstrategy(wdev, &bp->b_bio1);
137         if (biowait(&bp->b_bio1, "mbrrd") != 0) {
138                 if ((info->d_dsflags & DSO_MBRQUIET) == 0) {
139                         diskerr(&bp->b_bio1, wdev,
140                                 "reading primary partition table: error",
141                                 LOG_PRINTF, 0);
142                         kprintf("\n");
143                 }
144                 error = EIO;
145                 goto done;
146         }
147
148         /* Weakly verify it. */
149         cp = bp->b_data;
150         sname = dsname(dev, 0, 0, 0, NULL);
151         if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
152                 if (bootverbose)
153                         kprintf("%s: invalid primary partition table: no magic\n",
154                                sname);
155                 error = EINVAL;
156                 goto done;
157         }
158
159         /* Make a copy of the partition table to avoid alignment problems. */
160         memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
161
162         dp0 = &dpcopy[0];
163
164         /*
165          * Check for "Ontrack Diskmanager" or GPT.  If a GPT is found in
166          * the first dos partition, ignore the rest of the MBR and go
167          * to GPT processing.
168          */
169         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
170                 if (dospart == 0 && dp->dp_typ == DOSPTYP_PMBR) {
171                         if (bootverbose)
172                                 kprintf(
173             "%s: Found GPT in slice #%d\n", sname, dospart + 1);
174                         error = gptinit(dev, info, sspp);
175                         goto done;
176                 }
177
178                 if (dp->dp_typ == DOSPTYP_ONTRACK) {
179                         if (bootverbose)
180                                 kprintf(
181             "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname);
182                         bp->b_flags |= B_INVAL | B_AGE;
183                         brelse(bp);
184                         mbr_offset = 63;
185                         goto reread_mbr;
186                 }
187         }
188
189         if (bcmp(dp0, historical_bogus_partition_table,
190                  sizeof historical_bogus_partition_table) == 0 ||
191             bcmp(dp0, historical_bogus_partition_table_fixed,
192                  sizeof historical_bogus_partition_table_fixed) == 0) {
193 #if 0
194                 TRACE(("%s: invalid primary partition table: historical\n",
195                        sname));
196 #endif /* 0 */
197                 if (bootverbose)
198                         kprintf(
199      "%s: invalid primary partition table: Dangerously Dedicated (ignored)\n",
200                                sname);
201                 error = EINVAL;
202                 goto done;
203         }
204
205         /* Guess the geometry. */
206         /*
207          * TODO:
208          * Perhaps skip entries with 0 size.
209          * Perhaps only look at entries of type DOSPTYP_386BSD or
210          * DOSPTYP_DFLYBSD
211          */
212         max_ncyls = 0;
213         max_nsectors = 0;
214         max_ntracks = 0;
215         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
216                 int     ncyls;
217                 int     nsectors;
218                 int     ntracks;
219
220                 ncyls = DPCYL(dp->dp_ecyl, dp->dp_esect) + 1;
221                 if (max_ncyls < ncyls)
222                         max_ncyls = ncyls;
223                 nsectors = DPSECT(dp->dp_esect);
224                 if (max_nsectors < nsectors)
225                         max_nsectors = nsectors;
226                 ntracks = dp->dp_ehd + 1;
227                 if (max_ntracks < ntracks)
228                         max_ntracks = ntracks;
229         }
230
231         /*
232          * Check that we have guessed the geometry right by checking the
233          * partition entries.
234          */
235         /*
236          * TODO:
237          * As above.
238          * Check for overlaps.
239          * Check against d_secperunit if the latter is reliable.
240          */
241         error = 0;
242         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
243                 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
244                     && dp->dp_start == 0 && dp->dp_size == 0)
245                         continue;
246                 //sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
247                 //             WHOLE_SLICE_PART, partname);
248
249                 /*
250                  * Temporarily ignore errors from this check.  We could
251                  * simplify things by accepting the table eariler if we
252                  * always ignore errors here.  Perhaps we should always
253                  * accept the table if the magic is right but not let
254                  * bad entries affect the geometry.
255                  */
256                 check_part(sname, dp, mbr_offset, max_nsectors, max_ntracks,
257                            mbr_offset);
258         }
259         if (error != 0)
260                 goto done;
261
262         /*
263          * Accept the DOS partition table.
264          *
265          * Adjust the disk information structure with updated CHS
266          * conversion parameters, but only use values extracted from
267          * the primary partition table.
268          *
269          * NOTE!  Regardless of our having to deal with this old cruft,
270          * we do not screw around with the info->d_media* parameters.
271          */
272         secpercyl = (u_long)max_nsectors * max_ntracks;
273         if (secpercyl != 0 && mbr_offset == DOSBBSECTOR) {
274                 info->d_secpertrack = max_nsectors;
275                 info->d_nheads = max_ntracks;
276                 info->d_secpercyl = secpercyl;
277                 info->d_ncylinders = info->d_media_blocks / secpercyl;
278         }
279
280         /*
281          * We are passed a pointer to a suitably initialized minimal
282          * slices "struct" with no dangling pointers in it.  Replace it
283          * by a maximal one.  This usually oversizes the "struct", but
284          * enlarging it while searching for logical drives would be
285          * inconvenient.
286          */
287         kfree(*sspp, M_DEVBUF);
288         ssp = dsmakeslicestruct(MAX_SLICES, info);
289         *sspp = ssp;
290
291         /* Initialize normal slices. */
292         sp = &ssp->dss_slices[BASE_SLICE];
293         for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++, sp++) {
294                 sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
295                                WHOLE_SLICE_PART, partname);
296                 (void)mbr_setslice(sname, info, sp, dp, mbr_offset);
297         }
298         ssp->dss_nslices = BASE_SLICE + NDOSPART;
299
300         /* Handle extended partitions. */
301         sp -= NDOSPART;
302         for (dospart = 0; dospart < NDOSPART; dospart++, sp++) {
303                 if (sp->ds_type == DOSPTYP_EXTENDED ||
304                     sp->ds_type == DOSPTYP_EXTENDEDX) {
305                         mbr_extended(wdev, info, ssp,
306                                      sp->ds_offset, sp->ds_size, sp->ds_offset,
307                                      max_nsectors, max_ntracks, mbr_offset, 1);
308                 }
309         }
310
311         /*
312          * mbr_extended() abuses ssp->dss_nslices for the number of slices
313          * that would be found if there were no limit on the number of slices
314          * in *ssp.  Cut it back now.
315          */
316         if (ssp->dss_nslices > MAX_SLICES)
317                 ssp->dss_nslices = MAX_SLICES;
318
319 done:
320         bp->b_flags |= B_INVAL | B_AGE;
321         relpbuf(bp, NULL);
322         if (error == EINVAL)
323                 error = 0;
324         return (error);
325 }
326
327 static int
328 check_part(char *sname, struct dos_partition *dp, u_int64_t offset,
329             int nsectors, int ntracks, u_int64_t mbr_offset)
330 {
331         int     chs_ecyl;
332         int     chs_esect;
333         int     chs_scyl;
334         int     chs_ssect;
335         int     error;
336         u_long  secpercyl;
337         u_int64_t esector;
338         u_int64_t esector1;
339         u_int64_t ssector;
340         u_int64_t ssector1;
341
342         secpercyl = (u_long)nsectors * ntracks;
343         chs_scyl = DPCYL(dp->dp_scyl, dp->dp_ssect);
344         chs_ssect = DPSECT(dp->dp_ssect);
345         ssector = chs_ssect - 1 + dp->dp_shd * nsectors + chs_scyl * secpercyl
346                   + mbr_offset;
347         ssector1 = offset + dp->dp_start;
348
349         /*
350          * If ssector1 is on a cylinder >= 1024, then ssector can't be right.
351          * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct
352          * apart from the cylinder being reduced modulo 1024.  Always allow
353          * 1023/255/63, because this is the official way to represent
354          * pure-LBA for the starting position.
355          */
356         if ((ssector < ssector1
357              && ((chs_ssect == nsectors && dp->dp_shd == ntracks - 1
358                   && chs_scyl == 1023)
359                  || (secpercyl != 0
360                      && (ssector1 - ssector) % (1024 * secpercyl) == 0)))
361             || (dp->dp_scyl == 255 && dp->dp_shd == 255
362                 && dp->dp_ssect == 255)) {
363                 TRACE(("%s: C/H/S start %d/%d/%d, start %llu: allow\n",
364                        sname, chs_scyl, dp->dp_shd, chs_ssect,
365                        (long long)ssector1));
366                 ssector = ssector1;
367         }
368
369         chs_ecyl = DPCYL(dp->dp_ecyl, dp->dp_esect);
370         chs_esect = DPSECT(dp->dp_esect);
371         esector = chs_esect - 1 + dp->dp_ehd * nsectors + chs_ecyl * secpercyl
372                   + mbr_offset;
373         esector1 = ssector1 + dp->dp_size - 1;
374
375         /*
376          * Allow certain bogus C/H/S values for esector, as above. However,
377          * heads == 255 isn't really legal and causes some BIOS crashes. The
378          * correct value to indicate a pure-LBA end is 1023/heads-1/sectors -
379          * usually 1023/254/63. "heads" is base 0, "sectors" is base 1.
380          */
381         if ((esector < esector1
382              && ((chs_esect == nsectors && dp->dp_ehd == ntracks - 1
383                   && chs_ecyl == 1023)
384                  || (secpercyl != 0
385                      && (esector1 - esector) % (1024 * secpercyl) == 0)))
386             || (dp->dp_ecyl == 255 && dp->dp_ehd == 255
387                 && dp->dp_esect == 255)) {
388                 TRACE(("%s: C/H/S end %d/%d/%d, end %llu: allow\n",
389                        sname, chs_ecyl, dp->dp_ehd, chs_esect,
390                        (long long)esector1));
391                 esector = esector1;
392         }
393
394         error = (ssector == ssector1 && esector == esector1) ? 0 : EINVAL;
395         if (bootverbose)
396                 kprintf("%s: type 0x%x, start %llu, end = %llu, size %u %s\n",
397                        sname, dp->dp_typ,
398                        (long long)ssector1, (long long)esector1,
399                        dp->dp_size, (error ? "" : ": OK"));
400         if (ssector != ssector1 && bootverbose)
401                 kprintf("%s: C/H/S start %d/%d/%d (%llu) != start %llu: invalid\n",
402                        sname, chs_scyl, dp->dp_shd, chs_ssect,
403                        (long long)ssector, (long long)ssector1);
404         if (esector != esector1 && bootverbose)
405                 kprintf("%s: C/H/S end %d/%d/%d (%llu) != end %llu: invalid\n",
406                        sname, chs_ecyl, dp->dp_ehd, chs_esect,
407                        (long long)esector, (long long)esector1);
408         return (error);
409 }
410
411 static
412 void
413 mbr_extended(cdev_t dev, struct disk_info *info, struct diskslices *ssp,
414             u_int64_t ext_offset, u_int64_t ext_size, u_int64_t base_ext_offset,
415             int nsectors, int ntracks, u_int64_t mbr_offset, int level)
416 {
417         struct buf *bp;
418         u_char  *cp;
419         int     dospart;
420         struct dos_partition *dp;
421         struct dos_partition dpcopy[NDOSPART];
422         u_int64_t ext_offsets[NDOSPART];
423         u_int64_t ext_sizes[NDOSPART];
424         char    partname[2];
425         int     slice;
426         char    *sname;
427         struct diskslice *sp;
428
429         if (level >= 16) {
430                 kprintf(
431         "%s: excessive recursion in search for slices; aborting search\n",
432                        devtoname(dev));
433                 return;
434         }
435
436         /* Read extended boot record. */
437         bp = getpbuf_mem(NULL);
438         KKASSERT((int)info->d_media_blksize <= bp->b_bufsize);
439         bp->b_bio1.bio_offset = (off_t)ext_offset * info->d_media_blksize;
440         bp->b_bio1.bio_done = biodone_sync;
441         bp->b_bio1.bio_flags |= BIO_SYNC;
442         bp->b_bcount = info->d_media_blksize;
443         bp->b_cmd = BUF_CMD_READ;
444         bp->b_flags |= B_FAILONDIS;
445         dev_dstrategy(dev, &bp->b_bio1);
446         if (biowait(&bp->b_bio1, "mbrrd") != 0) {
447                 diskerr(&bp->b_bio1, dev,
448                         "reading extended partition table: error",
449                         LOG_PRINTF, 0);
450                 kprintf("\n");
451                 goto done;
452         }
453
454         /* Weakly verify it. */
455         cp = bp->b_data;
456         if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
457                 sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, WHOLE_SLICE_PART,
458                                partname);
459                 if (bootverbose)
460                         kprintf("%s: invalid extended partition table: no magic\n",
461                                sname);
462                 goto done;
463         }
464
465         /* Make a copy of the partition table to avoid alignment problems. */
466         memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
467
468         slice = ssp->dss_nslices;
469         for (dospart = 0, dp = &dpcopy[0]; dospart < NDOSPART;
470             dospart++, dp++) {
471                 ext_sizes[dospart] = 0;
472                 if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
473                     && dp->dp_start == 0 && dp->dp_size == 0)
474                         continue;
475                 if (dp->dp_typ == DOSPTYP_EXTENDED ||
476                     dp->dp_typ == DOSPTYP_EXTENDEDX) {
477                         static char buf[32];
478
479                         sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE,
480                                        WHOLE_SLICE_PART, partname);
481                         ksnprintf(buf, sizeof(buf), "%s", sname);
482                         if (strlen(buf) < sizeof buf - 11)
483                                 strcat(buf, "<extended>");
484                         check_part(buf, dp, base_ext_offset, nsectors,
485                                    ntracks, mbr_offset);
486                         ext_offsets[dospart] = base_ext_offset + dp->dp_start;
487                         ext_sizes[dospart] = dp->dp_size;
488                 } else {
489                         sname = dsname(dev, dkunit(dev), slice, WHOLE_SLICE_PART,
490                                        partname);
491                         check_part(sname, dp, ext_offset, nsectors, ntracks,
492                                    mbr_offset);
493                         if (slice >= MAX_SLICES) {
494                                 kprintf("%s: too many slices\n", sname);
495                                 slice++;
496                                 continue;
497                         }
498                         sp = &ssp->dss_slices[slice];
499                         if (mbr_setslice(sname, info, sp, dp, ext_offset) != 0)
500                                 continue;
501                         slice++;
502                 }
503         }
504         ssp->dss_nslices = slice;
505
506         /* If we found any more slices, recursively find all the subslices. */
507         for (dospart = 0; dospart < NDOSPART; dospart++) {
508                 if (ext_sizes[dospart] != 0) {
509                         mbr_extended(dev, info, ssp, ext_offsets[dospart],
510                                      ext_sizes[dospart], base_ext_offset,
511                                      nsectors, ntracks, mbr_offset, ++level);
512                 }
513         }
514
515 done:
516         bp->b_flags |= B_INVAL | B_AGE;
517         relpbuf(bp, NULL);
518 }
519
520 static int
521 mbr_setslice(char *sname, struct disk_info *info, struct diskslice *sp,
522             struct dos_partition *dp, u_int64_t br_offset)
523 {
524         u_int64_t       offset;
525         u_int64_t       size;
526
527         offset = br_offset + dp->dp_start;
528         if (offset > info->d_media_blocks || offset < br_offset) {
529                 kprintf(
530                 "%s: slice starts beyond end of the disk: rejecting it\n",
531                        sname);
532                 return (1);
533         }
534         size = info->d_media_blocks - offset;
535         if (size >= dp->dp_size) {
536                 if (dp->dp_size == 0xFFFFFFFFU) {
537                         kprintf("%s: slice >2TB, using media size instead "
538                                 "of slice table size\n", sname);
539                 } else {
540                         size = dp->dp_size;
541                 }
542         } else {
543                 kprintf("%s: slice extends beyond end of disk: "
544                         "truncating from %u to %llu sectors\n",
545                         sname, dp->dp_size, (unsigned long long)size);
546         }
547         sp->ds_offset = offset;
548         sp->ds_size = size;
549         sp->ds_type = dp->dp_typ;
550         bzero(&sp->ds_type_uuid, sizeof(sp->ds_type_uuid));
551         bzero(&sp->ds_stor_uuid, sizeof(sp->ds_type_uuid));
552
553         /*
554          * Slices do not overlap with the parent (if any).
555          */
556         sp->ds_reserved = 0;
557         return (0);
558 }