disksubr.c revision 1.24
1/*	$NetBSD: disksubr.c,v 1.24 2000/01/09 03:56:01 simonb 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 "opt_compat_ultrix.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/buf.h>
43#include <sys/device.h>
44#include <sys/disk.h>
45#include <sys/disklabel.h>
46#include <sys/syslog.h>
47
48#define	b_cylin	b_resid
49
50#ifdef COMPAT_ULTRIX
51#include <machine/dec_boot.h>
52
53char	*compat_label __P((dev_t dev, void (*strat) __P((struct buf *bp)),
54	    struct disklabel *lp, struct cpu_disklabel *osdep));	/* XXX */
55
56#endif
57
58/*
59 * Attempt to read a disk label from a device
60 * using the indicated stategy routine.
61 * The label must be partly set up before this:
62 * secpercyl and anything required in the strategy routine
63 * (e.g., sector size) must be filled in before calling us.
64 * Returns null on success and an error string on failure.
65 */
66char *
67readdisklabel(dev, strat, lp, osdep)
68	dev_t dev;
69	void (*strat) __P((struct buf *bp));
70	struct disklabel *lp;
71	struct cpu_disklabel *osdep;
72{
73	struct buf *bp;
74	struct disklabel *dlp;
75	char *msg = NULL;
76
77	if (lp->d_secperunit == 0)
78		lp->d_secperunit = 0x1fffffff;
79	lp->d_npartitions = 1;
80	if (lp->d_partitions[0].p_size == 0)
81		lp->d_partitions[0].p_size = 0x1fffffff;
82	lp->d_partitions[0].p_offset = 0;
83
84	bp = geteblk((int)lp->d_secsize);
85	bp->b_dev = dev;
86	bp->b_blkno = LABELSECTOR;
87	bp->b_bcount = lp->d_secsize;
88	bp->b_flags = B_BUSY | B_READ;
89	bp->b_cylin = LABELSECTOR / lp->d_secpercyl;
90	(*strat)(bp);
91	if (biowait(bp)) {
92		msg = "I/O error";
93	} else for (dlp = (struct disklabel *)bp->b_un.b_addr;
94	    dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
95	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
96		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
97			if (msg == NULL)
98				msg = "no disk label";
99		} else if (dlp->d_npartitions > MAXPARTITIONS ||
100			   dkcksum(dlp) != 0)
101			msg = "disk label corrupted";
102		else {
103			*lp = *dlp;
104			msg = NULL;
105			break;
106		}
107	}
108	bp->b_flags = B_INVAL | B_AGE;
109	brelse(bp);
110	return (msg);
111}
112
113#ifdef COMPAT_ULTRIX
114/*
115 * Given a buffer bp, try and interpret it as an Ultrix disk label,
116 * putting the partition info into a native NetBSD label
117 */
118char *
119compat_label(dev, strat, lp, osdep)
120	dev_t dev;
121	void (*strat) __P((struct buf *bp));
122	struct disklabel *lp;
123	struct cpu_disklabel *osdep;
124{
125	dec_disklabel *dlp;
126	struct buf *bp = NULL;
127	char *msg = NULL;
128
129	bp = geteblk((int)lp->d_secsize);
130	bp->b_dev = dev;
131	bp->b_blkno = DEC_LABEL_SECTOR;
132	bp->b_bcount = lp->d_secsize;
133	bp->b_flags = B_BUSY | B_READ;
134	bp->b_cylin = DEC_LABEL_SECTOR / lp->d_secpercyl;
135	(*strat)(bp);
136
137	if (biowait(bp)) {
138                msg = "I/O error";
139		goto done;
140	}
141
142	for (dlp = (dec_disklabel *)bp->b_un.b_addr;
143	     dlp <= (dec_disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
144	     dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) {
145
146		int part;
147
148		if (dlp->magic != DEC_LABEL_MAGIC) {
149			printf("label: %x\n",dlp->magic);
150			msg = ((msg != NULL) ? msg: "no disk label");
151			goto done;
152		}
153
154#ifdef DIAGNOSTIC
155/*XXX*/		printf("Interpreting Ultrix label\n");
156#endif
157
158		lp->d_magic = DEC_LABEL_MAGIC;
159		lp->d_npartitions = 0;
160		for (part = 0;
161		     part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
162			    MAXPARTITIONS : DEC_NUM_DISK_PARTS);
163		     part++) {
164			lp->d_partitions[part].p_size = dlp->map[part].num_blocks;
165			lp->d_partitions[part].p_offset = dlp->map[part].start_block;
166			lp->d_partitions[part].p_fsize = 1024;
167			lp->d_partitions[part].p_fstype =
168			  (part==1) ? FS_SWAP : FS_BSDFFS;
169			lp->d_npartitions += 1;
170
171#ifdef DIAGNOSTIC
172			printf(" Ultrix label rz%d%c: start %d len %d\n",
173			       DISKUNIT(dev), "abcdefgh"[part],
174			       lp->d_partitions[part].p_offset,
175			       lp->d_partitions[part].p_size);
176#endif
177		}
178		break;
179	}
180
181done:
182	bp->b_flags = B_INVAL | B_AGE;
183	brelse(bp);
184	return (msg);
185}
186#endif /* COMPAT_ULTRIX */
187
188
189/*
190 * Check new disk label for sensibility
191 * before setting it.
192 */
193int
194setdisklabel(olp, nlp, openmask, osdep)
195	struct disklabel *olp, *nlp;
196	u_long openmask;
197	struct cpu_disklabel *osdep;
198{
199	int i;
200	struct partition *opp, *npp;
201
202	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
203	    dkcksum(nlp) != 0)
204		return (EINVAL);
205	while ((i = ffs((long)openmask)) != 0) {
206		i--;
207		openmask &= ~(1 << i);
208		if (nlp->d_npartitions <= i)
209			return (EBUSY);
210		opp = &olp->d_partitions[i];
211		npp = &nlp->d_partitions[i];
212		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
213			return (EBUSY);
214		/*
215		 * Copy internally-set partition information
216		 * if new label doesn't include it.		XXX
217		 */
218		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
219			npp->p_fstype = opp->p_fstype;
220			npp->p_fsize = opp->p_fsize;
221			npp->p_frag = opp->p_frag;
222			npp->p_cpg = opp->p_cpg;
223		}
224	}
225 	nlp->d_checksum = 0;
226 	nlp->d_checksum = dkcksum(nlp);
227	*olp = *nlp;
228	return (0);
229}
230
231/* encoding of disk minor numbers, should be elsewhere... */
232#define dkunit(dev)		(minor(dev) >> 3)
233#define dkpart(dev)		(minor(dev) & 07)
234#define dkminor(unit, part)	(((unit) << 3) | (part))
235
236/*
237 * Write disk label back to device after modification.
238 */
239int
240writedisklabel(dev, strat, lp, osdep)
241	dev_t dev;
242	void (*strat) __P((struct buf *bp));
243	struct disklabel *lp;
244	struct cpu_disklabel *osdep;
245{
246	struct buf *bp;
247	struct disklabel *dlp;
248	int labelpart;
249	int error = 0;
250
251	labelpart = dkpart(dev);
252	if (lp->d_partitions[labelpart].p_offset != 0) {
253		if (lp->d_partitions[0].p_offset != 0)
254			return (EXDEV);			/* not quite right */
255		labelpart = 0;
256	}
257	bp = geteblk((int)lp->d_secsize);
258	bp->b_dev = makedev(major(dev), dkminor(dkunit(dev), labelpart));
259	bp->b_blkno = LABELSECTOR;
260	bp->b_bcount = lp->d_secsize;
261	bp->b_flags = B_READ;
262	(*strat)(bp);
263	if ((error = biowait(bp)) != 0)
264		goto done;
265	for (dlp = (struct disklabel *)bp->b_un.b_addr;
266	    dlp <= (struct disklabel *)
267	      (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
268	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
269		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
270		    dkcksum(dlp) == 0) {
271			*dlp = *lp;
272			bp->b_flags = B_WRITE;
273			(*strat)(bp);
274			error = biowait(bp);
275			goto done;
276		}
277	}
278	error = ESRCH;
279done:
280	brelse(bp);
281	return (error);
282}
283
284
285/*
286 * was this the boot device ?
287 */
288void
289dk_establish(dk, dev)
290	struct disk *dk;
291	struct device *dev;
292{
293	/* see also arch/alpha/alpha/disksubr.c */
294	printf("Warning: boot path unknown\n");
295	return;
296}
297
298/*
299 * UNTESTED !!
300 *
301 * Determine the size of the transfer, and make sure it is
302 * within the boundaries of the partition. Adjust transfer
303 * if needed, and signal errors or early completion.
304 */
305int
306bounds_check_with_label(bp, lp, wlabel)
307	struct buf *bp;
308	struct disklabel *lp;
309	int wlabel;
310{
311
312	struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
313	int labelsect = lp->d_partitions[RAW_PART].p_offset;
314	int maxsz = p->p_size;
315	int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
316
317	/* overwriting disk label ? */
318	/* XXX should also protect bootstrap in first 8K */
319	if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
320	    (bp->b_flags & B_READ) == 0 && wlabel == 0) {
321		bp->b_error = EROFS;
322		goto bad;
323	}
324
325	/* beyond partition? */
326	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
327		/* if exactly at end of disk, return an EOF */
328		if (bp->b_blkno == maxsz) {
329			bp->b_resid = bp->b_bcount;
330			return(0);
331		}
332		/* or truncate if part of it fits */
333		sz = maxsz - bp->b_blkno;
334		if (sz <= 0) {
335			bp->b_error = EINVAL;
336			goto bad;
337		}
338		bp->b_bcount = sz << DEV_BSHIFT;
339	}
340
341	/* calculate cylinder for disksort to order transfers with */
342	bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
343	return(1);
344bad:
345	bp->b_flags |= B_ERROR;
346	return(-1);
347}
348