disksubr.c revision 1.40
11.40Slukem/*	$NetBSD: disksubr.c,v 1.40 2003/07/15 02:54:40 lukem 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.1Sderaadt * 3. All advertising materials mentioning features or use of this software
161.1Sderaadt *    must display the following acknowledgement:
171.1Sderaadt *	This product includes software developed by the University of
181.1Sderaadt *	California, Berkeley and its contributors.
191.1Sderaadt * 4. Neither the name of the University nor the names of its contributors
201.1Sderaadt *    may be used to endorse or promote products derived from this software
211.1Sderaadt *    without specific prior written permission.
221.1Sderaadt *
231.1Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241.1Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251.1Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261.1Sderaadt * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271.1Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281.1Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291.1Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301.1Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311.1Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321.1Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331.1Sderaadt * SUCH DAMAGE.
341.1Sderaadt *
351.3Scgd *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
361.1Sderaadt */
371.40Slukem
381.40Slukem#include <sys/cdefs.h>
391.40Slukem__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.40 2003/07/15 02:54:40 lukem Exp $");
401.15Sthorpej
411.15Sthorpej#include "opt_compat_ultrix.h"
421.1Sderaadt
431.8Smycroft#include <sys/param.h>
441.8Smycroft#include <sys/systm.h>
451.8Smycroft#include <sys/buf.h>
461.9Sjonathan#include <sys/disk.h>
471.8Smycroft#include <sys/disklabel.h>
481.1Sderaadt
491.4Sdean#ifdef COMPAT_ULTRIX
501.33Smatt#include <dev/dec/dec_boot.h>
511.28Smhitch#include <ufs/ufs/dinode.h>		/* XXX for fs.h */
521.28Smhitch#include <ufs/ffs/fs.h>			/* XXX for BBSIZE & SBSIZE */
531.5Sjonathan
541.24Ssimonbchar	*compat_label __P((dev_t dev, void (*strat) __P((struct buf *bp)),
551.24Ssimonb	    struct disklabel *lp, struct cpu_disklabel *osdep));	/* XXX */
561.5Sjonathan
571.4Sdean#endif
581.9Sjonathan
591.1Sderaadt/*
601.1Sderaadt * Attempt to read a disk label from a device
611.35Swiz * using the indicated strategy routine.
621.1Sderaadt * The label must be partly set up before this:
631.1Sderaadt * secpercyl and anything required in the strategy routine
641.1Sderaadt * (e.g., sector size) must be filled in before calling us.
651.1Sderaadt * Returns null on success and an error string on failure.
661.1Sderaadt */
671.38Sdslconst char *
681.1Sderaadtreaddisklabel(dev, strat, lp, osdep)
691.1Sderaadt	dev_t dev;
701.11Sjonathan	void (*strat) __P((struct buf *bp));
711.20Ssimonb	struct disklabel *lp;
721.1Sderaadt	struct cpu_disklabel *osdep;
731.1Sderaadt{
741.20Ssimonb	struct buf *bp;
751.1Sderaadt	struct disklabel *dlp;
761.1Sderaadt	char *msg = NULL;
771.1Sderaadt
781.1Sderaadt	if (lp->d_secperunit == 0)
791.1Sderaadt		lp->d_secperunit = 0x1fffffff;
801.1Sderaadt	lp->d_npartitions = 1;
811.1Sderaadt	if (lp->d_partitions[0].p_size == 0)
821.1Sderaadt		lp->d_partitions[0].p_size = 0x1fffffff;
831.1Sderaadt	lp->d_partitions[0].p_offset = 0;
841.1Sderaadt
851.1Sderaadt	bp = geteblk((int)lp->d_secsize);
861.1Sderaadt	bp->b_dev = dev;
871.1Sderaadt	bp->b_blkno = LABELSECTOR;
881.1Sderaadt	bp->b_bcount = lp->d_secsize;
891.34Schs	bp->b_flags |= B_READ;
901.26Sthorpej	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
911.1Sderaadt	(*strat)(bp);
921.1Sderaadt	if (biowait(bp)) {
931.1Sderaadt		msg = "I/O error";
941.32Sthorpej	} else for (dlp = (struct disklabel *)bp->b_data;
951.32Sthorpej	    dlp <= (struct disklabel *)(bp->b_data+DEV_BSIZE-sizeof(*dlp));
961.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
971.1Sderaadt		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
981.1Sderaadt			if (msg == NULL)
991.1Sderaadt				msg = "no disk label";
1001.1Sderaadt		} else if (dlp->d_npartitions > MAXPARTITIONS ||
1011.1Sderaadt			   dkcksum(dlp) != 0)
1021.1Sderaadt			msg = "disk label corrupted";
1031.1Sderaadt		else {
1041.1Sderaadt			*lp = *dlp;
1051.1Sderaadt			msg = NULL;
1061.1Sderaadt			break;
1071.1Sderaadt		}
1081.1Sderaadt	}
1091.5Sjonathan	brelse(bp);
1101.28Smhitch#ifdef COMPAT_ULTRIX
1111.28Smhitch	/*
1121.28Smhitch	 * If no NetBSD label was found, check for an Ultrix label and
1131.28Smhitch	 * construct tne incore label from the Ultrix partition information.
1141.28Smhitch	 */
1151.28Smhitch	if (msg != NULL) {
1161.28Smhitch		msg = compat_label(dev, strat, lp, osdep);
1171.28Smhitch		if (msg == NULL) {
1181.28Smhitch			printf("WARNING: using Ultrix partition information\n");
1191.28Smhitch			/* set geometry? */
1201.28Smhitch		}
1211.28Smhitch	}
1221.28Smhitch#endif
1231.28Smhitch/* XXX If no NetBSD label or Ultrix label found, generate default label here */
1241.5Sjonathan	return (msg);
1251.5Sjonathan}
1261.5Sjonathan
1271.5Sjonathan#ifdef COMPAT_ULTRIX
1281.5Sjonathan/*
1291.5Sjonathan * Given a buffer bp, try and interpret it as an Ultrix disk label,
1301.5Sjonathan * putting the partition info into a native NetBSD label
1311.5Sjonathan */
1321.5Sjonathanchar *
1331.5Sjonathancompat_label(dev, strat, lp, osdep)
1341.5Sjonathan	dev_t dev;
1351.11Sjonathan	void (*strat) __P((struct buf *bp));
1361.20Ssimonb	struct disklabel *lp;
1371.5Sjonathan	struct cpu_disklabel *osdep;
1381.5Sjonathan{
1391.21Ssimonb	dec_disklabel *dlp;
1401.5Sjonathan	struct buf *bp = NULL;
1411.5Sjonathan	char *msg = NULL;
1421.5Sjonathan
1431.5Sjonathan	bp = geteblk((int)lp->d_secsize);
1441.5Sjonathan	bp->b_dev = dev;
1451.5Sjonathan	bp->b_blkno = DEC_LABEL_SECTOR;
1461.5Sjonathan	bp->b_bcount = lp->d_secsize;
1471.34Schs	bp->b_flags |= B_READ;
1481.26Sthorpej	bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl;
1491.5Sjonathan	(*strat)(bp);
1501.5Sjonathan
1511.5Sjonathan	if (biowait(bp)) {
1521.4Sdean                msg = "I/O error";
1531.5Sjonathan		goto done;
1541.5Sjonathan	}
1551.5Sjonathan
1561.32Sthorpej	for (dlp = (dec_disklabel *)bp->b_data;
1571.32Sthorpej	     dlp <= (dec_disklabel *)(bp->b_data+DEV_BSIZE-sizeof(*dlp));
1581.21Ssimonb	     dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) {
1591.5Sjonathan
1601.5Sjonathan		int part;
1611.5Sjonathan
1621.5Sjonathan		if (dlp->magic != DEC_LABEL_MAGIC) {
1631.13Schristos			printf("label: %x\n",dlp->magic);
1641.5Sjonathan			msg = ((msg != NULL) ? msg: "no disk label");
1651.5Sjonathan			goto done;
1661.5Sjonathan		}
1671.5Sjonathan
1681.6Sjonathan#ifdef DIAGNOSTIC
1691.13Schristos/*XXX*/		printf("Interpreting Ultrix label\n");
1701.6Sjonathan#endif
1711.5Sjonathan
1721.5Sjonathan		lp->d_magic = DEC_LABEL_MAGIC;
1731.5Sjonathan		lp->d_npartitions = 0;
1741.28Smhitch		strncpy(lp->d_packname, "Ultrix label", 16);
1751.28Smhitch		lp->d_rpm = 3600;
1761.28Smhitch		lp->d_interleave = 1;
1771.28Smhitch		lp->d_flags = 0;
1781.28Smhitch		lp->d_bbsize = BBSIZE;
1791.37Sdrochner		lp->d_sbsize = SBLOCKSIZE;
1801.5Sjonathan		for (part = 0;
1811.5Sjonathan		     part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
1821.5Sjonathan			    MAXPARTITIONS : DEC_NUM_DISK_PARTS);
1831.5Sjonathan		     part++) {
1841.21Ssimonb			lp->d_partitions[part].p_size = dlp->map[part].num_blocks;
1851.21Ssimonb			lp->d_partitions[part].p_offset = dlp->map[part].start_block;
1861.5Sjonathan			lp->d_partitions[part].p_fsize = 1024;
1871.5Sjonathan			lp->d_partitions[part].p_fstype =
1881.5Sjonathan			  (part==1) ? FS_SWAP : FS_BSDFFS;
1891.5Sjonathan			lp->d_npartitions += 1;
1901.5Sjonathan
1911.6Sjonathan#ifdef DIAGNOSTIC
1921.13Schristos			printf(" Ultrix label rz%d%c: start %d len %d\n",
1931.5Sjonathan			       DISKUNIT(dev), "abcdefgh"[part],
1941.5Sjonathan			       lp->d_partitions[part].p_offset,
1951.5Sjonathan			       lp->d_partitions[part].p_size);
1961.19Ssimonb#endif
1971.5Sjonathan		}
1981.5Sjonathan		break;
1991.4Sdean	}
2001.5Sjonathan
2011.5Sjonathandone:
2021.1Sderaadt	brelse(bp);
2031.1Sderaadt	return (msg);
2041.1Sderaadt}
2051.5Sjonathan#endif /* COMPAT_ULTRIX */
2061.5Sjonathan
2071.1Sderaadt
2081.1Sderaadt/*
2091.1Sderaadt * Check new disk label for sensibility
2101.1Sderaadt * before setting it.
2111.1Sderaadt */
2121.9Sjonathanint
2131.1Sderaadtsetdisklabel(olp, nlp, openmask, osdep)
2141.16Smrg	struct disklabel *olp, *nlp;
2151.1Sderaadt	u_long openmask;
2161.1Sderaadt	struct cpu_disklabel *osdep;
2171.1Sderaadt{
2181.16Smrg	int i;
2191.16Smrg	struct partition *opp, *npp;
2201.1Sderaadt
2211.1Sderaadt	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
2221.1Sderaadt	    dkcksum(nlp) != 0)
2231.1Sderaadt		return (EINVAL);
2241.36Ssimonb	while ((i = ffs(openmask)) != 0) {
2251.1Sderaadt		i--;
2261.1Sderaadt		openmask &= ~(1 << i);
2271.1Sderaadt		if (nlp->d_npartitions <= i)
2281.1Sderaadt			return (EBUSY);
2291.1Sderaadt		opp = &olp->d_partitions[i];
2301.1Sderaadt		npp = &nlp->d_partitions[i];
2311.1Sderaadt		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
2321.1Sderaadt			return (EBUSY);
2331.1Sderaadt		/*
2341.1Sderaadt		 * Copy internally-set partition information
2351.1Sderaadt		 * if new label doesn't include it.		XXX
2361.1Sderaadt		 */
2371.1Sderaadt		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
2381.1Sderaadt			npp->p_fstype = opp->p_fstype;
2391.1Sderaadt			npp->p_fsize = opp->p_fsize;
2401.1Sderaadt			npp->p_frag = opp->p_frag;
2411.1Sderaadt			npp->p_cpg = opp->p_cpg;
2421.1Sderaadt		}
2431.1Sderaadt	}
2441.1Sderaadt 	nlp->d_checksum = 0;
2451.1Sderaadt 	nlp->d_checksum = dkcksum(nlp);
2461.1Sderaadt	*olp = *nlp;
2471.1Sderaadt	return (0);
2481.1Sderaadt}
2491.1Sderaadt
2501.1Sderaadt/*
2511.1Sderaadt * Write disk label back to device after modification.
2521.1Sderaadt */
2531.9Sjonathanint
2541.1Sderaadtwritedisklabel(dev, strat, lp, osdep)
2551.1Sderaadt	dev_t dev;
2561.11Sjonathan	void (*strat) __P((struct buf *bp));
2571.20Ssimonb	struct disklabel *lp;
2581.1Sderaadt	struct cpu_disklabel *osdep;
2591.1Sderaadt{
2601.1Sderaadt	struct buf *bp;
2611.1Sderaadt	struct disklabel *dlp;
2621.1Sderaadt	int labelpart;
2631.1Sderaadt	int error = 0;
2641.1Sderaadt
2651.30Stsutsui	labelpart = DISKPART(dev);
2661.1Sderaadt	if (lp->d_partitions[labelpart].p_offset != 0) {
2671.1Sderaadt		if (lp->d_partitions[0].p_offset != 0)
2681.1Sderaadt			return (EXDEV);			/* not quite right */
2691.1Sderaadt		labelpart = 0;
2701.1Sderaadt	}
2711.1Sderaadt	bp = geteblk((int)lp->d_secsize);
2721.30Stsutsui	bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart));
2731.1Sderaadt	bp->b_blkno = LABELSECTOR;
2741.1Sderaadt	bp->b_bcount = lp->d_secsize;
2751.34Schs	bp->b_flags |= B_READ;
2761.1Sderaadt	(*strat)(bp);
2771.9Sjonathan	if ((error = biowait(bp)) != 0)
2781.1Sderaadt		goto done;
2791.32Sthorpej	for (dlp = (struct disklabel *)bp->b_data;
2801.1Sderaadt	    dlp <= (struct disklabel *)
2811.32Sthorpej	      (bp->b_data + lp->d_secsize - sizeof(*dlp));
2821.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
2831.1Sderaadt		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
2841.1Sderaadt		    dkcksum(dlp) == 0) {
2851.1Sderaadt			*dlp = *lp;
2861.34Schs			bp->b_flags &= ~(B_READ|B_DONE);
2871.34Schs			bp->b_flags |= B_WRITE;
2881.1Sderaadt			(*strat)(bp);
2891.1Sderaadt			error = biowait(bp);
2901.1Sderaadt			goto done;
2911.1Sderaadt		}
2921.1Sderaadt	}
2931.1Sderaadt	error = ESRCH;
2941.1Sderaadtdone:
2951.1Sderaadt	brelse(bp);
2961.1Sderaadt	return (error);
2971.6Sjonathan}
2981.6Sjonathan
2991.19Ssimonb/*
3001.6Sjonathan * UNTESTED !!
3011.6Sjonathan *
3021.6Sjonathan * Determine the size of the transfer, and make sure it is
3031.6Sjonathan * within the boundaries of the partition. Adjust transfer
3041.6Sjonathan * if needed, and signal errors or early completion.
3051.6Sjonathan */
3061.6Sjonathanint
3071.39Sthorpejbounds_check_with_label(dk, bp, wlabel)
3081.39Sthorpej	struct disk *dk;
3091.6Sjonathan	struct buf *bp;
3101.6Sjonathan	int wlabel;
3111.6Sjonathan{
3121.39Sthorpej	struct disklabel *lp = dk->dk_label;
3131.30Stsutsui	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
3141.22Ssimonb	int labelsect = lp->d_partitions[RAW_PART].p_offset;
3151.6Sjonathan	int maxsz = p->p_size;
3161.6Sjonathan	int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
3171.6Sjonathan
3181.6Sjonathan	/* overwriting disk label ? */
3191.19Ssimonb	/* XXX should also protect bootstrap in first 8K */
3201.6Sjonathan	if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
3211.6Sjonathan	    (bp->b_flags & B_READ) == 0 && wlabel == 0) {
3221.6Sjonathan		bp->b_error = EROFS;
3231.6Sjonathan		goto bad;
3241.6Sjonathan	}
3251.6Sjonathan
3261.19Ssimonb	/* beyond partition? */
3271.6Sjonathan	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
3281.6Sjonathan		/* if exactly at end of disk, return an EOF */
3291.6Sjonathan		if (bp->b_blkno == maxsz) {
3301.6Sjonathan			bp->b_resid = bp->b_bcount;
3311.6Sjonathan			return(0);
3321.6Sjonathan		}
3331.6Sjonathan		/* or truncate if part of it fits */
3341.6Sjonathan		sz = maxsz - bp->b_blkno;
3351.6Sjonathan		if (sz <= 0) {
3361.6Sjonathan			bp->b_error = EINVAL;
3371.6Sjonathan			goto bad;
3381.6Sjonathan		}
3391.6Sjonathan		bp->b_bcount = sz << DEV_BSHIFT;
3401.19Ssimonb	}
3411.6Sjonathan
3421.6Sjonathan	/* calculate cylinder for disksort to order transfers with */
3431.6Sjonathan	bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
3441.6Sjonathan	return(1);
3451.6Sjonathanbad:
3461.6Sjonathan	bp->b_flags |= B_ERROR;
3471.6Sjonathan	return(-1);
3481.1Sderaadt}
349