disksubr.c revision 1.13
11.13Schristos/*	$NetBSD: disksubr.c,v 1.13 1996/10/13 03:39:49 christos 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.1Sderaadt
381.8Smycroft#include <sys/param.h>
391.8Smycroft#include <sys/systm.h>
401.8Smycroft#include <sys/buf.h>
411.9Sjonathan#include <sys/device.h>
421.9Sjonathan#include <sys/disk.h>
431.8Smycroft#include <sys/disklabel.h>
441.8Smycroft#include <sys/syslog.h>
451.1Sderaadt
461.1Sderaadt#define	b_cylin	b_resid
471.1Sderaadt
481.4Sdean#ifdef COMPAT_ULTRIX
491.4Sdean#include "../../stand/dec_boot.h"
501.5Sjonathan
511.5Sjonathanextern char *
521.9Sjonathancompat_label __P((dev_t dev, void (*strat) __P((struct buf *bp)),
531.5Sjonathan		  struct disklabel *lp, struct cpu_disklabel *osdep));
541.5Sjonathan
551.4Sdean#endif
561.4Sdean
571.9Sjonathanchar*	readdisklabel __P((dev_t dev, void (*strat) __P((struct buf *bp)),
581.9Sjonathan		       register struct disklabel *lp,
591.9Sjonathan		       struct cpu_disklabel *osdep));
601.9Sjonathan
611.1Sderaadt/*
621.1Sderaadt * Attempt to read a disk label from a device
631.1Sderaadt * using the indicated stategy routine.
641.1Sderaadt * The label must be partly set up before this:
651.1Sderaadt * secpercyl and anything required in the strategy routine
661.1Sderaadt * (e.g., sector size) must be filled in before calling us.
671.1Sderaadt * Returns null on success and an error string on failure.
681.1Sderaadt */
691.1Sderaadtchar *
701.1Sderaadtreaddisklabel(dev, strat, lp, osdep)
711.1Sderaadt	dev_t dev;
721.11Sjonathan	void (*strat) __P((struct buf *bp));
731.1Sderaadt	register struct disklabel *lp;
741.1Sderaadt	struct cpu_disklabel *osdep;
751.1Sderaadt{
761.1Sderaadt	register struct buf *bp;
771.1Sderaadt	struct disklabel *dlp;
781.1Sderaadt	char *msg = NULL;
791.1Sderaadt
801.1Sderaadt	if (lp->d_secperunit == 0)
811.1Sderaadt		lp->d_secperunit = 0x1fffffff;
821.1Sderaadt	lp->d_npartitions = 1;
831.1Sderaadt	if (lp->d_partitions[0].p_size == 0)
841.1Sderaadt		lp->d_partitions[0].p_size = 0x1fffffff;
851.1Sderaadt	lp->d_partitions[0].p_offset = 0;
861.1Sderaadt
871.1Sderaadt	bp = geteblk((int)lp->d_secsize);
881.1Sderaadt	bp->b_dev = dev;
891.1Sderaadt	bp->b_blkno = LABELSECTOR;
901.1Sderaadt	bp->b_bcount = lp->d_secsize;
911.1Sderaadt	bp->b_flags = B_BUSY | B_READ;
921.1Sderaadt	bp->b_cylin = LABELSECTOR / lp->d_secpercyl;
931.1Sderaadt	(*strat)(bp);
941.1Sderaadt	if (biowait(bp)) {
951.1Sderaadt		msg = "I/O error";
961.1Sderaadt	} else for (dlp = (struct disklabel *)bp->b_un.b_addr;
971.1Sderaadt	    dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
981.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
991.1Sderaadt		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
1001.1Sderaadt			if (msg == NULL)
1011.1Sderaadt				msg = "no disk label";
1021.1Sderaadt		} else if (dlp->d_npartitions > MAXPARTITIONS ||
1031.1Sderaadt			   dkcksum(dlp) != 0)
1041.1Sderaadt			msg = "disk label corrupted";
1051.1Sderaadt		else {
1061.1Sderaadt			*lp = *dlp;
1071.1Sderaadt			msg = NULL;
1081.1Sderaadt			break;
1091.1Sderaadt		}
1101.1Sderaadt	}
1111.5Sjonathan	bp->b_flags = B_INVAL | B_AGE;
1121.5Sjonathan	brelse(bp);
1131.5Sjonathan	return (msg);
1141.5Sjonathan}
1151.5Sjonathan
1161.5Sjonathan#ifdef COMPAT_ULTRIX
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.5Sjonathanchar *
1221.5Sjonathancompat_label(dev, strat, lp, osdep)
1231.5Sjonathan	dev_t dev;
1241.11Sjonathan	void (*strat) __P((struct buf *bp));
1251.5Sjonathan	register struct disklabel *lp;
1261.5Sjonathan	struct cpu_disklabel *osdep;
1271.5Sjonathan{
1281.5Sjonathan	Dec_DiskLabel *dlp;
1291.5Sjonathan	struct buf *bp = NULL;
1301.5Sjonathan	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.5Sjonathan	bp->b_flags = B_BUSY | B_READ;
1371.5Sjonathan	bp->b_cylin = 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.5Sjonathan	for (dlp = (Dec_DiskLabel *)bp->b_un.b_addr;
1461.5Sjonathan	     dlp <= (Dec_DiskLabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
1471.5Sjonathan	     dlp = (Dec_DiskLabel *)((char *)dlp + sizeof(long))) {
1481.5Sjonathan
1491.5Sjonathan		int part;
1501.5Sjonathan
1511.5Sjonathan		if (dlp->magic != DEC_LABEL_MAGIC) {
1521.13Schristos			printf("label: %x\n",dlp->magic);
1531.5Sjonathan			msg = ((msg != NULL) ? msg: "no disk label");
1541.5Sjonathan			goto done;
1551.5Sjonathan		}
1561.5Sjonathan
1571.6Sjonathan#ifdef DIAGNOSTIC
1581.13Schristos/*XXX*/		printf("Interpreting Ultrix label\n");
1591.6Sjonathan#endif
1601.5Sjonathan
1611.5Sjonathan		lp->d_magic = DEC_LABEL_MAGIC;
1621.5Sjonathan		lp->d_npartitions = 0;
1631.5Sjonathan		for (part = 0;
1641.5Sjonathan		     part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
1651.5Sjonathan			    MAXPARTITIONS : DEC_NUM_DISK_PARTS);
1661.5Sjonathan		     part++) {
1671.5Sjonathan			lp->d_partitions[part].p_size = dlp->map[part].numBlocks;
1681.5Sjonathan			lp->d_partitions[part].p_offset = dlp->map[part].startBlock;
1691.5Sjonathan			lp->d_partitions[part].p_fsize = 1024;
1701.5Sjonathan			lp->d_partitions[part].p_fstype =
1711.5Sjonathan			  (part==1) ? FS_SWAP : FS_BSDFFS;
1721.5Sjonathan			lp->d_npartitions += 1;
1731.5Sjonathan
1741.6Sjonathan#ifdef DIAGNOSTIC
1751.13Schristos			printf(" Ultrix label rz%d%c: start %d len %d\n",
1761.5Sjonathan			       DISKUNIT(dev), "abcdefgh"[part],
1771.5Sjonathan			       lp->d_partitions[part].p_offset,
1781.5Sjonathan			       lp->d_partitions[part].p_size);
1791.5Sjonathan#endif
1801.5Sjonathan		}
1811.5Sjonathan		break;
1821.4Sdean	}
1831.5Sjonathan
1841.5Sjonathandone:
1851.1Sderaadt	bp->b_flags = B_INVAL | B_AGE;
1861.1Sderaadt	brelse(bp);
1871.1Sderaadt	return (msg);
1881.1Sderaadt}
1891.5Sjonathan#endif /* COMPAT_ULTRIX */
1901.5Sjonathan
1911.1Sderaadt
1921.1Sderaadt/*
1931.1Sderaadt * Check new disk label for sensibility
1941.1Sderaadt * before setting it.
1951.1Sderaadt */
1961.9Sjonathanint
1971.1Sderaadtsetdisklabel(olp, nlp, openmask, osdep)
1981.1Sderaadt	register struct disklabel *olp, *nlp;
1991.1Sderaadt	u_long openmask;
2001.1Sderaadt	struct cpu_disklabel *osdep;
2011.1Sderaadt{
2021.1Sderaadt	register i;
2031.1Sderaadt	register struct partition *opp, *npp;
2041.1Sderaadt
2051.1Sderaadt	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
2061.1Sderaadt	    dkcksum(nlp) != 0)
2071.1Sderaadt		return (EINVAL);
2081.1Sderaadt	while ((i = ffs((long)openmask)) != 0) {
2091.1Sderaadt		i--;
2101.1Sderaadt		openmask &= ~(1 << i);
2111.1Sderaadt		if (nlp->d_npartitions <= i)
2121.1Sderaadt			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.1Sderaadt			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.1Sderaadt	return (0);
2321.1Sderaadt}
2331.1Sderaadt
2341.1Sderaadt/* encoding of disk minor numbers, should be elsewhere... */
2351.1Sderaadt#define dkunit(dev)		(minor(dev) >> 3)
2361.1Sderaadt#define dkpart(dev)		(minor(dev) & 07)
2371.1Sderaadt#define dkminor(unit, part)	(((unit) << 3) | (part))
2381.1Sderaadt
2391.1Sderaadt/*
2401.1Sderaadt * Write disk label back to device after modification.
2411.1Sderaadt */
2421.9Sjonathanint
2431.1Sderaadtwritedisklabel(dev, strat, lp, osdep)
2441.1Sderaadt	dev_t dev;
2451.11Sjonathan	void (*strat) __P((struct buf *bp));
2461.1Sderaadt	register struct disklabel *lp;
2471.1Sderaadt	struct cpu_disklabel *osdep;
2481.1Sderaadt{
2491.1Sderaadt	struct buf *bp;
2501.1Sderaadt	struct disklabel *dlp;
2511.1Sderaadt	int labelpart;
2521.1Sderaadt	int error = 0;
2531.1Sderaadt
2541.1Sderaadt	labelpart = dkpart(dev);
2551.1Sderaadt	if (lp->d_partitions[labelpart].p_offset != 0) {
2561.1Sderaadt		if (lp->d_partitions[0].p_offset != 0)
2571.1Sderaadt			return (EXDEV);			/* not quite right */
2581.1Sderaadt		labelpart = 0;
2591.1Sderaadt	}
2601.1Sderaadt	bp = geteblk((int)lp->d_secsize);
2611.1Sderaadt	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart));
2621.1Sderaadt	bp->b_blkno = LABELSECTOR;
2631.1Sderaadt	bp->b_bcount = lp->d_secsize;
2641.1Sderaadt	bp->b_flags = B_READ;
2651.1Sderaadt	(*strat)(bp);
2661.9Sjonathan	if ((error = biowait(bp)) != 0)
2671.1Sderaadt		goto done;
2681.1Sderaadt	for (dlp = (struct disklabel *)bp->b_un.b_addr;
2691.1Sderaadt	    dlp <= (struct disklabel *)
2701.1Sderaadt	      (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
2711.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
2721.1Sderaadt		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
2731.1Sderaadt		    dkcksum(dlp) == 0) {
2741.1Sderaadt			*dlp = *lp;
2751.1Sderaadt			bp->b_flags = B_WRITE;
2761.1Sderaadt			(*strat)(bp);
2771.1Sderaadt			error = biowait(bp);
2781.1Sderaadt			goto done;
2791.1Sderaadt		}
2801.1Sderaadt	}
2811.1Sderaadt	error = ESRCH;
2821.1Sderaadtdone:
2831.1Sderaadt	brelse(bp);
2841.1Sderaadt	return (error);
2851.6Sjonathan}
2861.6Sjonathan
2871.6Sjonathan
2881.6Sjonathan/*
2891.6Sjonathan * was this the boot device ?
2901.6Sjonathan */
2911.10Sjonathanvoid
2921.6Sjonathandk_establish(dk, dev)
2931.7Sthorpej	struct disk *dk;
2941.6Sjonathan	struct device *dev;
2951.6Sjonathan{
2961.6Sjonathan	/* see also arch/alpha/alpha/disksubr.c */
2971.13Schristos	printf("Warning: boot path unknown\n");
2981.10Sjonathan	return;
2991.6Sjonathan}
3001.6Sjonathan
3011.6Sjonathan/*
3021.6Sjonathan * UNTESTED !!
3031.6Sjonathan *
3041.6Sjonathan * Determine the size of the transfer, and make sure it is
3051.6Sjonathan * within the boundaries of the partition. Adjust transfer
3061.6Sjonathan * if needed, and signal errors or early completion.
3071.6Sjonathan */
3081.6Sjonathanint
3091.6Sjonathanbounds_check_with_label(bp, lp, wlabel)
3101.6Sjonathan	struct buf *bp;
3111.6Sjonathan	struct disklabel *lp;
3121.6Sjonathan	int wlabel;
3131.6Sjonathan{
3141.6Sjonathan
3151.6Sjonathan	struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
3161.6Sjonathan	int labelsect = lp->d_partitions[0].p_offset;
3171.6Sjonathan	int maxsz = p->p_size;
3181.6Sjonathan	int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
3191.6Sjonathan
3201.6Sjonathan	/* overwriting disk label ? */
3211.6Sjonathan	/* XXX should also protect bootstrap in first 8K */
3221.6Sjonathan	if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
3231.6Sjonathan	    (bp->b_flags & B_READ) == 0 && wlabel == 0) {
3241.6Sjonathan		bp->b_error = EROFS;
3251.6Sjonathan		goto bad;
3261.6Sjonathan	}
3271.6Sjonathan
3281.6Sjonathan	/* beyond partition? */
3291.6Sjonathan	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
3301.6Sjonathan		/* if exactly at end of disk, return an EOF */
3311.6Sjonathan		if (bp->b_blkno == maxsz) {
3321.6Sjonathan			bp->b_resid = bp->b_bcount;
3331.6Sjonathan			return(0);
3341.6Sjonathan		}
3351.6Sjonathan		/* or truncate if part of it fits */
3361.6Sjonathan		sz = maxsz - bp->b_blkno;
3371.6Sjonathan		if (sz <= 0) {
3381.6Sjonathan			bp->b_error = EINVAL;
3391.6Sjonathan			goto bad;
3401.6Sjonathan		}
3411.6Sjonathan		bp->b_bcount = sz << DEV_BSHIFT;
3421.6Sjonathan	}
3431.6Sjonathan
3441.6Sjonathan	/* calculate cylinder for disksort to order transfers with */
3451.6Sjonathan	bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
3461.6Sjonathan	return(1);
3471.6Sjonathanbad:
3481.6Sjonathan	bp->b_flags |= B_ERROR;
3491.6Sjonathan	return(-1);
3501.1Sderaadt}
351