disksubr.c revision 1.2
11.1Sderaadt/*
21.1Sderaadt * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
31.1Sderaadt * All rights reserved.
41.1Sderaadt *
51.1Sderaadt * Redistribution and use in source and binary forms, with or without
61.1Sderaadt * modification, are permitted provided that the following conditions
71.1Sderaadt * are met:
81.1Sderaadt * 1. Redistributions of source code must retain the above copyright
91.1Sderaadt *    notice, this list of conditions and the following disclaimer.
101.1Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
111.1Sderaadt *    notice, this list of conditions and the following disclaimer in the
121.1Sderaadt *    documentation and/or other materials provided with the distribution.
131.1Sderaadt * 3. All advertising materials mentioning features or use of this software
141.1Sderaadt *    must display the following acknowledgement:
151.1Sderaadt *	This product includes software developed by the University of
161.1Sderaadt *	California, Berkeley and its contributors.
171.1Sderaadt * 4. Neither the name of the University nor the names of its contributors
181.1Sderaadt *    may be used to endorse or promote products derived from this software
191.1Sderaadt *    without specific prior written permission.
201.1Sderaadt *
211.1Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221.1Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231.1Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241.1Sderaadt * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251.1Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261.1Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271.1Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281.1Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291.1Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301.1Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311.1Sderaadt * SUCH DAMAGE.
321.1Sderaadt *
331.1Sderaadt *	from: @(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
341.2Sglass *	$Id: disksubr.c,v 1.2 1994/05/27 09:03:09 glass Exp $
351.1Sderaadt */
361.1Sderaadt
371.2Sglass#include "param.h"
381.2Sglass#include "systm.h"
391.2Sglass#include "buf.h"
401.2Sglass#include "disklabel.h"
411.2Sglass#include "syslog.h"
421.1Sderaadt
431.1Sderaadt#define	b_cylin	b_resid
441.1Sderaadt
451.1Sderaadt/*
461.1Sderaadt * Attempt to read a disk label from a device
471.1Sderaadt * using the indicated stategy routine.
481.1Sderaadt * The label must be partly set up before this:
491.1Sderaadt * secpercyl and anything required in the strategy routine
501.1Sderaadt * (e.g., sector size) must be filled in before calling us.
511.1Sderaadt * Returns null on success and an error string on failure.
521.1Sderaadt */
531.1Sderaadtchar *
541.1Sderaadtreaddisklabel(dev, strat, lp, osdep)
551.1Sderaadt	dev_t dev;
561.1Sderaadt	void (*strat)();
571.1Sderaadt	register struct disklabel *lp;
581.1Sderaadt	struct cpu_disklabel *osdep;
591.1Sderaadt{
601.1Sderaadt	register struct buf *bp;
611.1Sderaadt	struct disklabel *dlp;
621.1Sderaadt	char *msg = NULL;
631.1Sderaadt
641.1Sderaadt	if (lp->d_secperunit == 0)
651.1Sderaadt		lp->d_secperunit = 0x1fffffff;
661.1Sderaadt	lp->d_npartitions = 1;
671.1Sderaadt	if (lp->d_partitions[0].p_size == 0)
681.1Sderaadt		lp->d_partitions[0].p_size = 0x1fffffff;
691.1Sderaadt	lp->d_partitions[0].p_offset = 0;
701.1Sderaadt
711.1Sderaadt	bp = geteblk((int)lp->d_secsize);
721.1Sderaadt	bp->b_dev = dev;
731.1Sderaadt	bp->b_blkno = LABELSECTOR;
741.1Sderaadt	bp->b_bcount = lp->d_secsize;
751.1Sderaadt	bp->b_flags = B_BUSY | B_READ;
761.1Sderaadt	bp->b_cylin = LABELSECTOR / lp->d_secpercyl;
771.1Sderaadt	(*strat)(bp);
781.1Sderaadt	if (biowait(bp)) {
791.1Sderaadt		msg = "I/O error";
801.1Sderaadt	} else for (dlp = (struct disklabel *)bp->b_un.b_addr;
811.1Sderaadt	    dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
821.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
831.1Sderaadt		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
841.1Sderaadt			if (msg == NULL)
851.1Sderaadt				msg = "no disk label";
861.1Sderaadt		} else if (dlp->d_npartitions > MAXPARTITIONS ||
871.1Sderaadt			   dkcksum(dlp) != 0)
881.1Sderaadt			msg = "disk label corrupted";
891.1Sderaadt		else {
901.1Sderaadt			*lp = *dlp;
911.1Sderaadt			msg = NULL;
921.1Sderaadt			break;
931.1Sderaadt		}
941.1Sderaadt	}
951.1Sderaadt	bp->b_flags = B_INVAL | B_AGE;
961.1Sderaadt	brelse(bp);
971.1Sderaadt	return (msg);
981.1Sderaadt}
991.1Sderaadt
1001.1Sderaadt/*
1011.1Sderaadt * Check new disk label for sensibility
1021.1Sderaadt * before setting it.
1031.1Sderaadt */
1041.1Sderaadtsetdisklabel(olp, nlp, openmask, osdep)
1051.1Sderaadt	register struct disklabel *olp, *nlp;
1061.1Sderaadt	u_long openmask;
1071.1Sderaadt	struct cpu_disklabel *osdep;
1081.1Sderaadt{
1091.1Sderaadt	register i;
1101.1Sderaadt	register struct partition *opp, *npp;
1111.1Sderaadt
1121.1Sderaadt	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
1131.1Sderaadt	    dkcksum(nlp) != 0)
1141.1Sderaadt		return (EINVAL);
1151.1Sderaadt	while ((i = ffs((long)openmask)) != 0) {
1161.1Sderaadt		i--;
1171.1Sderaadt		openmask &= ~(1 << i);
1181.1Sderaadt		if (nlp->d_npartitions <= i)
1191.1Sderaadt			return (EBUSY);
1201.1Sderaadt		opp = &olp->d_partitions[i];
1211.1Sderaadt		npp = &nlp->d_partitions[i];
1221.1Sderaadt		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
1231.1Sderaadt			return (EBUSY);
1241.1Sderaadt		/*
1251.1Sderaadt		 * Copy internally-set partition information
1261.1Sderaadt		 * if new label doesn't include it.		XXX
1271.1Sderaadt		 */
1281.1Sderaadt		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
1291.1Sderaadt			npp->p_fstype = opp->p_fstype;
1301.1Sderaadt			npp->p_fsize = opp->p_fsize;
1311.1Sderaadt			npp->p_frag = opp->p_frag;
1321.1Sderaadt			npp->p_cpg = opp->p_cpg;
1331.1Sderaadt		}
1341.1Sderaadt	}
1351.1Sderaadt 	nlp->d_checksum = 0;
1361.1Sderaadt 	nlp->d_checksum = dkcksum(nlp);
1371.1Sderaadt	*olp = *nlp;
1381.1Sderaadt	return (0);
1391.1Sderaadt}
1401.1Sderaadt
1411.1Sderaadt/* encoding of disk minor numbers, should be elsewhere... */
1421.1Sderaadt#define dkunit(dev)		(minor(dev) >> 3)
1431.1Sderaadt#define dkpart(dev)		(minor(dev) & 07)
1441.1Sderaadt#define dkminor(unit, part)	(((unit) << 3) | (part))
1451.1Sderaadt
1461.1Sderaadt/*
1471.1Sderaadt * Write disk label back to device after modification.
1481.1Sderaadt */
1491.1Sderaadtwritedisklabel(dev, strat, lp, osdep)
1501.1Sderaadt	dev_t dev;
1511.1Sderaadt	void (*strat)();
1521.1Sderaadt	register struct disklabel *lp;
1531.1Sderaadt	struct cpu_disklabel *osdep;
1541.1Sderaadt{
1551.1Sderaadt	struct buf *bp;
1561.1Sderaadt	struct disklabel *dlp;
1571.1Sderaadt	int labelpart;
1581.1Sderaadt	int error = 0;
1591.1Sderaadt
1601.1Sderaadt	labelpart = dkpart(dev);
1611.1Sderaadt	if (lp->d_partitions[labelpart].p_offset != 0) {
1621.1Sderaadt		if (lp->d_partitions[0].p_offset != 0)
1631.1Sderaadt			return (EXDEV);			/* not quite right */
1641.1Sderaadt		labelpart = 0;
1651.1Sderaadt	}
1661.1Sderaadt	bp = geteblk((int)lp->d_secsize);
1671.1Sderaadt	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart));
1681.1Sderaadt	bp->b_blkno = LABELSECTOR;
1691.1Sderaadt	bp->b_bcount = lp->d_secsize;
1701.1Sderaadt	bp->b_flags = B_READ;
1711.1Sderaadt	(*strat)(bp);
1721.1Sderaadt	if (error = biowait(bp))
1731.1Sderaadt		goto done;
1741.1Sderaadt	for (dlp = (struct disklabel *)bp->b_un.b_addr;
1751.1Sderaadt	    dlp <= (struct disklabel *)
1761.1Sderaadt	      (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
1771.1Sderaadt	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
1781.1Sderaadt		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
1791.1Sderaadt		    dkcksum(dlp) == 0) {
1801.1Sderaadt			*dlp = *lp;
1811.1Sderaadt			bp->b_flags = B_WRITE;
1821.1Sderaadt			(*strat)(bp);
1831.1Sderaadt			error = biowait(bp);
1841.1Sderaadt			goto done;
1851.1Sderaadt		}
1861.1Sderaadt	}
1871.1Sderaadt	error = ESRCH;
1881.1Sderaadtdone:
1891.1Sderaadt	brelse(bp);
1901.1Sderaadt	return (error);
1911.1Sderaadt}
192