disksubr.c revision 1.3
1/*	$NetBSD: disksubr.c,v 1.3 1994/10/26 21:10:20 cgd Exp $	*/
2
3/*
4 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
36 */
37
38#include "param.h"
39#include "systm.h"
40#include "buf.h"
41#include "disklabel.h"
42#include "syslog.h"
43
44#define	b_cylin	b_resid
45
46/*
47 * Attempt to read a disk label from a device
48 * using the indicated stategy routine.
49 * The label must be partly set up before this:
50 * secpercyl and anything required in the strategy routine
51 * (e.g., sector size) must be filled in before calling us.
52 * Returns null on success and an error string on failure.
53 */
54char *
55readdisklabel(dev, strat, lp, osdep)
56	dev_t dev;
57	void (*strat)();
58	register struct disklabel *lp;
59	struct cpu_disklabel *osdep;
60{
61	register struct buf *bp;
62	struct disklabel *dlp;
63	char *msg = NULL;
64
65	if (lp->d_secperunit == 0)
66		lp->d_secperunit = 0x1fffffff;
67	lp->d_npartitions = 1;
68	if (lp->d_partitions[0].p_size == 0)
69		lp->d_partitions[0].p_size = 0x1fffffff;
70	lp->d_partitions[0].p_offset = 0;
71
72	bp = geteblk((int)lp->d_secsize);
73	bp->b_dev = dev;
74	bp->b_blkno = LABELSECTOR;
75	bp->b_bcount = lp->d_secsize;
76	bp->b_flags = B_BUSY | B_READ;
77	bp->b_cylin = LABELSECTOR / lp->d_secpercyl;
78	(*strat)(bp);
79	if (biowait(bp)) {
80		msg = "I/O error";
81	} else for (dlp = (struct disklabel *)bp->b_un.b_addr;
82	    dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
83	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
84		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
85			if (msg == NULL)
86				msg = "no disk label";
87		} else if (dlp->d_npartitions > MAXPARTITIONS ||
88			   dkcksum(dlp) != 0)
89			msg = "disk label corrupted";
90		else {
91			*lp = *dlp;
92			msg = NULL;
93			break;
94		}
95	}
96	bp->b_flags = B_INVAL | B_AGE;
97	brelse(bp);
98	return (msg);
99}
100
101/*
102 * Check new disk label for sensibility
103 * before setting it.
104 */
105setdisklabel(olp, nlp, openmask, osdep)
106	register struct disklabel *olp, *nlp;
107	u_long openmask;
108	struct cpu_disklabel *osdep;
109{
110	register i;
111	register struct partition *opp, *npp;
112
113	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
114	    dkcksum(nlp) != 0)
115		return (EINVAL);
116	while ((i = ffs((long)openmask)) != 0) {
117		i--;
118		openmask &= ~(1 << i);
119		if (nlp->d_npartitions <= i)
120			return (EBUSY);
121		opp = &olp->d_partitions[i];
122		npp = &nlp->d_partitions[i];
123		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
124			return (EBUSY);
125		/*
126		 * Copy internally-set partition information
127		 * if new label doesn't include it.		XXX
128		 */
129		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
130			npp->p_fstype = opp->p_fstype;
131			npp->p_fsize = opp->p_fsize;
132			npp->p_frag = opp->p_frag;
133			npp->p_cpg = opp->p_cpg;
134		}
135	}
136 	nlp->d_checksum = 0;
137 	nlp->d_checksum = dkcksum(nlp);
138	*olp = *nlp;
139	return (0);
140}
141
142/* encoding of disk minor numbers, should be elsewhere... */
143#define dkunit(dev)		(minor(dev) >> 3)
144#define dkpart(dev)		(minor(dev) & 07)
145#define dkminor(unit, part)	(((unit) << 3) | (part))
146
147/*
148 * Write disk label back to device after modification.
149 */
150writedisklabel(dev, strat, lp, osdep)
151	dev_t dev;
152	void (*strat)();
153	register struct disklabel *lp;
154	struct cpu_disklabel *osdep;
155{
156	struct buf *bp;
157	struct disklabel *dlp;
158	int labelpart;
159	int error = 0;
160
161	labelpart = dkpart(dev);
162	if (lp->d_partitions[labelpart].p_offset != 0) {
163		if (lp->d_partitions[0].p_offset != 0)
164			return (EXDEV);			/* not quite right */
165		labelpart = 0;
166	}
167	bp = geteblk((int)lp->d_secsize);
168	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart));
169	bp->b_blkno = LABELSECTOR;
170	bp->b_bcount = lp->d_secsize;
171	bp->b_flags = B_READ;
172	(*strat)(bp);
173	if (error = biowait(bp))
174		goto done;
175	for (dlp = (struct disklabel *)bp->b_un.b_addr;
176	    dlp <= (struct disklabel *)
177	      (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
178	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
179		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
180		    dkcksum(dlp) == 0) {
181			*dlp = *lp;
182			bp->b_flags = B_WRITE;
183			(*strat)(bp);
184			error = biowait(bp);
185			goto done;
186		}
187	}
188	error = ESRCH;
189done:
190	brelse(bp);
191	return (error);
192}
193