disksubr.c revision 1.1
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.1 1994/01/15 02:04:56 deraadt Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/buf.h>
40#include <sys/disklabel.h>
41#include <sys/syslog.h>
42
43/* XXX encoding of disk minor numbers, should be elsewhere... */
44#define dkunit(dev)		(minor(dev) >> 3)
45#define dkpart(dev)		(minor(dev) & 7)
46#define dkminor(unit, part)	(((unit) << 3) | (part))
47
48#define	b_cylin	b_resid
49
50/*
51 * Attempt to read a disk label from a device
52 * using the indicated stategy routine.
53 * The label must be partly set up before this:
54 * secpercyl and anything required in the strategy routine
55 * (e.g., sector size) must be filled in before calling us.
56 * Returns null on success and an error string on failure.
57 */
58char *
59readdisklabel(dev, strat, lp, osdep)
60	dev_t dev;
61	void (*strat)();
62	register struct disklabel *lp;
63	struct cpu_disklabel *osdep;
64{
65	register struct buf *bp;
66	struct disklabel *dlp;
67	char *msg = NULL;
68
69	if (lp->d_secperunit == 0)
70		lp->d_secperunit = 0x1fffffff;
71	lp->d_npartitions = 1;
72	if (lp->d_partitions[0].p_size == 0)
73		lp->d_partitions[0].p_size = 0x1fffffff;
74	lp->d_partitions[0].p_offset = 0;
75
76	bp = geteblk((int)lp->d_secsize);
77	bp->b_dev = dev;
78	bp->b_blkno = LABELSECTOR;
79	bp->b_bcount = lp->d_secsize;
80	bp->b_flags = B_BUSY | B_READ;
81	bp->b_cylin = LABELSECTOR / lp->d_secpercyl;
82	(*strat)(bp);
83	if (biowait(bp)) {
84		msg = "I/O error";
85	} else for (dlp = (struct disklabel *)bp->b_un.b_addr;
86	    dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
87	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
88		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
89			if (msg == NULL)
90				msg = "no disk label";
91		} else if (dlp->d_npartitions > MAXPARTITIONS ||
92			   dkcksum(dlp) != 0)
93			msg = "disk label corrupted";
94		else {
95			*lp = *dlp;
96			msg = NULL;
97			break;
98		}
99	}
100	bp->b_flags = B_INVAL | B_AGE;
101	brelse(bp);
102	return (msg);
103}
104
105/*
106 * Check new disk label for sensibility
107 * before setting it.
108 */
109setdisklabel(olp, nlp, openmask, osdep)
110	register struct disklabel *olp, *nlp;
111	u_long openmask;
112	struct cpu_disklabel *osdep;
113{
114	register i;
115	register struct partition *opp, *npp;
116
117	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
118	    dkcksum(nlp) != 0)
119		return (EINVAL);
120	while ((i = ffs((long)openmask)) != 0) {
121		i--;
122		openmask &= ~(1 << i);
123		if (nlp->d_npartitions <= i)
124			return (EBUSY);
125		opp = &olp->d_partitions[i];
126		npp = &nlp->d_partitions[i];
127		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
128			return (EBUSY);
129		/*
130		 * Copy internally-set partition information
131		 * if new label doesn't include it.		XXX
132		 */
133		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
134			npp->p_fstype = opp->p_fstype;
135			npp->p_fsize = opp->p_fsize;
136			npp->p_frag = opp->p_frag;
137			npp->p_cpg = opp->p_cpg;
138		}
139	}
140 	nlp->d_checksum = 0;
141 	nlp->d_checksum = dkcksum(nlp);
142	*olp = *nlp;
143	return (0);
144}
145
146/* encoding of disk minor numbers, should be elsewhere... */
147#define dkunit(dev)		(minor(dev) >> 3)
148#define dkpart(dev)		(minor(dev) & 07)
149#define dkminor(unit, part)	(((unit) << 3) | (part))
150
151/*
152 * Write disk label back to device after modification.
153 */
154writedisklabel(dev, strat, lp, osdep)
155	dev_t dev;
156	void (*strat)();
157	register struct disklabel *lp;
158	struct cpu_disklabel *osdep;
159{
160	struct buf *bp;
161	struct disklabel *dlp;
162	int labelpart;
163	int error = 0;
164
165	labelpart = dkpart(dev);
166	if (lp->d_partitions[labelpart].p_offset != 0) {
167		if (lp->d_partitions[0].p_offset != 0)
168			return (EXDEV);			/* not quite right */
169		labelpart = 0;
170	}
171	bp = geteblk((int)lp->d_secsize);
172	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart));
173	bp->b_blkno = LABELSECTOR;
174	bp->b_bcount = lp->d_secsize;
175	bp->b_flags = B_READ;
176	(*strat)(bp);
177	if (error = biowait(bp))
178		goto done;
179	for (dlp = (struct disklabel *)bp->b_un.b_addr;
180	    dlp <= (struct disklabel *)
181	      (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
182	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
183		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
184		    dkcksum(dlp) == 0) {
185			*dlp = *lp;
186			bp->b_flags = B_WRITE;
187			(*strat)(bp);
188			error = biowait(bp);
189			goto done;
190		}
191	}
192	error = ESRCH;
193done:
194	brelse(bp);
195	return (error);
196}
197