The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_disklabel32.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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 void
  159 l32_freedisklabel(disklabel_t *lpp)
  160 {
  161         kfree((*lpp).lab32, M_DEVBUF);
  162         (*lpp).lab32 = NULL;
  163 }
  164 
  165 /*
  166  * Attempt to read a disk label from a device.  
  167  *
  168  * Returns NULL on sucess, and an error string on failure
  169  */
  170 static const char *
  171 l32_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
  172                 struct disk_info *info)
  173 {
  174         disklabel_t lpx;
  175         struct buf *bp;
  176         struct disklabel32 *dlp;
  177         const char *msg = NULL;
  178         int secsize = info->d_media_blksize;
  179 
  180         bp = geteblk(secsize);
  181         bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * secsize;
  182         bp->b_bio1.bio_done = biodone_sync;
  183         bp->b_bio1.bio_flags |= BIO_SYNC;
  184         bp->b_bcount = secsize;
  185         bp->b_flags &= ~B_INVAL;
  186         bp->b_cmd = BUF_CMD_READ;
  187         dev_dstrategy(dev, &bp->b_bio1);
  188         if (biowait(&bp->b_bio1, "labrd"))
  189                 msg = "I/O error";
  190         else for (dlp = (struct disklabel32 *)bp->b_data;
  191             dlp <= (struct disklabel32 *)((char *)bp->b_data +
  192             secsize - sizeof(*dlp));
  193             dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
  194                 if (dlp->d_magic != DISKMAGIC32 ||
  195                     dlp->d_magic2 != DISKMAGIC32) {
  196                         /*
  197                          * NOTE! dsreadandsetlabel() does a strcmp() on
  198                          * this string.
  199                          */
  200                         if (msg == NULL) 
  201                                 msg = "no disk label";
  202                 } else if (dlp->d_npartitions > MAXPARTITIONS32 ||
  203                            dkcksum32(dlp) != 0) {
  204                         msg = "disk label corrupted";
  205                 } else {
  206                         lpx.lab32 = dlp;
  207                         msg = l32_fixlabel(NULL, sp, lpx, FALSE);
  208                         if (msg == NULL) {
  209                                 (*lpp).lab32 = kmalloc(sizeof(*dlp),
  210                                                        M_DEVBUF, M_WAITOK|M_ZERO);
  211                                 *(*lpp).lab32 = *dlp;
  212                         }
  213                         break;
  214                 }
  215         }
  216         bp->b_flags |= B_INVAL | B_AGE;
  217         brelse(bp);
  218         return (msg);
  219 }
  220 
  221 /*
  222  * Check new disk label for sensibility before setting it.
  223  */
  224 static int
  225 l32_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
  226                  struct diskslice *sp, u_int32_t *openmask)
  227 {
  228         struct disklabel32 *olp, *nlp;
  229         struct partition32 *opp, *npp;
  230         int part;
  231         int i;
  232 
  233         olp = olpx.lab32;
  234         nlp = nlpx.lab32;
  235 
  236         /*
  237          * Check it is actually a disklabel we are looking at.
  238          */
  239         if (nlp->d_magic != DISKMAGIC32 || nlp->d_magic2 != DISKMAGIC32 ||
  240             dkcksum32(nlp) != 0)
  241                 return (EINVAL);
  242 
  243         /*
  244          * For each partition that we think is open, check the new disklabel
  245          * for compatibility.  Ignore special partitions (>= 128).
  246          */
  247         i = 0;
  248         while (i < 128) {
  249                 if (openmask[i >> 5] == 0) {
  250                         i += 32;
  251                         continue;
  252                 }
  253                 if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
  254                         ++i;
  255                         continue;
  256                 }
  257                 if (nlp->d_npartitions <= i)
  258                         return (EBUSY);
  259                 opp = &olp->d_partitions[i];
  260                 npp = &nlp->d_partitions[i];
  261                 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
  262                         return (EBUSY);
  263                 /*
  264                  * Copy internally-set partition information
  265                  * if new label doesn't include it.             XXX
  266                  * (If we are using it then we had better stay the same type)
  267                  * This is possibly dubious, as someone else noted (XXX)
  268                  */
  269                 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
  270                         npp->p_fstype = opp->p_fstype;
  271                         npp->p_fsize = opp->p_fsize;
  272                         npp->p_frag = opp->p_frag;
  273                         npp->p_cpg = opp->p_cpg;
  274                 }
  275                 ++i;
  276         }
  277         nlp->d_checksum = 0;
  278         nlp->d_checksum = dkcksum32(nlp);
  279         *olp = *nlp;
  280 
  281         if (olp->d_partitions[RAW_PART].p_offset)
  282                 return (EXDEV);
  283         if (olp->d_secperunit > sp->ds_size)
  284                 return (ENOSPC);
  285         for (part = 0; part < olp->d_npartitions; ++part) {
  286                 if (olp->d_partitions[part].p_size > sp->ds_size)
  287                         return(ENOSPC);
  288         }
  289         return (0);
  290 }
  291 
  292 /*
  293  * Write disk label back to device after modification.
  294  */
  295 static int
  296 l32_writedisklabel(cdev_t dev, struct diskslices *ssp, struct diskslice *sp,
  297                    disklabel_t lpx)
  298 {
  299         struct disklabel32 *lp;
  300         struct disklabel32 *dlp;
  301         struct buf *bp;
  302         const char *msg;
  303         int error = 0;
  304 
  305         lp = lpx.lab32;
  306 
  307         if (lp->d_partitions[RAW_PART].p_offset != 0)
  308                 return (EXDEV);                 /* not quite right */
  309 
  310         bp = geteblk((int)lp->d_secsize);
  311         bp->b_bio1.bio_offset = (off_t)LABELSECTOR32 * lp->d_secsize;
  312         bp->b_bio1.bio_done = biodone_sync;
  313         bp->b_bio1.bio_flags |= BIO_SYNC;
  314         bp->b_bcount = lp->d_secsize;
  315 
  316 #if 1
  317         /*
  318          * We read the label first to see if it's there,
  319          * in which case we will put ours at the same offset into the block..
  320          * (I think this is stupid [Julian])
  321          * Note that you can't write a label out over a corrupted label!
  322          * (also stupid.. how do you write the first one? by raw writes?)
  323          */
  324         bp->b_flags &= ~B_INVAL;
  325         bp->b_cmd = BUF_CMD_READ;
  326         KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
  327         dev_dstrategy(dev, &bp->b_bio1);
  328         error = biowait(&bp->b_bio1, "labrd");
  329         if (error)
  330                 goto done;
  331         for (dlp = (struct disklabel32 *)bp->b_data;
  332             dlp <= (struct disklabel32 *)
  333               ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
  334             dlp = (struct disklabel32 *)((char *)dlp + sizeof(long))) {
  335                 if (dlp->d_magic == DISKMAGIC32 &&
  336                     dlp->d_magic2 == DISKMAGIC32 && dkcksum32(dlp) == 0) {
  337                         *dlp = *lp;
  338                         lpx.lab32 = dlp;
  339                         msg = l32_fixlabel(NULL, sp, lpx, TRUE);
  340                         if (msg) {
  341                                 error = EINVAL;
  342                         } else {
  343                                 bp->b_cmd = BUF_CMD_WRITE;
  344                                 bp->b_bio1.bio_done = biodone_sync;
  345                                 bp->b_bio1.bio_flags |= BIO_SYNC;
  346                                 KKASSERT(dkpart(dev) == WHOLE_SLICE_PART);
  347                                 dev_dstrategy(dev, &bp->b_bio1);
  348                                 error = biowait(&bp->b_bio1, "labwr");
  349                         }
  350                         goto done;
  351                 }
  352         }
  353         error = ESRCH;
  354 done:
  355 #else
  356         bzero(bp->b_data, lp->d_secsize);
  357         dlp = (struct disklabel32 *)bp->b_data;
  358         *dlp = *lp;
  359         bp->b_flags &= ~B_INVAL;
  360         bp->b_cmd = BUF_CMD_WRITE;
  361         bp->b_bio1.bio_done = biodone_sync;
  362         bp->b_bio1.bio_flags |= BIO_SYNC;
  363         BUF_STRATEGY(bp, 1);
  364         error = biowait(&bp->b_bio1, "labwr");
  365 #endif
  366         bp->b_flags |= B_INVAL | B_AGE;
  367         brelse(bp);
  368         return (error);
  369 }
  370 
  371 /*
  372  * Create a disklabel based on a disk_info structure, initializing
  373  * the appropriate fields and creating a raw partition that covers the
  374  * whole disk.
  375  *
  376  * If a diskslice is passed, the label is truncated to the slice
  377  */
  378 static disklabel_t
  379 l32_clone_label(struct disk_info *info, struct diskslice *sp)
  380 {
  381         struct disklabel32 *lp;
  382         disklabel_t res;
  383 
  384         lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
  385         lp->d_nsectors = info->d_secpertrack;
  386         lp->d_ntracks = info->d_nheads;
  387         lp->d_secpercyl = info->d_secpercyl;
  388         lp->d_secsize = info->d_media_blksize;
  389 
  390         if (sp)
  391                 lp->d_secperunit = (u_int)sp->ds_size;
  392         else
  393                 lp->d_secperunit = (u_int)info->d_media_blocks;
  394 
  395         if (lp->d_typename[0] == '\0')
  396                 strncpy(lp->d_typename, "amnesiac", sizeof(lp->d_typename));
  397         if (lp->d_packname[0] == '\0')
  398                 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
  399         if (lp->d_nsectors == 0)
  400                 lp->d_nsectors = 32;
  401         if (lp->d_ntracks == 0)
  402                 lp->d_ntracks = 64;
  403         lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
  404         lp->d_ncylinders = lp->d_secperunit / lp->d_secpercyl;
  405         if (lp->d_rpm == 0)
  406                 lp->d_rpm = 3600;
  407         if (lp->d_interleave == 0)
  408                 lp->d_interleave = 1;
  409         if (lp->d_npartitions < RAW_PART + 1)
  410                 lp->d_npartitions = MAXPARTITIONS32;
  411         if (lp->d_bbsize == 0)
  412                 lp->d_bbsize = BBSIZE;
  413         if (lp->d_sbsize == 0)
  414                 lp->d_sbsize = SBSIZE;
  415 
  416         /*
  417          * Used by various devices to create a compatibility slice which
  418          * allows us to mount root from devices which do not have a
  419          * disklabel.  Particularly: CDs.
  420          */
  421         lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
  422         if (info->d_dsflags & DSO_COMPATPARTA) {
  423                 lp->d_partitions[0].p_size = lp->d_secperunit;
  424                 lp->d_partitions[0].p_fstype = FS_OTHER;
  425         }
  426         lp->d_magic = DISKMAGIC32;
  427         lp->d_magic2 = DISKMAGIC32;
  428         lp->d_checksum = dkcksum32(lp);
  429         res.lab32 = lp;
  430         return (res);
  431 }
  432 
  433 static void
  434 l32_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
  435                     struct diskslice *sp, struct disk_info *info)
  436 {
  437         struct disklabel32 *lp = lpx.lab32;
  438         struct partition32 *pp;
  439         disklabel_t template;
  440 
  441         template = l32_clone_label(info, NULL);
  442         bcopy(template.opaque, lp, sizeof(struct disklabel32));
  443 
  444         lp->d_magic = DISKMAGIC32;
  445         lp->d_magic2 = DISKMAGIC32;
  446 
  447         lp->d_npartitions = MAXPARTITIONS32;
  448         if (lp->d_interleave == 0)
  449                 lp->d_interleave = 1;
  450         if (lp->d_rpm == 0)
  451                 lp->d_rpm = 3600;
  452         if (lp->d_nsectors == 0)        /* sectors per track */
  453                 lp->d_nsectors = 32;
  454         if (lp->d_ntracks == 0)         /* heads */
  455                 lp->d_ntracks = 64;
  456         lp->d_ncylinders = 0;
  457         lp->d_bbsize = BBSIZE;
  458         lp->d_sbsize = SBSIZE;
  459 
  460         /*
  461          * If the slice or GPT partition is really small we could
  462          * wind up with an absurd calculation for ncylinders.
  463          */
  464         while (lp->d_ncylinders < 4) {
  465                 if (lp->d_ntracks > 1)
  466                         lp->d_ntracks >>= 1;
  467                 else if (lp->d_nsectors > 1)
  468                         lp->d_nsectors >>= 1;
  469                 else
  470                         break;
  471                 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
  472                 lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
  473         }
  474 
  475         /*
  476          * Set or Modify the partition sizes to accomodate the slice,
  477          * since we started with a copy of the virgin label stored
  478          * in the whole-disk-slice and we are probably not a
  479          * whole-disk slice.
  480          */
  481         lp->d_secperunit = sp->ds_size;
  482         pp = &lp->d_partitions[RAW_PART];
  483         pp->p_offset = 0;
  484         pp->p_size = lp->d_secperunit;
  485         if (info->d_dsflags & DSO_COMPATPARTA) {
  486                 pp = &lp->d_partitions[0];
  487                 pp->p_offset = 0;
  488                 pp->p_size = lp->d_secperunit;
  489                 pp->p_fstype = FS_OTHER;
  490         }
  491         lp->d_checksum = 0;
  492         lp->d_checksum = dkcksum32(lp);
  493 
  494         kfree(template.opaque, M_DEVBUF);
  495 }
  496 
  497 static const char *
  498 l32_fixlabel(const char *sname, struct diskslice *sp,
  499              disklabel_t lpx, int writeflag)
  500 {
  501         struct disklabel32 *lp;
  502         struct partition32 *pp;
  503         u_int64_t start;
  504         u_int64_t end;
  505         u_int64_t offset;
  506         int part;
  507         int warned;
  508 
  509         lp = lpx.lab32;
  510 
  511         /* These errors "can't happen" so don't bother reporting details. */
  512         if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32)
  513                 return ("fixlabel: invalid magic");
  514         if (dkcksum32(lp) != 0)
  515                 return ("fixlabel: invalid checksum");
  516 
  517         pp = &lp->d_partitions[RAW_PART];
  518 
  519         /*
  520          * What a mess.  For ages old backwards compatibility the disklabel
  521          * on-disk stores absolute offsets instead of slice-relative offsets.
  522          * So fix it up when reading, writing, or snooping.
  523          *
  524          * The in-core label is always slice-relative.
  525          */
  526         if (writeflag) {
  527                 start = 0;
  528                 offset = sp->ds_offset;
  529         } else {
  530                 start = sp->ds_offset;
  531                 offset = -sp->ds_offset;
  532         }
  533         if (pp->p_offset != start) {
  534                 if (sname != NULL) {
  535                         kprintf(
  536 "%s: rejecting BSD label: raw partition offset != slice offset\n",
  537                                sname);
  538                         slice_info(sname, sp);
  539                         partition_info(sname, RAW_PART, pp);
  540                 }
  541                 return ("fixlabel: raw partition offset != slice offset");
  542         }
  543         if (pp->p_size != sp->ds_size) {
  544                 if (sname != NULL) {
  545                         kprintf("%s: raw partition size != slice size\n", sname);
  546                         slice_info(sname, sp);
  547                         partition_info(sname, RAW_PART, pp);
  548                 }
  549                 if (pp->p_size > sp->ds_size) {
  550                         if (sname == NULL)
  551                                 return ("fixlabel: raw partition size > slice size");
  552                         kprintf("%s: truncating raw partition\n", sname);
  553                         pp->p_size = sp->ds_size;
  554                 }
  555         }
  556         end = start + sp->ds_size;
  557         if (start > end)
  558                 return ("fixlabel: slice wraps");
  559         if (lp->d_secpercyl <= 0)
  560                 return ("fixlabel: d_secpercyl <= 0");
  561         pp -= RAW_PART;
  562         warned = FALSE;
  563         for (part = 0; part < lp->d_npartitions; part++, pp++) {
  564                 if (pp->p_offset != 0 || pp->p_size != 0) {
  565                         if (pp->p_offset < start
  566                             || pp->p_offset + pp->p_size > end
  567                             || pp->p_offset + pp->p_size < pp->p_offset) {
  568                                 if (sname != NULL) {
  569                                         kprintf(
  570 "%s: rejecting partition in BSD label: it isn't entirely within the slice\n",
  571                                                sname);
  572                                         if (!warned) {
  573                                                 slice_info(sname, sp);
  574                                                 warned = TRUE;
  575                                         }
  576                                         partition_info(sname, part, pp);
  577                                 }
  578                                 /* XXX else silently discard junk. */
  579                                 bzero(pp, sizeof *pp);
  580                         } else {
  581                                 pp->p_offset += offset;
  582                         }
  583                 }
  584         }
  585         lp->d_ncylinders = sp->ds_size / lp->d_secpercyl;
  586         lp->d_secperunit = sp->ds_size;
  587         lp->d_checksum = 0;
  588         lp->d_checksum = dkcksum32(lp);
  589         return (NULL);
  590 }
  591 
  592 /*
  593  * Set the number of blocks at the beginning of the slice which have
  594  * been reserved for label operations.  This area will be write-protected
  595  * when accessed via the slice.
  596  */
  597 static void
  598 l32_adjust_label_reserved(struct diskslices *ssp, int slice,
  599                           struct diskslice *sp)
  600 {
  601         /*struct disklabel32 *lp = sp->ds_label.lab32;*/
  602         sp->ds_reserved = SBSIZE / ssp->dss_secsize;
  603 }
  604 
  605 static void
  606 partition_info(const char *sname, int part, struct partition32 *pp)
  607 {
  608         kprintf("%s%c: start %lu, end %lu, size %lu\n", sname, 'a' + part,
  609                 (u_long)pp->p_offset, (u_long)(pp->p_offset + pp->p_size - 1),
  610                 (u_long)pp->p_size);
  611 }
  612 
  613 static void
  614 slice_info(const char *sname, struct diskslice *sp)
  615 {
  616         kprintf("%s: start %llu, end %llu, size %llu\n", sname,
  617                (long long)sp->ds_offset,
  618                (long long)sp->ds_offset + sp->ds_size - 1,
  619                (long long)sp->ds_size);
  620 }
  621 
  622 struct disklabel_ops disklabel32_ops = {
  623         .labelsize = sizeof(struct disklabel32),
  624         .op_readdisklabel = l32_readdisklabel,
  625         .op_setdisklabel = l32_setdisklabel,
  626         .op_writedisklabel = l32_writedisklabel,
  627         .op_clone_label = l32_clone_label,
  628         .op_adjust_label_reserved = l32_adjust_label_reserved,
  629         .op_getpartbounds = l32_getpartbounds,
  630         .op_loadpartinfo = l32_loadpartinfo,
  631         .op_getnumparts = l32_getnumparts,
  632         .op_makevirginlabel = l32_makevirginlabel,
  633         .op_freedisklabel = l32_freedisklabel
  634 };
  635 

Cache object: a8b7d494aa15fcd97dc4ba9591652f53


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.