Home | History | Annotate | Line # | Download | only in sun
disksubr.c revision 1.1
      1 /*	$NetBSD: disksubr.c,v 1.1 2003/11/15 17:52:31 bouyer Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
      5  * All rights reserved.
      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  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Copyright (c) 1994, 1995 Gordon W. Ross
     33  * Copyright (c) 1994 Theo de Raadt
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *      This product includes software developed by Theo de Raadt.
     47  * 4. The name of the author may not be used to endorse or promote products
     48  *    derived from this software without specific prior written permission
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     53  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     55  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     59  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.1 2003/11/15 17:52:31 bouyer Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/buf.h>
     68 #include <sys/ioccom.h>
     69 #include <sys/device.h>
     70 #include <sys/disklabel.h>
     71 #include <sys/disk.h>
     72 #include <sys/dkbad.h>
     73 
     74 #include <dev/sun/disklabel.h>
     75 
     76 #if LABELSECTOR != 0
     77 #error  "Default value of LABELSECTOR no longer zero?"
     78 #endif
     79 
     80 static	char *disklabel_sun_to_bsd __P((char *, struct disklabel *));
     81 static	int disklabel_bsd_to_sun __P((struct disklabel *, char *));
     82 
     83 /*
     84  * Attempt to read a disk label from a device
     85  * using the indicated strategy routine.
     86  * The label must be partly set up before this:
     87  * secpercyl, secsize and anything required for a block i/o read
     88  * operation in the driver's strategy/start routines
     89  * must be filled in before calling us.
     90  *
     91  * Return buffer for use in signalling errors if requested.
     92  *
     93  * Returns null on success and an error string on failure.
     94  */
     95 const char *
     96 readdisklabel(dev, strat, lp, clp)
     97 	dev_t dev;
     98 	void (*strat) __P((struct buf *));
     99 	struct disklabel *lp;
    100 	struct cpu_disklabel *clp;
    101 {
    102 	struct buf *bp;
    103 	struct disklabel *dlp;
    104 	struct sun_disklabel *slp;
    105 	int error;
    106 
    107 	/* minimal requirements for archtypal disk label */
    108 	if (lp->d_secperunit == 0)
    109 		lp->d_secperunit = 0x1fffffff;
    110 	if (lp->d_npartitions == 0) {
    111 		lp->d_npartitions = RAW_PART + 1;
    112 		if (lp->d_partitions[RAW_PART].p_size == 0)
    113 			lp->d_partitions[RAW_PART].p_size = 0x1fffffff;
    114 		lp->d_partitions[RAW_PART].p_offset = 0;
    115 	}
    116 
    117 	/* obtain buffer to probe drive with */
    118 	bp = geteblk((int)lp->d_secsize);
    119 
    120 	/* next, dig out disk label */
    121 	bp->b_dev = dev;
    122 	bp->b_blkno = LABELSECTOR;
    123 	bp->b_cylinder = 0;
    124 	bp->b_bcount = lp->d_secsize;
    125 	bp->b_flags |= B_READ;
    126 	(*strat)(bp);
    127 
    128 	/* if successful, locate disk label within block and validate */
    129 	error = biowait(bp);
    130 	if (error == 0) {
    131 		/* Save the whole block in case it has info we need. */
    132 		memcpy(clp->cd_block, bp->b_data, sizeof(clp->cd_block));
    133 	}
    134 	brelse(bp);
    135 	if (error)
    136 		return ("disk label read error");
    137 
    138 	/* Check for a NetBSD disk label at LABELOFFSET */
    139 	dlp = (struct disklabel *) (clp->cd_block + LABELOFFSET);
    140 	if (dlp->d_magic == DISKMAGIC) {
    141 		if (dkcksum(dlp))
    142 			return ("NetBSD disk label corrupted");
    143 		*lp = *dlp;
    144 		return (NULL);
    145 	}
    146 
    147 	/* Check for a Sun disk label (for PROM compatibility). */
    148 	slp = (struct sun_disklabel *) clp->cd_block;
    149 	if (slp->sl_magic == SUN_DKMAGIC)
    150 		return (disklabel_sun_to_bsd(clp->cd_block, lp));
    151 
    152 	/*
    153 	 * Check for a NetBSD disk label somewhere in LABELSECTOR
    154 	 * (compat with others big-endian boxes)
    155 	 */
    156 	for (dlp = (struct disklabel *)clp->cd_block;
    157 	    dlp <= (struct disklabel *)((char *)clp->cd_block +
    158 	    DEV_BSIZE - sizeof(*dlp));
    159 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
    160 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
    161 			continue;
    162 		}
    163 		if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0)
    164 			return("NetBSD disk label corrupted");
    165 		else {
    166 			*lp = *dlp;
    167 			return(NULL);
    168 		}
    169 	}
    170 
    171 	memset(clp->cd_block, 0, sizeof(clp->cd_block));
    172 	return ("no disk label");
    173 }
    174 
    175 /*
    176  * Check new disk label for sensibility
    177  * before setting it.
    178  */
    179 int
    180 setdisklabel(olp, nlp, openmask, clp)
    181 	struct disklabel *olp, *nlp;
    182 	u_long openmask;
    183 	struct cpu_disklabel *clp;
    184 {
    185 	int i;
    186 	struct partition *opp, *npp;
    187 
    188 	/* sanity clause */
    189 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0 ||
    190 	    (nlp->d_secsize % DEV_BSIZE) != 0)
    191 		return(EINVAL);
    192 
    193 	/* special case to allow disklabel to be invalidated */
    194 	if (nlp->d_magic == 0xffffffff) {
    195 		*olp = *nlp;
    196 		return (0);
    197 	}
    198 
    199 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
    200 	    dkcksum(nlp) != 0)
    201 		return (EINVAL);
    202 
    203 	while ((i = ffs(openmask)) != 0) {
    204 		i--;
    205 		openmask &= ~(1 << i);
    206 		if (nlp->d_npartitions <= i)
    207 			return (EBUSY);
    208 		opp = &olp->d_partitions[i];
    209 		npp = &nlp->d_partitions[i];
    210 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
    211 			return (EBUSY);
    212 	}
    213 
    214 	*olp = *nlp;
    215 	return (0);
    216 }
    217 
    218 /*
    219  * Write disk label back to device after modification.
    220  * Current label is already in clp->cd_block[]
    221  */
    222 int
    223 writedisklabel(dev, strat, lp, clp)
    224 	dev_t dev;
    225 	void (*strat) __P((struct buf *));
    226 	struct disklabel *lp;
    227 	struct cpu_disklabel *clp;
    228 {
    229 	struct buf *bp;
    230 	int error;
    231 	struct disklabel *dlp;
    232 	struct sun_disklabel *slp;
    233 
    234 	/*
    235 	 * Embed native label in a piece of wasteland.
    236 	 */
    237 	if (sizeof(struct disklabel) > sizeof slp->sl_bsdlabel)
    238 		return EFBIG;
    239 
    240 	slp = (struct sun_disklabel *)clp->cd_block;
    241 	memset(slp->sl_bsdlabel, 0, sizeof(slp->sl_bsdlabel));
    242 	dlp = (struct disklabel *)slp->sl_bsdlabel;
    243 	*dlp = *lp;
    244 
    245 	/* Build a SunOS compatible label around the native label */
    246 	error = disklabel_bsd_to_sun(lp, clp->cd_block);
    247 	if (error)
    248 		return (error);
    249 
    250 	/* Get a buffer and copy the new label into it. */
    251 	bp = geteblk((int)lp->d_secsize);
    252 	memcpy(bp->b_data, clp->cd_block, sizeof(clp->cd_block));
    253 
    254 	/* Write out the updated label. */
    255 	bp->b_dev = dev;
    256 	bp->b_blkno = LABELSECTOR;
    257 	bp->b_cylinder = 0;
    258 	bp->b_bcount = lp->d_secsize;
    259 	bp->b_flags |= B_WRITE;
    260 	(*strat)(bp);
    261 	error = biowait(bp);
    262 	brelse(bp);
    263 
    264 	return (error);
    265 }
    266 
    267 /*
    268  * Determine the size of the transfer, and make sure it is
    269  * within the boundaries of the partition. Adjust transfer
    270  * if needed, and signal errors or early completion.
    271  */
    272 int
    273 bounds_check_with_label(dk, bp, wlabel)
    274 	struct disk *dk;
    275 	struct buf *bp;
    276 	int wlabel;
    277 {
    278 	struct disklabel *lp = dk->dk_label;
    279 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    280 	int maxsz = p->p_size;
    281 	int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
    282 
    283 	/*
    284 	 * overwriting disk label ?
    285 	 * The label is always in sector LABELSECTOR.
    286 	 * XXX should also protect bootstrap in first 8K
    287 	 */
    288 	if (bp->b_blkno + p->p_offset <= LABELSECTOR &&
    289 	    (bp->b_flags & B_READ) == 0 && wlabel == 0) {
    290 		bp->b_error = EROFS;
    291 		goto bad;
    292 	}
    293 
    294 	/* beyond partition? */
    295 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
    296 		/* if exactly at end of disk, return an EOF */
    297 		if (bp->b_blkno == maxsz) {
    298 			bp->b_resid = bp->b_bcount;
    299 			return(0);
    300 		}
    301 		/* or truncate if part of it fits */
    302 		sz = maxsz - bp->b_blkno;
    303 		if (sz <= 0) {
    304 			bp->b_error = EINVAL;
    305 			goto bad;
    306 		}
    307 		bp->b_bcount = sz << DEV_BSHIFT;
    308 	}
    309 
    310 	/* calculate cylinder for disksort to order transfers with */
    311 	bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
    312 	return(1);
    313 bad:
    314 	bp->b_flags |= B_ERROR;
    315 	return(-1);
    316 }
    317 
    318 /************************************************************************
    319  *
    320  * The rest of this was taken from arch/sparc/scsi/sun_disklabel.c
    321  * and then substantially rewritten by Gordon W. Ross
    322  *
    323  ************************************************************************/
    324 
    325 /* What partition types to assume for Sun disklabels: */
    326 static u_char
    327 sun_fstypes[8] = {
    328 	FS_BSDFFS,	/* a */
    329 	FS_SWAP,	/* b */
    330 	FS_OTHER,	/* c - whole disk */
    331 	FS_BSDFFS,	/* d */
    332 	FS_BSDFFS,	/* e */
    333 	FS_BSDFFS,	/* f */
    334 	FS_BSDFFS,	/* g */
    335 	FS_BSDFFS,	/* h */
    336 };
    337 
    338 /*
    339  * Given a SunOS disk label, set lp to a BSD disk label.
    340  * Returns NULL on success, else an error string.
    341  *
    342  * The BSD label is cleared out before this is called.
    343  */
    344 static char *
    345 disklabel_sun_to_bsd(cp, lp)
    346 	char *cp;
    347 	struct disklabel *lp;
    348 {
    349 	struct sun_disklabel *sl;
    350 	struct partition *npp;
    351 	struct sun_dkpart *spp;
    352 	int i, secpercyl;
    353 	u_short cksum, *sp1, *sp2;
    354 
    355 	sl = (struct sun_disklabel *)cp;
    356 
    357 	/* Verify the XOR check. */
    358 	sp1 = (u_short *)sl;
    359 	sp2 = (u_short *)(sl + 1);
    360 	cksum = 0;
    361 	while (sp1 < sp2)
    362 		cksum ^= *sp1++;
    363 	if (cksum != 0)
    364 		return("SunOS disk label, bad checksum");
    365 
    366 	/* Format conversion. */
    367 	lp->d_magic = DISKMAGIC;
    368 	lp->d_magic2 = DISKMAGIC;
    369 	memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
    370 
    371 	lp->d_secsize = 512;
    372 	lp->d_nsectors   = sl->sl_nsectors;
    373 	lp->d_ntracks    = sl->sl_ntracks;
    374 	lp->d_ncylinders = sl->sl_ncylinders;
    375 
    376 	secpercyl = sl->sl_nsectors * sl->sl_ntracks;
    377 	lp->d_secpercyl  = secpercyl;
    378 	lp->d_secperunit = secpercyl * sl->sl_ncylinders;
    379 
    380 	lp->d_sparespercyl = sl->sl_sparespercyl;
    381 	lp->d_acylinders   = sl->sl_acylinders;
    382 	lp->d_rpm          = sl->sl_rpm;
    383 	lp->d_interleave   = sl->sl_interleave;
    384 
    385 	lp->d_npartitions = 8;
    386 	/* These are as defined in <ufs/ffs/fs.h> */
    387 	lp->d_bbsize = 8192;	/* XXX */
    388 	lp->d_sbsize = 8192;	/* XXX */
    389 
    390 	for (i = 0; i < 8; i++) {
    391 		spp = &sl->sl_part[i];
    392 		npp = &lp->d_partitions[i];
    393 		npp->p_offset = spp->sdkp_cyloffset * secpercyl;
    394 		npp->p_size = spp->sdkp_nsectors;
    395 		if (npp->p_size == 0) {
    396 			npp->p_fstype = FS_UNUSED;
    397 		} else {
    398 			npp->p_fstype = sun_fstypes[i];
    399 			if (npp->p_fstype == FS_BSDFFS) {
    400 				/*
    401 				 * The sun label does not store the FFS fields,
    402 				 * so just set them with default values here.
    403 				 */
    404 				npp->p_fsize = 1024;
    405 				npp->p_frag = 8;
    406 				npp->p_cpg = 16;
    407 			}
    408 		}
    409 	}
    410 
    411 	lp->d_checksum = 0;
    412 	lp->d_checksum = dkcksum(lp);
    413 	return (NULL);
    414 }
    415 
    416 /*
    417  * Given a BSD disk label, update the Sun disklabel
    418  * pointed to by cp with the new info.  Note that the
    419  * Sun disklabel may have other info we need to keep.
    420  * Returns zero or error code.
    421  */
    422 static int
    423 disklabel_bsd_to_sun(lp, cp)
    424 	struct disklabel *lp;
    425 	char *cp;
    426 {
    427 	struct sun_disklabel *sl;
    428 	struct partition *npp;
    429 	struct sun_dkpart *spp;
    430 	int i, secpercyl;
    431 	u_short cksum, *sp1, *sp2;
    432 
    433 	if (lp->d_secsize != 512)
    434 		return (EINVAL);
    435 
    436 	sl = (struct sun_disklabel *)cp;
    437 
    438 	/*
    439 	 * Format conversion.
    440 	 */
    441 	memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
    442 	sl->sl_rpm = lp->d_rpm;
    443 	sl->sl_pcylinders   = lp->d_ncylinders + lp->d_acylinders; /* XXX */
    444 	sl->sl_sparespercyl = lp->d_sparespercyl;
    445 	sl->sl_interleave   = lp->d_interleave;
    446 	sl->sl_ncylinders   = lp->d_ncylinders;
    447 	sl->sl_acylinders   = lp->d_acylinders;
    448 	sl->sl_ntracks      = lp->d_ntracks;
    449 	sl->sl_nsectors     = lp->d_nsectors;
    450 
    451 	secpercyl = sl->sl_nsectors * sl->sl_ntracks;
    452 	for (i = 0; i < 8; i++) {
    453 		spp = &sl->sl_part[i];
    454 		npp = &lp->d_partitions[i];
    455 
    456 		/*
    457 		 * SunOS partitions must start on a cylinder boundary.
    458 		 * Note this restriction is forced upon NetBSD/sparc
    459 		 * labels too, since we want to keep both labels
    460 		 * synchronised.
    461 		 */
    462 		if (npp->p_offset % secpercyl)
    463 			return (EINVAL);
    464 		spp->sdkp_cyloffset = npp->p_offset / secpercyl;
    465 		spp->sdkp_nsectors = npp->p_size;
    466 	}
    467 	sl->sl_magic = SUN_DKMAGIC;
    468 
    469 	/* Compute the XOR check. */
    470 	sp1 = (u_short *)sl;
    471 	sp2 = (u_short *)(sl + 1);
    472 	sl->sl_cksum = cksum = 0;
    473 	while (sp1 < sp2)
    474 		cksum ^= *sp1++;
    475 	sl->sl_cksum = cksum;
    476 
    477 	return (0);
    478 }
    479 
    480 /*
    481  * Search the bad sector table looking for the specified sector.
    482  * Return index if found.
    483  * Return -1 if not found.
    484  */
    485 int
    486 isbad(bt, cyl, trk, sec)
    487 	struct dkbad *bt;
    488 	int cyl, trk, sec;
    489 {
    490 	int i;
    491 	long blk, bblk;
    492 
    493 	blk = ((long)cyl << 16) + (trk << 8) + sec;
    494 	for (i = 0; i < 126; i++) {
    495 		bblk = ((long)bt->bt_bad[i].bt_cyl << 16) +
    496 			bt->bt_bad[i].bt_trksec;
    497 		if (blk == bblk)
    498 			return (i);
    499 		if (blk < bblk || bblk < 0)
    500 			break;
    501 	}
    502 	return (-1);
    503 }
    504