disksubr.c revision 1.50
11.50Sdsl/*	$NetBSD: disksubr.c,v 1.50 2009/03/14 14:46:04 dsl 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.50Sdsl__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.50 2009/03/14 14:46:04 dsl Exp $");
361.1Sderaadt
371.8Smycroft#include <sys/param.h>
381.8Smycroft#include <sys/systm.h>
391.8Smycroft#include <sys/buf.h>
401.9Sjonathan#include <sys/disk.h>
411.8Smycroft#include <sys/disklabel.h>
421.1Sderaadt
431.33Smatt#include <dev/dec/dec_boot.h>
441.28Smhitch#include <ufs/ufs/dinode.h>		/* XXX for fs.h */
451.28Smhitch#include <ufs/ffs/fs.h>			/* XXX for BBSIZE & SBSIZE */
461.5Sjonathan
471.50Sdslconst char *compat_label(dev_t dev, void (*strat)(struct buf *bp),
481.50Sdsl	struct disklabel *lp, struct cpu_disklabel *osdep);	/* XXX */
491.5Sjonathan
501.1Sderaadt/*
511.1Sderaadt * Attempt to read a disk label from a device
521.35Swiz * using the indicated strategy routine.
531.1Sderaadt * The label must be partly set up before this:
541.1Sderaadt * secpercyl and anything required in the strategy routine
551.1Sderaadt * (e.g., sector size) must be filled in before calling us.
561.1Sderaadt * Returns null on success and an error string on failure.
571.1Sderaadt */
581.38Sdslconst char *
591.1Sderaadtreaddisklabel(dev, strat, lp, osdep)
601.1Sderaadt	dev_t dev;
611.50Sdsl	void (*strat)(struct buf *bp);
621.20Ssimonb	struct disklabel *lp;
631.1Sderaadt	struct cpu_disklabel *osdep;
641.1Sderaadt{
651.20Ssimonb	struct buf *bp;
661.1Sderaadt	struct disklabel *dlp;
671.42Sdrochner	const char *msg = NULL;
681.1Sderaadt
691.1Sderaadt	if (lp->d_secperunit == 0)
701.1Sderaadt		lp->d_secperunit = 0x1fffffff;
711.1Sderaadt	lp->d_npartitions = 1;
721.1Sderaadt	if (lp->d_partitions[0].p_size == 0)
731.1Sderaadt		lp->d_partitions[0].p_size = 0x1fffffff;
741.1Sderaadt	lp->d_partitions[0].p_offset = 0;
751.1Sderaadt
761.1Sderaadt	bp = geteblk((int)lp->d_secsize);
771.1Sderaadt	bp->b_dev = dev;
781.1Sderaadt	bp->b_blkno = LABELSECTOR;
791.1Sderaadt	bp->b_bcount = lp->d_secsize;
801.34Schs	bp->b_flags |= B_READ;
811.26Sthorpej	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
821.1Sderaadt	(*strat)(bp);
831.1Sderaadt	if (biowait(bp)) {
841.1Sderaadt		msg = "I/O error";
851.32Sthorpej	} else for (dlp = (struct disklabel *)bp->b_data;
861.45Ssimonb	    dlp <= (struct disklabel *)
871.45Ssimonb	    ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp));
881.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
891.1Sderaadt		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
901.1Sderaadt			if (msg == NULL)
911.1Sderaadt				msg = "no disk label";
921.1Sderaadt		} else if (dlp->d_npartitions > MAXPARTITIONS ||
931.1Sderaadt			   dkcksum(dlp) != 0)
941.1Sderaadt			msg = "disk label corrupted";
951.1Sderaadt		else {
961.1Sderaadt			*lp = *dlp;
971.1Sderaadt			msg = NULL;
981.1Sderaadt			break;
991.1Sderaadt		}
1001.1Sderaadt	}
1011.46Sad	brelse(bp, 0);
1021.28Smhitch	/*
1031.28Smhitch	 * If no NetBSD label was found, check for an Ultrix label and
1041.28Smhitch	 * construct tne incore label from the Ultrix partition information.
1051.28Smhitch	 */
1061.28Smhitch	if (msg != NULL) {
1071.28Smhitch		msg = compat_label(dev, strat, lp, osdep);
1081.28Smhitch		if (msg == NULL) {
1091.28Smhitch			printf("WARNING: using Ultrix partition information\n");
1101.28Smhitch			/* set geometry? */
1111.28Smhitch		}
1121.28Smhitch	}
1131.28Smhitch/* XXX If no NetBSD label or Ultrix label found, generate default label here */
1141.5Sjonathan	return (msg);
1151.5Sjonathan}
1161.5Sjonathan
1171.5Sjonathan/*
1181.5Sjonathan * Given a buffer bp, try and interpret it as an Ultrix disk label,
1191.5Sjonathan * putting the partition info into a native NetBSD label
1201.5Sjonathan */
1211.42Sdrochnerconst char *
1221.5Sjonathancompat_label(dev, strat, lp, osdep)
1231.5Sjonathan	dev_t dev;
1241.50Sdsl	void (*strat)(struct buf *bp);
1251.20Ssimonb	struct disklabel *lp;
1261.5Sjonathan	struct cpu_disklabel *osdep;
1271.5Sjonathan{
1281.21Ssimonb	dec_disklabel *dlp;
1291.5Sjonathan	struct buf *bp = NULL;
1301.42Sdrochner	const char *msg = NULL;
1311.5Sjonathan
1321.5Sjonathan	bp = geteblk((int)lp->d_secsize);
1331.5Sjonathan	bp->b_dev = dev;
1341.5Sjonathan	bp->b_blkno = DEC_LABEL_SECTOR;
1351.5Sjonathan	bp->b_bcount = lp->d_secsize;
1361.34Schs	bp->b_flags |= B_READ;
1371.26Sthorpej	bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl;
1381.5Sjonathan	(*strat)(bp);
1391.5Sjonathan
1401.5Sjonathan	if (biowait(bp)) {
1411.4Sdean                msg = "I/O error";
1421.5Sjonathan		goto done;
1431.5Sjonathan	}
1441.5Sjonathan
1451.32Sthorpej	for (dlp = (dec_disklabel *)bp->b_data;
1461.45Ssimonb	     dlp <= (dec_disklabel *)
1471.45Ssimonb	     ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp));
1481.21Ssimonb	     dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) {
1491.5Sjonathan
1501.5Sjonathan		int part;
1511.5Sjonathan
1521.5Sjonathan		if (dlp->magic != DEC_LABEL_MAGIC) {
1531.13Schristos			printf("label: %x\n",dlp->magic);
1541.5Sjonathan			msg = ((msg != NULL) ? msg: "no disk label");
1551.5Sjonathan			goto done;
1561.5Sjonathan		}
1571.5Sjonathan
1581.6Sjonathan#ifdef DIAGNOSTIC
1591.13Schristos/*XXX*/		printf("Interpreting Ultrix label\n");
1601.6Sjonathan#endif
1611.5Sjonathan
1621.5Sjonathan		lp->d_magic = DEC_LABEL_MAGIC;
1631.5Sjonathan		lp->d_npartitions = 0;
1641.28Smhitch		strncpy(lp->d_packname, "Ultrix label", 16);
1651.28Smhitch		lp->d_rpm = 3600;
1661.28Smhitch		lp->d_interleave = 1;
1671.28Smhitch		lp->d_flags = 0;
1681.28Smhitch		lp->d_bbsize = BBSIZE;
1691.37Sdrochner		lp->d_sbsize = SBLOCKSIZE;
1701.5Sjonathan		for (part = 0;
1711.5Sjonathan		     part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
1721.5Sjonathan			    MAXPARTITIONS : DEC_NUM_DISK_PARTS);
1731.5Sjonathan		     part++) {
1741.21Ssimonb			lp->d_partitions[part].p_size = dlp->map[part].num_blocks;
1751.21Ssimonb			lp->d_partitions[part].p_offset = dlp->map[part].start_block;
1761.5Sjonathan			lp->d_partitions[part].p_fsize = 1024;
1771.5Sjonathan			lp->d_partitions[part].p_fstype =
1781.5Sjonathan			  (part==1) ? FS_SWAP : FS_BSDFFS;
1791.5Sjonathan			lp->d_npartitions += 1;
1801.5Sjonathan
1811.6Sjonathan#ifdef DIAGNOSTIC
1821.13Schristos			printf(" Ultrix label rz%d%c: start %d len %d\n",
1831.5Sjonathan			       DISKUNIT(dev), "abcdefgh"[part],
1841.5Sjonathan			       lp->d_partitions[part].p_offset,
1851.5Sjonathan			       lp->d_partitions[part].p_size);
1861.19Ssimonb#endif
1871.5Sjonathan		}
1881.5Sjonathan		break;
1891.4Sdean	}
1901.5Sjonathan
1911.5Sjonathandone:
1921.46Sad	brelse(bp, 0);
1931.1Sderaadt	return (msg);
1941.1Sderaadt}
1951.1Sderaadt
1961.1Sderaadt/*
1971.1Sderaadt * Check new disk label for sensibility
1981.1Sderaadt * before setting it.
1991.1Sderaadt */
2001.9Sjonathanint
2011.1Sderaadtsetdisklabel(olp, nlp, openmask, osdep)
2021.16Smrg	struct disklabel *olp, *nlp;
2031.1Sderaadt	u_long openmask;
2041.1Sderaadt	struct cpu_disklabel *osdep;
2051.1Sderaadt{
2061.16Smrg	int i;
2071.16Smrg	struct partition *opp, *npp;
2081.1Sderaadt
2091.1Sderaadt	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
2101.1Sderaadt	    dkcksum(nlp) != 0)
2111.1Sderaadt		return (EINVAL);
2121.36Ssimonb	while ((i = ffs(openmask)) != 0) {
2131.1Sderaadt		i--;
2141.1Sderaadt		openmask &= ~(1 << i);
2151.1Sderaadt		if (nlp->d_npartitions <= i)
2161.1Sderaadt			return (EBUSY);
2171.1Sderaadt		opp = &olp->d_partitions[i];
2181.1Sderaadt		npp = &nlp->d_partitions[i];
2191.1Sderaadt		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
2201.1Sderaadt			return (EBUSY);
2211.1Sderaadt		/*
2221.1Sderaadt		 * Copy internally-set partition information
2231.1Sderaadt		 * if new label doesn't include it.		XXX
2241.1Sderaadt		 */
2251.1Sderaadt		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
2261.1Sderaadt			npp->p_fstype = opp->p_fstype;
2271.1Sderaadt			npp->p_fsize = opp->p_fsize;
2281.1Sderaadt			npp->p_frag = opp->p_frag;
2291.1Sderaadt			npp->p_cpg = opp->p_cpg;
2301.1Sderaadt		}
2311.1Sderaadt	}
2321.1Sderaadt 	nlp->d_checksum = 0;
2331.1Sderaadt 	nlp->d_checksum = dkcksum(nlp);
2341.1Sderaadt	*olp = *nlp;
2351.1Sderaadt	return (0);
2361.1Sderaadt}
2371.1Sderaadt
2381.1Sderaadt/*
2391.1Sderaadt * Write disk label back to device after modification.
2401.1Sderaadt */
2411.9Sjonathanint
2421.1Sderaadtwritedisklabel(dev, strat, lp, osdep)
2431.1Sderaadt	dev_t dev;
2441.50Sdsl	void (*strat)(struct buf *bp);
2451.20Ssimonb	struct disklabel *lp;
2461.1Sderaadt	struct cpu_disklabel *osdep;
2471.1Sderaadt{
2481.1Sderaadt	struct buf *bp;
2491.1Sderaadt	struct disklabel *dlp;
2501.1Sderaadt	int labelpart;
2511.1Sderaadt	int error = 0;
2521.1Sderaadt
2531.30Stsutsui	labelpart = DISKPART(dev);
2541.1Sderaadt	if (lp->d_partitions[labelpart].p_offset != 0) {
2551.1Sderaadt		if (lp->d_partitions[0].p_offset != 0)
2561.1Sderaadt			return (EXDEV);			/* not quite right */
2571.1Sderaadt		labelpart = 0;
2581.1Sderaadt	}
2591.1Sderaadt	bp = geteblk((int)lp->d_secsize);
2601.30Stsutsui	bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart));
2611.1Sderaadt	bp->b_blkno = LABELSECTOR;
2621.1Sderaadt	bp->b_bcount = lp->d_secsize;
2631.34Schs	bp->b_flags |= B_READ;
2641.1Sderaadt	(*strat)(bp);
2651.9Sjonathan	if ((error = biowait(bp)) != 0)
2661.1Sderaadt		goto done;
2671.32Sthorpej	for (dlp = (struct disklabel *)bp->b_data;
2681.1Sderaadt	    dlp <= (struct disklabel *)
2691.45Ssimonb	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
2701.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
2711.1Sderaadt		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
2721.1Sderaadt		    dkcksum(dlp) == 0) {
2731.1Sderaadt			*dlp = *lp;
2741.48Sad			bp->b_oflags &= ~(BO_DONE);
2751.48Sad			bp->b_flags &= ~(B_READ);
2761.34Schs			bp->b_flags |= B_WRITE;
2771.1Sderaadt			(*strat)(bp);
2781.1Sderaadt			error = biowait(bp);
2791.1Sderaadt			goto done;
2801.1Sderaadt		}
2811.1Sderaadt	}
2821.1Sderaadt	error = ESRCH;
2831.1Sderaadtdone:
2841.46Sad	brelse(bp, 0);
2851.1Sderaadt	return (error);
2861.6Sjonathan}
287