disksubr.c revision 1.4
11.4Sdean/*	$NetBSD: disksubr.c,v 1.4 1994/11/28 18:42:22 dean 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.2Sglass#include "param.h"
391.2Sglass#include "systm.h"
401.2Sglass#include "buf.h"
411.2Sglass#include "disklabel.h"
421.2Sglass#include "syslog.h"
431.1Sderaadt
441.1Sderaadt#define	b_cylin	b_resid
451.1Sderaadt
461.4Sdean#ifdef COMPAT_ULTRIX
471.4Sdean#include "../../stand/dec_boot.h"
481.4Sdean#endif
491.4Sdean
501.4Sdean
511.1Sderaadt/*
521.1Sderaadt * Attempt to read a disk label from a device
531.1Sderaadt * using the indicated stategy routine.
541.1Sderaadt * The label must be partly set up before this:
551.1Sderaadt * secpercyl and anything required in the strategy routine
561.1Sderaadt * (e.g., sector size) must be filled in before calling us.
571.1Sderaadt * Returns null on success and an error string on failure.
581.1Sderaadt */
591.1Sderaadtchar *
601.1Sderaadtreaddisklabel(dev, strat, lp, osdep)
611.1Sderaadt	dev_t dev;
621.1Sderaadt	void (*strat)();
631.1Sderaadt	register struct disklabel *lp;
641.1Sderaadt	struct cpu_disklabel *osdep;
651.1Sderaadt{
661.1Sderaadt	register struct buf *bp;
671.1Sderaadt	struct disklabel *dlp;
681.4Sdean#ifdef COMPAT_ULTRIX
691.4Sdean	Dec_DiskLabel *Dec_dlp;
701.4Sdean#endif
711.1Sderaadt	char *msg = NULL;
721.1Sderaadt
731.1Sderaadt	if (lp->d_secperunit == 0)
741.1Sderaadt		lp->d_secperunit = 0x1fffffff;
751.1Sderaadt	lp->d_npartitions = 1;
761.1Sderaadt	if (lp->d_partitions[0].p_size == 0)
771.1Sderaadt		lp->d_partitions[0].p_size = 0x1fffffff;
781.1Sderaadt	lp->d_partitions[0].p_offset = 0;
791.1Sderaadt
801.1Sderaadt	bp = geteblk((int)lp->d_secsize);
811.1Sderaadt	bp->b_dev = dev;
821.1Sderaadt	bp->b_blkno = LABELSECTOR;
831.1Sderaadt	bp->b_bcount = lp->d_secsize;
841.1Sderaadt	bp->b_flags = B_BUSY | B_READ;
851.1Sderaadt	bp->b_cylin = LABELSECTOR / lp->d_secpercyl;
861.1Sderaadt	(*strat)(bp);
871.1Sderaadt	if (biowait(bp)) {
881.1Sderaadt		msg = "I/O error";
891.1Sderaadt	} else for (dlp = (struct disklabel *)bp->b_un.b_addr;
901.1Sderaadt	    dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
911.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
921.1Sderaadt		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
931.1Sderaadt			if (msg == NULL)
941.1Sderaadt				msg = "no disk label";
951.1Sderaadt		} else if (dlp->d_npartitions > MAXPARTITIONS ||
961.1Sderaadt			   dkcksum(dlp) != 0)
971.1Sderaadt			msg = "disk label corrupted";
981.1Sderaadt		else {
991.1Sderaadt			*lp = *dlp;
1001.1Sderaadt			msg = NULL;
1011.1Sderaadt			break;
1021.1Sderaadt		}
1031.1Sderaadt	}
1041.4Sdean#ifdef COMPAT_ULTRIX	  /* look for ultrix disklabel
1051.4Sdean			     gallatin@isds.duke.edu , 8/21/94 */
1061.4Sdean	if(msg){
1071.4Sdean	     msg = NULL;
1081.4Sdean	     bp->b_dev = dev;
1091.4Sdean	     bp->b_blkno = DEC_LABEL_SECTOR;
1101.4Sdean	     bp->b_bcount = lp->d_secsize;
1111.4Sdean	     bp->b_flags = B_BUSY | B_READ;
1121.4Sdean	     bp->b_cylin = DEC_LABEL_SECTOR / lp->d_secpercyl;
1131.4Sdean	     (*strat)(bp);
1141.4Sdean	     if (biowait(bp)) {
1151.4Sdean                msg = "I/O error";
1161.4Sdean	     }  else for (Dec_dlp = (Dec_DiskLabel *)bp->b_un.b_addr;
1171.4Sdean			  Dec_dlp <= (Dec_DiskLabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*Dec_dlp));
1181.4Sdean			  Dec_dlp = (Dec_DiskLabel *)((char *)Dec_dlp + sizeof(long))) {
1191.4Sdean	       if (Dec_dlp->magic != DEC_LABEL_MAGIC) {
1201.4Sdean		 printf("label: %x\n",Dec_dlp->magic);
1211.4Sdean		 if (msg == NULL)
1221.4Sdean		   msg = "no disk label";
1231.4Sdean	       }
1241.4Sdean	       else {
1251.4Sdean		 int i;
1261.4Sdean		 lp->d_magic=DEC_LABEL_MAGIC;
1271.4Sdean		 for(i=0;i<((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
1281.4Sdean			    MAXPARTITIONS : DEC_NUM_DISK_PARTS); i++) {
1291.4Sdean		   lp->d_partitions[i].p_size = Dec_dlp->map[i].numBlocks;
1301.4Sdean		   lp->d_partitions[i].p_offset = Dec_dlp->map[i].startBlock;
1311.4Sdean		   lp->d_partitions[i].p_fsize = 1024;
1321.4Sdean		   if(i==1)
1331.4Sdean		     lp->d_partitions[i].p_fstype=FS_SWAP;
1341.4Sdean		   else
1351.4Sdean		     lp->d_partitions[i].p_fstype=FS_BSDFFS;
1361.4Sdean		 }
1371.4Sdean		 msg = "using ULTRIX partition information";
1381.4Sdean		 break;
1391.4Sdean	       }
1401.4Sdean	     }
1411.4Sdean	}
1421.4Sdean#endif /* COMPAT_ULTRIX */
1431.1Sderaadt	bp->b_flags = B_INVAL | B_AGE;
1441.1Sderaadt	brelse(bp);
1451.1Sderaadt	return (msg);
1461.1Sderaadt}
1471.1Sderaadt
1481.1Sderaadt/*
1491.1Sderaadt * Check new disk label for sensibility
1501.1Sderaadt * before setting it.
1511.1Sderaadt */
1521.1Sderaadtsetdisklabel(olp, nlp, openmask, osdep)
1531.1Sderaadt	register struct disklabel *olp, *nlp;
1541.1Sderaadt	u_long openmask;
1551.1Sderaadt	struct cpu_disklabel *osdep;
1561.1Sderaadt{
1571.1Sderaadt	register i;
1581.1Sderaadt	register struct partition *opp, *npp;
1591.1Sderaadt
1601.1Sderaadt	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
1611.1Sderaadt	    dkcksum(nlp) != 0)
1621.1Sderaadt		return (EINVAL);
1631.1Sderaadt	while ((i = ffs((long)openmask)) != 0) {
1641.1Sderaadt		i--;
1651.1Sderaadt		openmask &= ~(1 << i);
1661.1Sderaadt		if (nlp->d_npartitions <= i)
1671.1Sderaadt			return (EBUSY);
1681.1Sderaadt		opp = &olp->d_partitions[i];
1691.1Sderaadt		npp = &nlp->d_partitions[i];
1701.1Sderaadt		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
1711.1Sderaadt			return (EBUSY);
1721.1Sderaadt		/*
1731.1Sderaadt		 * Copy internally-set partition information
1741.1Sderaadt		 * if new label doesn't include it.		XXX
1751.1Sderaadt		 */
1761.1Sderaadt		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
1771.1Sderaadt			npp->p_fstype = opp->p_fstype;
1781.1Sderaadt			npp->p_fsize = opp->p_fsize;
1791.1Sderaadt			npp->p_frag = opp->p_frag;
1801.1Sderaadt			npp->p_cpg = opp->p_cpg;
1811.1Sderaadt		}
1821.1Sderaadt	}
1831.1Sderaadt 	nlp->d_checksum = 0;
1841.1Sderaadt 	nlp->d_checksum = dkcksum(nlp);
1851.1Sderaadt	*olp = *nlp;
1861.1Sderaadt	return (0);
1871.1Sderaadt}
1881.1Sderaadt
1891.1Sderaadt/* encoding of disk minor numbers, should be elsewhere... */
1901.1Sderaadt#define dkunit(dev)		(minor(dev) >> 3)
1911.1Sderaadt#define dkpart(dev)		(minor(dev) & 07)
1921.1Sderaadt#define dkminor(unit, part)	(((unit) << 3) | (part))
1931.1Sderaadt
1941.1Sderaadt/*
1951.1Sderaadt * Write disk label back to device after modification.
1961.1Sderaadt */
1971.1Sderaadtwritedisklabel(dev, strat, lp, osdep)
1981.1Sderaadt	dev_t dev;
1991.1Sderaadt	void (*strat)();
2001.1Sderaadt	register struct disklabel *lp;
2011.1Sderaadt	struct cpu_disklabel *osdep;
2021.1Sderaadt{
2031.1Sderaadt	struct buf *bp;
2041.1Sderaadt	struct disklabel *dlp;
2051.1Sderaadt	int labelpart;
2061.1Sderaadt	int error = 0;
2071.1Sderaadt
2081.1Sderaadt	labelpart = dkpart(dev);
2091.1Sderaadt	if (lp->d_partitions[labelpart].p_offset != 0) {
2101.1Sderaadt		if (lp->d_partitions[0].p_offset != 0)
2111.1Sderaadt			return (EXDEV);			/* not quite right */
2121.1Sderaadt		labelpart = 0;
2131.1Sderaadt	}
2141.1Sderaadt	bp = geteblk((int)lp->d_secsize);
2151.1Sderaadt	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart));
2161.1Sderaadt	bp->b_blkno = LABELSECTOR;
2171.1Sderaadt	bp->b_bcount = lp->d_secsize;
2181.1Sderaadt	bp->b_flags = B_READ;
2191.1Sderaadt	(*strat)(bp);
2201.1Sderaadt	if (error = biowait(bp))
2211.1Sderaadt		goto done;
2221.1Sderaadt	for (dlp = (struct disklabel *)bp->b_un.b_addr;
2231.1Sderaadt	    dlp <= (struct disklabel *)
2241.1Sderaadt	      (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
2251.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
2261.1Sderaadt		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
2271.1Sderaadt		    dkcksum(dlp) == 0) {
2281.1Sderaadt			*dlp = *lp;
2291.1Sderaadt			bp->b_flags = B_WRITE;
2301.1Sderaadt			(*strat)(bp);
2311.1Sderaadt			error = biowait(bp);
2321.1Sderaadt			goto done;
2331.1Sderaadt		}
2341.1Sderaadt	}
2351.1Sderaadt	error = ESRCH;
2361.1Sderaadtdone:
2371.1Sderaadt	brelse(bp);
2381.1Sderaadt	return (error);
2391.1Sderaadt}
240