disksubr.c revision 1.54
11.54Smatt/*	$NetBSD: disksubr.c,v 1.54 2011/02/20 07:50:25 matt Exp $	*/
21.3Scgd
31.1Sderaadt/*
41.1Sderaadt * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
51.1Sderaadt * All rights reserved.
61.1Sderaadt *
71.1Sderaadt * Redistribution and use in source and binary forms, with or without
81.1Sderaadt * modification, are permitted provided that the following conditions
91.1Sderaadt * are met:
101.1Sderaadt * 1. Redistributions of source code must retain the above copyright
111.1Sderaadt *    notice, this list of conditions and the following disclaimer.
121.1Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
131.1Sderaadt *    notice, this list of conditions and the following disclaimer in the
141.1Sderaadt *    documentation and/or other materials provided with the distribution.
151.41Sagc * 3. Neither the name of the University nor the names of its contributors
161.1Sderaadt *    may be used to endorse or promote products derived from this software
171.1Sderaadt *    without specific prior written permission.
181.1Sderaadt *
191.1Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201.1Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211.1Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221.1Sderaadt * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231.1Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241.1Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251.1Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261.1Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271.1Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281.1Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291.1Sderaadt * SUCH DAMAGE.
301.1Sderaadt *
311.3Scgd *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
321.1Sderaadt */
331.40Slukem
341.40Slukem#include <sys/cdefs.h>
351.54Smatt__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.54 2011/02/20 07:50:25 matt Exp $");
361.54Smatt
371.54Smatt#include "opt_compat_ultrix.h"
381.1Sderaadt
391.8Smycroft#include <sys/param.h>
401.8Smycroft#include <sys/systm.h>
411.8Smycroft#include <sys/buf.h>
421.9Sjonathan#include <sys/disk.h>
431.8Smycroft#include <sys/disklabel.h>
441.1Sderaadt
451.33Smatt#include <dev/dec/dec_boot.h>
461.28Smhitch#include <ufs/ufs/dinode.h>		/* XXX for fs.h */
471.28Smhitch#include <ufs/ffs/fs.h>			/* XXX for BBSIZE & SBSIZE */
481.5Sjonathan
491.50Sdslconst char *compat_label(dev_t dev, void (*strat)(struct buf *bp),
501.50Sdsl	struct disklabel *lp, struct cpu_disklabel *osdep);	/* XXX */
511.5Sjonathan
521.1Sderaadt/*
531.1Sderaadt * Attempt to read a disk label from a device
541.35Swiz * using the indicated strategy routine.
551.1Sderaadt * The label must be partly set up before this:
561.1Sderaadt * secpercyl and anything required in the strategy routine
571.1Sderaadt * (e.g., sector size) must be filled in before calling us.
581.1Sderaadt * Returns null on success and an error string on failure.
591.1Sderaadt */
601.38Sdslconst char *
611.53Stsutsuireaddisklabel(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp,
621.53Stsutsui    struct cpu_disklabel *osdep)
631.1Sderaadt{
641.20Ssimonb	struct buf *bp;
651.1Sderaadt	struct disklabel *dlp;
661.42Sdrochner	const char *msg = NULL;
671.1Sderaadt
681.1Sderaadt	if (lp->d_secperunit == 0)
691.1Sderaadt		lp->d_secperunit = 0x1fffffff;
701.1Sderaadt	lp->d_npartitions = 1;
711.1Sderaadt	if (lp->d_partitions[0].p_size == 0)
721.1Sderaadt		lp->d_partitions[0].p_size = 0x1fffffff;
731.1Sderaadt	lp->d_partitions[0].p_offset = 0;
741.1Sderaadt
751.1Sderaadt	bp = geteblk((int)lp->d_secsize);
761.1Sderaadt	bp->b_dev = dev;
771.1Sderaadt	bp->b_blkno = LABELSECTOR;
781.1Sderaadt	bp->b_bcount = lp->d_secsize;
791.34Schs	bp->b_flags |= B_READ;
801.26Sthorpej	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
811.1Sderaadt	(*strat)(bp);
821.1Sderaadt	if (biowait(bp)) {
831.1Sderaadt		msg = "I/O error";
841.32Sthorpej	} else for (dlp = (struct disklabel *)bp->b_data;
851.45Ssimonb	    dlp <= (struct disklabel *)
861.45Ssimonb	    ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp));
871.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
881.1Sderaadt		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
891.1Sderaadt			if (msg == NULL)
901.1Sderaadt				msg = "no disk label";
911.1Sderaadt		} else if (dlp->d_npartitions > MAXPARTITIONS ||
921.1Sderaadt			   dkcksum(dlp) != 0)
931.1Sderaadt			msg = "disk label corrupted";
941.1Sderaadt		else {
951.1Sderaadt			*lp = *dlp;
961.1Sderaadt			msg = NULL;
971.1Sderaadt			break;
981.1Sderaadt		}
991.1Sderaadt	}
1001.46Sad	brelse(bp, 0);
1011.28Smhitch	/*
1021.28Smhitch	 * If no NetBSD label was found, check for an Ultrix label and
1031.28Smhitch	 * construct tne incore label from the Ultrix partition information.
1041.28Smhitch	 */
1051.28Smhitch	if (msg != NULL) {
1061.28Smhitch		msg = compat_label(dev, strat, lp, osdep);
1071.28Smhitch		if (msg == NULL) {
1081.28Smhitch			printf("WARNING: using Ultrix partition information\n");
1091.28Smhitch			/* set geometry? */
1101.28Smhitch		}
1111.28Smhitch	}
1121.28Smhitch/* XXX If no NetBSD label or Ultrix label found, generate default label here */
1131.53Stsutsui	return msg;
1141.5Sjonathan}
1151.5Sjonathan
1161.5Sjonathan/*
1171.5Sjonathan * Given a buffer bp, try and interpret it as an Ultrix disk label,
1181.5Sjonathan * putting the partition info into a native NetBSD label
1191.5Sjonathan */
1201.42Sdrochnerconst char *
1211.53Stsutsuicompat_label(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp,
1221.53Stsutsui    struct cpu_disklabel *osdep)
1231.5Sjonathan{
1241.21Ssimonb	dec_disklabel *dlp;
1251.5Sjonathan	struct buf *bp = NULL;
1261.42Sdrochner	const char *msg = NULL;
1271.5Sjonathan
1281.5Sjonathan	bp = geteblk((int)lp->d_secsize);
1291.5Sjonathan	bp->b_dev = dev;
1301.5Sjonathan	bp->b_blkno = DEC_LABEL_SECTOR;
1311.5Sjonathan	bp->b_bcount = lp->d_secsize;
1321.34Schs	bp->b_flags |= B_READ;
1331.26Sthorpej	bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl;
1341.5Sjonathan	(*strat)(bp);
1351.5Sjonathan
1361.5Sjonathan	if (biowait(bp)) {
1371.4Sdean                msg = "I/O error";
1381.5Sjonathan		goto done;
1391.5Sjonathan	}
1401.5Sjonathan
1411.32Sthorpej	for (dlp = (dec_disklabel *)bp->b_data;
1421.45Ssimonb	     dlp <= (dec_disklabel *)
1431.45Ssimonb	     ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp));
1441.21Ssimonb	     dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) {
1451.5Sjonathan
1461.5Sjonathan		int part;
1471.5Sjonathan
1481.5Sjonathan		if (dlp->magic != DEC_LABEL_MAGIC) {
1491.13Schristos			printf("label: %x\n",dlp->magic);
1501.5Sjonathan			msg = ((msg != NULL) ? msg: "no disk label");
1511.5Sjonathan			goto done;
1521.5Sjonathan		}
1531.5Sjonathan
1541.6Sjonathan#ifdef DIAGNOSTIC
1551.13Schristos/*XXX*/		printf("Interpreting Ultrix label\n");
1561.6Sjonathan#endif
1571.5Sjonathan
1581.5Sjonathan		lp->d_magic = DEC_LABEL_MAGIC;
1591.5Sjonathan		lp->d_npartitions = 0;
1601.28Smhitch		strncpy(lp->d_packname, "Ultrix label", 16);
1611.28Smhitch		lp->d_rpm = 3600;
1621.28Smhitch		lp->d_interleave = 1;
1631.28Smhitch		lp->d_flags = 0;
1641.28Smhitch		lp->d_bbsize = BBSIZE;
1651.37Sdrochner		lp->d_sbsize = SBLOCKSIZE;
1661.5Sjonathan		for (part = 0;
1671.5Sjonathan		     part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
1681.5Sjonathan			    MAXPARTITIONS : DEC_NUM_DISK_PARTS);
1691.5Sjonathan		     part++) {
1701.53Stsutsui			lp->d_partitions[part].p_size =
1711.53Stsutsui			    dlp->map[part].num_blocks;
1721.53Stsutsui			lp->d_partitions[part].p_offset =
1731.53Stsutsui			    dlp->map[part].start_block;
1741.5Sjonathan			lp->d_partitions[part].p_fsize = 1024;
1751.5Sjonathan			lp->d_partitions[part].p_fstype =
1761.5Sjonathan			  (part==1) ? FS_SWAP : FS_BSDFFS;
1771.5Sjonathan			lp->d_npartitions += 1;
1781.5Sjonathan
1791.6Sjonathan#ifdef DIAGNOSTIC
1801.13Schristos			printf(" Ultrix label rz%d%c: start %d len %d\n",
1811.5Sjonathan			       DISKUNIT(dev), "abcdefgh"[part],
1821.5Sjonathan			       lp->d_partitions[part].p_offset,
1831.5Sjonathan			       lp->d_partitions[part].p_size);
1841.19Ssimonb#endif
1851.5Sjonathan		}
1861.5Sjonathan		break;
1871.4Sdean	}
1881.5Sjonathan
1891.5Sjonathandone:
1901.46Sad	brelse(bp, 0);
1911.53Stsutsui	return msg;
1921.1Sderaadt}
1931.1Sderaadt
1941.1Sderaadt/*
1951.1Sderaadt * Check new disk label for sensibility
1961.1Sderaadt * before setting it.
1971.1Sderaadt */
1981.9Sjonathanint
1991.53Stsutsuisetdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
2001.53Stsutsui    struct cpu_disklabel *osdep)
2011.1Sderaadt{
2021.16Smrg	int i;
2031.16Smrg	struct partition *opp, *npp;
2041.1Sderaadt
2051.1Sderaadt	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
2061.1Sderaadt	    dkcksum(nlp) != 0)
2071.53Stsutsui		return EINVAL;
2081.36Ssimonb	while ((i = ffs(openmask)) != 0) {
2091.1Sderaadt		i--;
2101.1Sderaadt		openmask &= ~(1 << i);
2111.1Sderaadt		if (nlp->d_npartitions <= i)
2121.53Stsutsui			return EBUSY;
2131.1Sderaadt		opp = &olp->d_partitions[i];
2141.1Sderaadt		npp = &nlp->d_partitions[i];
2151.1Sderaadt		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
2161.53Stsutsui			return EBUSY;
2171.1Sderaadt		/*
2181.1Sderaadt		 * Copy internally-set partition information
2191.1Sderaadt		 * if new label doesn't include it.		XXX
2201.1Sderaadt		 */
2211.1Sderaadt		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
2221.1Sderaadt			npp->p_fstype = opp->p_fstype;
2231.1Sderaadt			npp->p_fsize = opp->p_fsize;
2241.1Sderaadt			npp->p_frag = opp->p_frag;
2251.1Sderaadt			npp->p_cpg = opp->p_cpg;
2261.1Sderaadt		}
2271.1Sderaadt	}
2281.1Sderaadt 	nlp->d_checksum = 0;
2291.1Sderaadt 	nlp->d_checksum = dkcksum(nlp);
2301.1Sderaadt	*olp = *nlp;
2311.53Stsutsui	return 0;
2321.1Sderaadt}
2331.1Sderaadt
2341.1Sderaadt/*
2351.1Sderaadt * Write disk label back to device after modification.
2361.1Sderaadt */
2371.9Sjonathanint
2381.53Stsutsuiwritedisklabel(dev_t dev, void (*strat)(struct buf *bp), struct disklabel *lp,
2391.53Stsutsui    struct cpu_disklabel *osdep)
2401.1Sderaadt{
2411.1Sderaadt	struct buf *bp;
2421.1Sderaadt	struct disklabel *dlp;
2431.1Sderaadt	int labelpart;
2441.1Sderaadt	int error = 0;
2451.1Sderaadt
2461.30Stsutsui	labelpart = DISKPART(dev);
2471.1Sderaadt	if (lp->d_partitions[labelpart].p_offset != 0) {
2481.1Sderaadt		if (lp->d_partitions[0].p_offset != 0)
2491.53Stsutsui			return EXDEV;			/* not quite right */
2501.1Sderaadt		labelpart = 0;
2511.1Sderaadt	}
2521.1Sderaadt	bp = geteblk((int)lp->d_secsize);
2531.30Stsutsui	bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart));
2541.1Sderaadt	bp->b_blkno = LABELSECTOR;
2551.1Sderaadt	bp->b_bcount = lp->d_secsize;
2561.34Schs	bp->b_flags |= B_READ;
2571.1Sderaadt	(*strat)(bp);
2581.9Sjonathan	if ((error = biowait(bp)) != 0)
2591.1Sderaadt		goto done;
2601.32Sthorpej	for (dlp = (struct disklabel *)bp->b_data;
2611.1Sderaadt	    dlp <= (struct disklabel *)
2621.45Ssimonb	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
2631.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
2641.1Sderaadt		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
2651.1Sderaadt		    dkcksum(dlp) == 0) {
2661.1Sderaadt			*dlp = *lp;
2671.48Sad			bp->b_oflags &= ~(BO_DONE);
2681.48Sad			bp->b_flags &= ~(B_READ);
2691.34Schs			bp->b_flags |= B_WRITE;
2701.1Sderaadt			(*strat)(bp);
2711.1Sderaadt			error = biowait(bp);
2721.1Sderaadt			goto done;
2731.1Sderaadt		}
2741.1Sderaadt	}
2751.1Sderaadt	error = ESRCH;
2761.1Sderaadtdone:
2771.46Sad	brelse(bp, 0);
2781.53Stsutsui	return error;
2791.6Sjonathan}
280