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