disksubr.c revision 1.5
1/*	$NetBSD: disksubr.c,v 1.5 1995/07/14 01:05:22 jonathan 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#ifdef COMPAT_ULTRIX
47#include "../../stand/dec_boot.h"
48
49extern char *
50compat_label __P((dev_t dev, void (*strat)(),
51		  struct disklabel *lp, struct cpu_disklabel *osdep));
52
53#endif
54
55
56/*
57 * Attempt to read a disk label from a device
58 * using the indicated stategy routine.
59 * The label must be partly set up before this:
60 * secpercyl and anything required in the strategy routine
61 * (e.g., sector size) must be filled in before calling us.
62 * Returns null on success and an error string on failure.
63 */
64char *
65readdisklabel(dev, strat, lp, osdep)
66	dev_t dev;
67	void (*strat)();
68	register struct disklabel *lp;
69	struct cpu_disklabel *osdep;
70{
71	register struct buf *bp;
72	struct disklabel *dlp;
73	char *msg = NULL;
74
75	if (lp->d_secperunit == 0)
76		lp->d_secperunit = 0x1fffffff;
77	lp->d_npartitions = 1;
78	if (lp->d_partitions[0].p_size == 0)
79		lp->d_partitions[0].p_size = 0x1fffffff;
80	lp->d_partitions[0].p_offset = 0;
81
82	bp = geteblk((int)lp->d_secsize);
83	bp->b_dev = dev;
84	bp->b_blkno = LABELSECTOR;
85	bp->b_bcount = lp->d_secsize;
86	bp->b_flags = B_BUSY | B_READ;
87	bp->b_cylin = LABELSECTOR / lp->d_secpercyl;
88	(*strat)(bp);
89	if (biowait(bp)) {
90		msg = "I/O error";
91	} else for (dlp = (struct disklabel *)bp->b_un.b_addr;
92	    dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
93	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
94		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
95			if (msg == NULL)
96				msg = "no disk label";
97		} else if (dlp->d_npartitions > MAXPARTITIONS ||
98			   dkcksum(dlp) != 0)
99			msg = "disk label corrupted";
100		else {
101			*lp = *dlp;
102			msg = NULL;
103			break;
104		}
105	}
106	bp->b_flags = B_INVAL | B_AGE;
107	brelse(bp);
108	return (msg);
109}
110
111#ifdef COMPAT_ULTRIX
112/*
113 * Given a buffer bp, try and interpret it as an Ultrix disk label,
114 * putting the partition info into a native NetBSD label
115 */
116char *
117compat_label(dev, strat, lp, osdep)
118	dev_t dev;
119	void (*strat)();
120	register struct disklabel *lp;
121	struct cpu_disklabel *osdep;
122{
123	Dec_DiskLabel *dlp;
124	struct buf *bp = NULL;
125	char *msg = NULL;
126
127	bp = geteblk((int)lp->d_secsize);
128	bp->b_dev = dev;
129	bp->b_blkno = DEC_LABEL_SECTOR;
130	bp->b_bcount = lp->d_secsize;
131	bp->b_flags = B_BUSY | B_READ;
132	bp->b_cylin = DEC_LABEL_SECTOR / lp->d_secpercyl;
133	(*strat)(bp);
134
135	if (biowait(bp)) {
136                msg = "I/O error";
137		goto done;
138	}
139
140	for (dlp = (Dec_DiskLabel *)bp->b_un.b_addr;
141	     dlp <= (Dec_DiskLabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
142	     dlp = (Dec_DiskLabel *)((char *)dlp + sizeof(long))) {
143
144		int part;
145
146		if (dlp->magic != DEC_LABEL_MAGIC) {
147			printf("label: %x\n",dlp->magic);
148			msg = ((msg != NULL) ? msg: "no disk label");
149			goto done;
150		}
151
152/*XXX*/		printf("Ultrix label loop\n");
153
154		lp->d_magic = DEC_LABEL_MAGIC;
155		lp->d_npartitions = 0;
156		for (part = 0;
157		     part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
158			    MAXPARTITIONS : DEC_NUM_DISK_PARTS);
159		     part++) {
160			lp->d_partitions[part].p_size = dlp->map[part].numBlocks;
161			lp->d_partitions[part].p_offset = dlp->map[part].startBlock;
162			lp->d_partitions[part].p_fsize = 1024;
163			lp->d_partitions[part].p_fstype =
164			  (part==1) ? FS_SWAP : FS_BSDFFS;
165			lp->d_npartitions += 1;
166
167#ifdef DEBUG
168			printf(" Ultrix label rz%d%c: start %d len %d\n",
169			       DISKUNIT(dev), "abcdefgh"[part],
170			       lp->d_partitions[part].p_offset,
171			       lp->d_partitions[part].p_size);
172#endif
173		}
174		break;
175	}
176
177done:
178	bp->b_flags = B_INVAL | B_AGE;
179	brelse(bp);
180	return (msg);
181}
182#endif /* COMPAT_ULTRIX */
183
184
185/*
186 * Check new disk label for sensibility
187 * before setting it.
188 */
189setdisklabel(olp, nlp, openmask, osdep)
190	register struct disklabel *olp, *nlp;
191	u_long openmask;
192	struct cpu_disklabel *osdep;
193{
194	register i;
195	register struct partition *opp, *npp;
196
197	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
198	    dkcksum(nlp) != 0)
199		return (EINVAL);
200	while ((i = ffs((long)openmask)) != 0) {
201		i--;
202		openmask &= ~(1 << i);
203		if (nlp->d_npartitions <= i)
204			return (EBUSY);
205		opp = &olp->d_partitions[i];
206		npp = &nlp->d_partitions[i];
207		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
208			return (EBUSY);
209		/*
210		 * Copy internally-set partition information
211		 * if new label doesn't include it.		XXX
212		 */
213		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
214			npp->p_fstype = opp->p_fstype;
215			npp->p_fsize = opp->p_fsize;
216			npp->p_frag = opp->p_frag;
217			npp->p_cpg = opp->p_cpg;
218		}
219	}
220 	nlp->d_checksum = 0;
221 	nlp->d_checksum = dkcksum(nlp);
222	*olp = *nlp;
223	return (0);
224}
225
226/* encoding of disk minor numbers, should be elsewhere... */
227#define dkunit(dev)		(minor(dev) >> 3)
228#define dkpart(dev)		(minor(dev) & 07)
229#define dkminor(unit, part)	(((unit) << 3) | (part))
230
231/*
232 * Write disk label back to device after modification.
233 */
234writedisklabel(dev, strat, lp, osdep)
235	dev_t dev;
236	void (*strat)();
237	register struct disklabel *lp;
238	struct cpu_disklabel *osdep;
239{
240	struct buf *bp;
241	struct disklabel *dlp;
242	int labelpart;
243	int error = 0;
244
245	labelpart = dkpart(dev);
246	if (lp->d_partitions[labelpart].p_offset != 0) {
247		if (lp->d_partitions[0].p_offset != 0)
248			return (EXDEV);			/* not quite right */
249		labelpart = 0;
250	}
251	bp = geteblk((int)lp->d_secsize);
252	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart));
253	bp->b_blkno = LABELSECTOR;
254	bp->b_bcount = lp->d_secsize;
255	bp->b_flags = B_READ;
256	(*strat)(bp);
257	if (error = biowait(bp))
258		goto done;
259	for (dlp = (struct disklabel *)bp->b_un.b_addr;
260	    dlp <= (struct disklabel *)
261	      (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
262	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
263		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
264		    dkcksum(dlp) == 0) {
265			*dlp = *lp;
266			bp->b_flags = B_WRITE;
267			(*strat)(bp);
268			error = biowait(bp);
269			goto done;
270		}
271	}
272	error = ESRCH;
273done:
274	brelse(bp);
275	return (error);
276}
277