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