kernel - Add callout debugging
[dragonfly.git] / sys / kern / subr_disklabel32.c
1 /*
2  * Copyright (c) 2003-2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * ----------------------------------------------------------------------------
35  * "THE BEER-WARE LICENSE" (Revision 42):
36  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
37  * can do whatever you want with this stuff. If we meet some day, and you think
38  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
39  * ----------------------------------------------------------------------------
40  *
41  * Copyright (c) 1982, 1986, 1988, 1993
42  *      The Regents of the University of California.  All rights reserved.
43  * (c) UNIX System Laboratories, Inc.
44  * All or some portions of this file are derived from material licensed
45  * to the University of California by American Telephone and Telegraph
46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47  * the permission of UNIX System Laboratories, Inc.
48  *
49  * Copyright (c) 1994 Bruce D. Evans.
50  * All rights reserved.
51  *
52  * Copyright (c) 1990 The Regents of the University of California.
53  * All rights reserved.
54  *
55  * This code is derived from software contributed to Berkeley by
56  * William Jolitz.
57  *
58  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
59  * All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  * 2. Redistributions in binary form must reproduce the above copyright
67  *    notice, this list of conditions and the following disclaimer in the
68  *    documentation and/or other materials provided with the distribution.
69  * 3. Neither the name of the University nor the names of its contributors
70  *    may be used to endorse or promote products derived from this software
71  *    without specific prior written permission.
72  *
73  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83  * SUCH DAMAGE.
84  *
85  *      @(#)ufs_disksubr.c      8.5 (Berkeley) 1/21/94
86  * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
87  * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
88  */
89
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/kernel.h>
93 #include <sys/proc.h>
94 #include <sys/sysctl.h>
95 #include <sys/buf.h>
96 #include <sys/conf.h>
97 #include <sys/disklabel.h>
98 #include <sys/disklabel32.h>
99 #include <sys/diskslice.h>
100 #include <sys/disk.h>
101 #include <sys/dtype.h>          /* DTYPE_* constants */
102 #include <machine/md_var.h>
103 #include <sys/ctype.h>
104 #include <sys/syslog.h>
105 #include <sys/device.h>
106 #include <sys/msgport.h>
107 #include <sys/msgport2.h>
108 #include <sys/buf2.h>
109
110 #include <vfs/ufs/dinode.h>     /* XXX used only for fs.h */
111 #include <vfs/ufs/fs.h>         /* XXX used only to get BBSIZE/SBSIZE */
112
113 static void partition_info(const char *sname, int part, struct partition32 *pp);
114 static void slice_info(const char *sname, struct diskslice *sp);
115 static const char *l32_fixlabel(const char *sname, struct diskslice *sp,
116                                 disklabel_t lpx, int writeflag);
117
118 /*
119  * Retrieve the partition start and extent, in blocks.  Return 0 on success,
120  * EINVAL on error.
121  */
122 static int
123 l32_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
124                   u_int64_t *start, u_int64_t *blocks)
125 {
126         struct partition32 *pp;
127
128         if (part >= lp.lab32->d_npartitions)
129                 return (EINVAL);
130         pp = &lp.lab32->d_partitions[part];
131         *start = pp->p_offset;
132         *blocks = pp->p_size;
133         return(0);
134 }
135
136 static void
137 l32_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart)
138 {
139         struct partition32 *pp;
140         const size_t uuid_size = sizeof(struct uuid);
141
142         bzero(&dpart->fstype_uuid, uuid_size);
143         bzero(&dpart->storage_uuid, uuid_size);
144         if (part < lp.lab32->d_npartitions) {
145                 pp = &lp.lab32->d_partitions[part];
146                 dpart->fstype = pp->p_fstype;
147         } else {
148                 dpart->fstype = 0;
149         }
150 }
151
152 static u_int32_t
153 l32_getnumparts(disklabel_t lp)
154 {
155         return(lp.lab32->d_npartitions);
156 }
157
158 static int
159 l32_getpackname(disklabel_t lp, char *buf, size_t bytes)
160 {
161         size_t slen;
162
163         if (lp.lab32->d_packname[0] == 0) {
164                 buf[0] = 0;
165                 return -1;
166         }
167         slen = strnlen(lp.lab32->d_packname, sizeof(lp.lab32->d_packname));
168         if (slen >= bytes)
169                 slen = bytes - 1;
170         bcopy(lp.lab32->d_packname, buf, slen);
171         buf[slen] = 0;
172
173         return 0;
174 }
175
176 static void
177 l32_freedisklabel(disklabel_t *lpp)
178 {
179         kfree((*lpp).lab32, M_DEVBUF);
180         (*lpp).lab32 = NULL;
181 }
182
183 /*
184  * Attempt to read a disk label from a device.  
185  *
186  * Returns NULL on sucess, and an error string on failure
187  */
188 static const char *
189 l32_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
190                 struct disk_info *info)
191 {
192         disklabel_t lpx;
193         struct buf *bp;
194         struct disklabel32 *dlp;
195         const char *msg = NULL;
196         int secsize = info->d_media_blksize;
197
198         bp = getpbuf_mem(NULL);
199         KKASSERT(secsize <= bp->b_bufsize);
200         bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * secsize;
201         bp->b_bio1.bio_done = biodone_sync;
202         bp->b_bio1.bio_flags |= BIO_SYNC;
203         bp->b_bcount = secsize;
204         bp->b_flags &= ~B_INVAL;
205         bp->b_cmd = BUF_CMD_READ;
206         bp->b_flags |= B_FAILONDIS;
207         dev_dstrategy(dev, &bp->b_bio1);
208         if (biowait(&bp->b_bio1, "labrd"))
209                 msg = "I/O error";
210         else for (dlp = (struct disklabel32 *)bp->b_data;
211             dlp <= (struct disklabel32 *)((char *)bp->b_data +
212             secsize - sizeof(*dlp));
213             dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
214                 if (dlp->d_magic != DISKMAGIC32 ||
215                     dlp->d_magic2 != DISKMAGIC32) {
216                         /*
217                          * NOTE! dsreadandsetlabel() does a strcmp() on
218                          * this string.
219                          */
220                         if (msg == NULL) 
221                                 msg = "no disk label";
222                 } else if (dlp->d_npartitions > MAXPARTITIONS32 ||
223                            dkcksum32(dlp) != 0) {
224                         msg = "disk label corrupted";
225                 } else {
226                         lpx.lab32 = dlp;
227                         msg = l32_fixlabel(NULL, sp, lpx, FALSE);
228                         if (msg == NULL) {
229                                 (*lpp).lab32 = kmalloc(sizeof(*dlp),
230                                                        M_DEVBUF, M_WAITOK|M_ZERO);
231                                 *(*lpp).lab32 = *dlp;
232                         }
233                         break;
234                 }
235         }
236         bp->b_flags |= B_INVAL | B_AGE;
237         relpbuf(bp, NULL);
238
239         return (msg);
240 }
241
242 /*
243  * Check new disk label for sensibility before setting it.
244  */
245 static int
246 l32_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
247                  struct diskslice *sp, u_int32_t *openmask)
248 {
249         struct disklabel32 *olp, *nlp;
250         struct partition32 *opp, *npp;
251         int part;
252         int i;
253
254         olp = olpx.lab32;
255         nlp = nlpx.lab32;
256
257         /*
258          * Check it is actually a disklabel we are looking at.
259          */
260         if (nlp->d_magic != DISKMAGIC32 || nlp->d_magic2 != DISKMAGIC32 ||
261             dkcksum32(nlp) != 0)
262                 return (EINVAL);
263
264         /*
265          * For each partition that we think is open, check the new disklabel
266          * for compatibility.  Ignore special partitions (>= 128).
267          */
268         i = 0;
269         while (i < 128) {
270                 if (openmask[i >> 5] == 0) {
271                         i += 32;
272                         continue;
273                 }
274                 if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
275                         ++i;
276                         continue;
277                 }
278                 if (nlp->d_npartitions <= i)
279                         return (EBUSY);
280                 opp = &olp->d_partitions[i];
281                 npp = &nlp->d_partitions[i];
282                 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
283                         return (EBUSY);
284                 /*
285                  * Copy internally-set partition information
286                  * if new label doesn't include it.             XXX
287                  * (If we are using it then we had better stay the same type)
288                  * This is possibly dubious, as someone else noted (XXX)
289                  */
290                 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
291                         npp->p_fstype = opp->p_fstype;
292                         npp->p_fsize = opp->p_fsize;
293                         npp->p_frag = opp->p_frag;
294                         npp->p_cpg = opp->p_cpg;
295                 }
296                 ++i;
297         }
298         nlp->d_checksum = 0;
299         nlp->d_checksum = dkcksum32(nlp);
300         *olp = *nlp;
301
302         if (olp->d_partitions[RAW_PART].p_offset)
303                 return (EXDEV);
304         if (olp->d_secperunit > sp->ds_size)
305                 return (ENOSPC);
306         for (part = 0; part < olp->d_npartitions; ++part) {
307                 if (olp->d_partitions[part].p_size > sp->ds_size)
308                         return(ENOSPC);
309         }
310         return (0);
311 }
312
313 /*
314  * Write disk label back to device after modification.
315  */
316 static int
317 l32_writedisklabel(cdev_t dev, struct diskslices *ssp, struct diskslice *sp,
318                    disklabel_t lpx)
319 {
320         struct disklabel32 *lp;
321         struct disklabel32 *dlp;
322         struct buf *bp;
323         const char *msg;
324         int error = 0;
325
326         lp = lpx.lab32;
327
328         if (lp->d_partitions[RAW_PART].p_offset != 0)
329                 return (EXDEV);                 /* not quite right */
330
331         bp = getpbuf_mem(NULL);
332         KKASSERT((int)lp->d_secsize <= bp->b_bufsize);
333         bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * lp->d_secsize;
334         bp->b_bio1.bio_done = biodone_sync;
335         bp->b_bio1.bio_flags |= BIO_SYNC;
336         bp->b_bcount = lp->d_secsize;
337         bp->b_flags |= B_FAILONDIS;
338
339 #if 1
340         /*
341          * We read the label first to see if it's there,
342          * in which case we will put ours at the same offset into the block..
343          * (I think this is stupid [Julian])
344          * Note that you can't write a label out over a corrupted label!
345          * (also stupid.. how do you write the first one? by raw writes?)
346          */
347         bp->b_flags &= ~B_INVAL;
348         bp->b_cmd = BUF_CMD_READ;
349         KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
350         dev_dstrategy(dev, &bp->b_bio1);
351         error = biowait(&bp->b_bio1, "labrd");
352         if (error)
353                 goto done;
354         for (dlp = (struct disklabel32 *)bp->b_data;
355             dlp <= (struct disklabel32 *)
356               ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
357             dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
358                 if (dlp->d_magic == DISKMAGIC32 &&
359                     dlp->d_magic2 == DISKMAGIC32 && dkcksum32(dlp) == 0) {
360                         *dlp = *lp;
361                         lpx.lab32 = dlp;
362                         msg = l32_fixlabel(NULL, sp, lpx, TRUE);
363                         if (msg) {
364                                 error = EINVAL;
365                         } else {
366                                 bp->b_cmd = BUF_CMD_WRITE;
367                                 bp->b_bio1.bio_done = biodone_sync;
368                                 bp->b_bio1.bio_flags |= BIO_SYNC;
369                                 KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
370                                 dev_dstrategy(dev, &bp->b_bio1);
371                                 error = biowait(&bp->b_bio1, "labwr");
372                         }
373                         goto done;
374                 }
375         }
376         error = ESRCH;
377 done:
378 #else
379         bzero(bp->b_data, lp->d_secsize);
380         dlp = (struct disklabel32 *)bp->b_data;
381         *dlp = *lp;
382         bp->b_flags &= ~B_INVAL;
383         bp->b_cmd = BUF_CMD_WRITE;
384         bp->b_bio1.bio_done = biodone_sync;
385         bp->b_bio1.bio_flags |= BIO_SYNC;
386         BUF_STRATEGY(bp, 1);
387         error = biowait(&bp->b_bio1, "labwr");
388 #endif
389         bp->b_flags |= B_INVAL | B_AGE;
390         relpbuf(bp, NULL);
391
392         return (error);
393 }
394
395 /*
396  * Create a disklabel based on a disk_info structure, initializing
397  * the appropriate fields and creating a raw partition that covers the
398  * whole disk.
399  *
400  * If a diskslice is passed, the label is truncated to the slice
401  */
402 static disklabel_t
403 l32_clone_label(struct disk_info *info, struct diskslice *sp)
404 {
405         struct disklabel32 *lp;
406         disklabel_t res;
407
408         lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
409         lp->d_nsectors = info->d_secpertrack;
410         lp->d_ntracks = info->d_nheads;
411         lp->d_secpercyl = info->d_secpercyl;
412         lp->d_secsize = info->d_media_blksize;
413
414         if (sp)
415                 lp->d_secperunit = (u_int)sp->ds_size;
416         else
417                 lp->d_secperunit = (u_int)info->d_media_blocks;
418
419         if (lp->d_typename[0] == '\0')
420                 strncpy(lp->d_typename, "amnesiac", sizeof(lp->d_typename));
421         if (lp->d_packname[0] == '\0')
422                 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
423         if (lp->d_nsectors == 0)
424                 lp->d_nsectors = 32;
425         if (lp->d_ntracks == 0)
426                 lp->d_ntracks = 64;
427         lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
428         lp->d_ncylinders = lp->d_secperunit / lp->d_secpercyl;
429         if (lp->d_rpm == 0)
430                 lp->d_rpm = 3600;
431         if (lp->d_interleave == 0)
432                 lp->d_interleave = 1;
433         if (lp->d_npartitions < RAW_PART + 1)
434                 lp->d_npartitions = MAXPARTITIONS32;
435         if (lp->d_bbsize == 0)
436                 lp->d_bbsize = BBSIZE;
437         if (lp->d_sbsize == 0)
438                 lp->d_sbsize = SBSIZE;
439
440         /*
441          * Used by various devices to create a compatibility slice which
442          * allows us to mount root from devices which do not have a
443          * disklabel.  Particularly: CDs.
444          */
445         lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
446         if (info->d_dsflags & DSO_COMPATPARTA) {
447                 lp->d_partitions[0].p_size = lp->d_secperunit;
448                 lp->d_partitions[0].p_fstype = FS_OTHER;
449         }
450         lp->d_magic = DISKMAGIC32;
451         lp->d_magic2 = DISKMAGIC32;
452         lp->d_checksum = dkcksum32(lp);
453         res.lab32 = lp;
454         return (res);
455 }
456
457 static void
458 l32_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
459                     struct diskslice *sp, struct disk_info *info)
460 {
461         struct disklabel32 *lp = lpx.lab32;
462         struct partition32 *pp;
463         disklabel_t template;
464
465         template = l32_clone_label(info, NULL);
466         bcopy(template.opaque, lp, sizeof(struct disklabel32));
467
468         lp->d_magic = DISKMAGIC32;
469         lp->d_magic2 = DISKMAGIC32;
470
471         lp->d_npartitions = MAXPARTITIONS32;
472         if (lp->d_interleave == 0)
473                 lp->d_interleave = 1;
474         if (lp->d_rpm == 0)
475                 lp->d_rpm = 3600;
476         if (lp->d_nsectors == 0)        /* sectors per track */
477                 lp->d_nsectors = 32;
478         if (lp->d_ntracks == 0)         /* heads */
479                 lp->d_ntracks = 64;
480         lp->d_ncylinders = 0;
481         lp->d_bbsize = BBSIZE;
482         lp->d_sbsize = SBSIZE;
483
484         /*
485          * If the slice or GPT partition is really small we could
486          * wind up with an absurd calculation for ncylinders.
487          */
488         while (lp->d_ncylinders < 4) {
489                 if (lp->d_ntracks > 1)
490                         lp->d_ntracks >>= 1;
491                 else if (lp->d_nsectors > 1)
492                         lp->d_nsectors >>= 1;
493                 else
494                         break;
495                 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
496                 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
497         }
498
499         /*
500          * Set or Modify the partition sizes to accomodate the slice,
501          * since we started with a copy of the virgin label stored
502          * in the whole-disk-slice and we are probably not a
503          * whole-disk slice.
504          */
505         lp->d_secperunit = sp->ds_size;
506         pp = &lp->d_partitions[RAW_PART];
507         pp->p_offset = 0;
508         pp->p_size = lp->d_secperunit;
509         if (info->d_dsflags & DSO_COMPATPARTA) {
510                 pp = &lp->d_partitions[0];
511                 pp->p_offset = 0;
512                 pp->p_size = lp->d_secperunit;
513                 pp->p_fstype = FS_OTHER;
514         }
515         lp->d_checksum = 0;
516         lp->d_checksum = dkcksum32(lp);
517
518         kfree(template.opaque, M_DEVBUF);
519 }
520
521 static const char *
522 l32_fixlabel(const char *sname, struct diskslice *sp,
523              disklabel_t lpx, int writeflag)
524 {
525         struct disklabel32 *lp;
526         struct partition32 *pp;
527         u_int64_t start;
528         u_int64_t end;
529         u_int64_t offset;
530         int part;
531         int warned;
532
533         lp = lpx.lab32;
534
535         /* These errors "can't happen" so don't bother reporting details. */
536         if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32)
537                 return ("fixlabel: invalid magic");
538         if (dkcksum32(lp) != 0)
539                 return ("fixlabel: invalid checksum");
540
541         pp = &lp->d_partitions[RAW_PART];
542
543         /*
544          * What a mess.  For ages old backwards compatibility the disklabel
545          * on-disk stores absolute offsets instead of slice-relative offsets.
546          * So fix it up when reading, writing, or snooping.
547          *
548          * The in-core label is always slice-relative.
549          */
550         if (writeflag) {
551                 start = 0;
552                 offset = sp->ds_offset;
553         } else {
554                 start = sp->ds_offset;
555                 offset = -sp->ds_offset;
556         }
557         if (pp->p_offset != start) {
558                 if (sname != NULL) {
559                         kprintf(
560 "%s: rejecting BSD label: raw partition offset != slice offset\n",
561                                sname);
562                         slice_info(sname, sp);
563                         partition_info(sname, RAW_PART, pp);
564                 }
565                 return ("fixlabel: raw partition offset != slice offset");
566         }
567         if (pp->p_size != sp->ds_size) {
568                 if (sname != NULL) {
569                         kprintf("%s: raw partition size != slice size\n", sname);
570                         slice_info(sname, sp);
571                         partition_info(sname, RAW_PART, pp);
572                 }
573                 if (pp->p_size > sp->ds_size) {
574                         if (sname == NULL)
575                                 return ("fixlabel: raw partition size > slice size");
576                         kprintf("%s: truncating raw partition\n", sname);
577                         pp->p_size = sp->ds_size;
578                 }
579         }
580         end = start + sp->ds_size;
581         if (start > end)
582                 return ("fixlabel: slice wraps");
583         if (lp->d_secpercyl <= 0)
584                 return ("fixlabel: d_secpercyl <= 0");
585         pp -= RAW_PART;
586         warned = FALSE;
587         for (part = 0; part < lp->d_npartitions; part++, pp++) {
588                 if (pp->p_offset != 0 || pp->p_size != 0) {
589                         if (pp->p_offset < start
590                             || pp->p_offset + pp->p_size > end
591                             || pp->p_offset + pp->p_size < pp->p_offset) {
592                                 if (sname != NULL) {
593                                         kprintf(
594 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
595                                                sname);
596                                         if (!warned) {
597                                                 slice_info(sname, sp);
598                                                 warned = TRUE;
599                                         }
600                                         partition_info(sname, part, pp);
601                                 }
602                                 /* XXX else silently discard junk. */
603                                 bzero(pp, sizeof *pp);
604                         } else {
605                                 pp->p_offset += offset;
606                         }
607                 }
608         }
609         lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
610         lp->d_secperunit = sp->ds_size;
611         lp->d_checksum = 0;
612         lp->d_checksum = dkcksum32(lp);
613         return (NULL);
614 }
615
616 /*
617  * Set the number of blocks at the beginning of the slice which have
618  * been reserved for label operations.  This area will be write-protected
619  * when accessed via the slice.
620  */
621 static void
622 l32_adjust_label_reserved(struct diskslices *ssp, int slice,
623                           struct diskslice *sp)
624 {
625         /*struct disklabel32 *lp = sp->ds_label.lab32;*/
626         sp->ds_reserved = SBSIZE / ssp->dss_secsize;
627 }
628
629 static void
630 partition_info(const char *sname, int part, struct partition32 *pp)
631 {
632         kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
633                 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
634                 (u_long)pp->p_size);
635 }
636
637 static void
638 slice_info(const char *sname, struct diskslice *sp)
639 {
640         kprintf("%s: start %llu, end %llu, size %llu\n", sname,
641                (long long)sp->ds_offset,
642                (long long)sp->ds_offset + sp->ds_size - 1,
643                (long long)sp->ds_size);
644 }
645
646 struct disklabel_ops disklabel32_ops = {
647         .labelsize = sizeof(struct disklabel32),
648         .op_readdisklabel = l32_readdisklabel,
649         .op_setdisklabel = l32_setdisklabel,
650         .op_writedisklabel = l32_writedisklabel,
651         .op_clone_label = l32_clone_label,
652         .op_adjust_label_reserved = l32_adjust_label_reserved,
653         .op_getpartbounds = l32_getpartbounds,
654         .op_loadpartinfo = l32_loadpartinfo,
655         .op_getnumparts = l32_getnumparts,
656         .op_makevirginlabel = l32_makevirginlabel,
657         .op_getpackname = l32_getpackname,
658         .op_freedisklabel = l32_freedisklabel
659 };
660