disksubr.c revision 1.45
1/*	$NetBSD: disksubr.c,v 1.45 2007/03/06 22:31:36 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. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
32 */
33
34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.45 2007/03/06 22:31:36 simonb Exp $");
36
37#include "opt_compat_ultrix.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/buf.h>
42#include <sys/disk.h>
43#include <sys/disklabel.h>
44
45#ifdef COMPAT_ULTRIX
46#include <dev/dec/dec_boot.h>
47#include <ufs/ufs/dinode.h>		/* XXX for fs.h */
48#include <ufs/ffs/fs.h>			/* XXX for BBSIZE & SBSIZE */
49
50const char *compat_label __P((dev_t dev, void (*strat) __P((struct buf *bp)),
51	struct disklabel *lp, struct cpu_disklabel *osdep));	/* XXX */
52
53#endif
54
55/*
56 * Attempt to read a disk label from a device
57 * using the indicated strategy routine.
58 * The label must be partly set up before this:
59 * secpercyl and anything required in the strategy routine
60 * (e.g., sector size) must be filled in before calling us.
61 * Returns null on success and an error string on failure.
62 */
63const char *
64readdisklabel(dev, strat, lp, osdep)
65	dev_t dev;
66	void (*strat) __P((struct buf *bp));
67	struct disklabel *lp;
68	struct cpu_disklabel *osdep;
69{
70	struct buf *bp;
71	struct disklabel *dlp;
72	const char *msg = NULL;
73
74	if (lp->d_secperunit == 0)
75		lp->d_secperunit = 0x1fffffff;
76	lp->d_npartitions = 1;
77	if (lp->d_partitions[0].p_size == 0)
78		lp->d_partitions[0].p_size = 0x1fffffff;
79	lp->d_partitions[0].p_offset = 0;
80
81	bp = geteblk((int)lp->d_secsize);
82	bp->b_dev = dev;
83	bp->b_blkno = LABELSECTOR;
84	bp->b_bcount = lp->d_secsize;
85	bp->b_flags |= B_READ;
86	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
87	(*strat)(bp);
88	if (biowait(bp)) {
89		msg = "I/O error";
90	} else for (dlp = (struct disklabel *)bp->b_data;
91	    dlp <= (struct disklabel *)
92	    ((char *)bp->b_data + 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	brelse(bp);
107#ifdef COMPAT_ULTRIX
108	/*
109	 * If no NetBSD label was found, check for an Ultrix label and
110	 * construct tne incore label from the Ultrix partition information.
111	 */
112	if (msg != NULL) {
113		msg = compat_label(dev, strat, lp, osdep);
114		if (msg == NULL) {
115			printf("WARNING: using Ultrix partition information\n");
116			/* set geometry? */
117		}
118	}
119#endif
120/* XXX If no NetBSD label or Ultrix label found, generate default label here */
121	return (msg);
122}
123
124#ifdef COMPAT_ULTRIX
125/*
126 * Given a buffer bp, try and interpret it as an Ultrix disk label,
127 * putting the partition info into a native NetBSD label
128 */
129const char *
130compat_label(dev, strat, lp, osdep)
131	dev_t dev;
132	void (*strat) __P((struct buf *bp));
133	struct disklabel *lp;
134	struct cpu_disklabel *osdep;
135{
136	dec_disklabel *dlp;
137	struct buf *bp = NULL;
138	const char *msg = NULL;
139
140	bp = geteblk((int)lp->d_secsize);
141	bp->b_dev = dev;
142	bp->b_blkno = DEC_LABEL_SECTOR;
143	bp->b_bcount = lp->d_secsize;
144	bp->b_flags |= B_READ;
145	bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl;
146	(*strat)(bp);
147
148	if (biowait(bp)) {
149                msg = "I/O error";
150		goto done;
151	}
152
153	for (dlp = (dec_disklabel *)bp->b_data;
154	     dlp <= (dec_disklabel *)
155	     ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp));
156	     dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) {
157
158		int part;
159
160		if (dlp->magic != DEC_LABEL_MAGIC) {
161			printf("label: %x\n",dlp->magic);
162			msg = ((msg != NULL) ? msg: "no disk label");
163			goto done;
164		}
165
166#ifdef DIAGNOSTIC
167/*XXX*/		printf("Interpreting Ultrix label\n");
168#endif
169
170		lp->d_magic = DEC_LABEL_MAGIC;
171		lp->d_npartitions = 0;
172		strncpy(lp->d_packname, "Ultrix label", 16);
173		lp->d_rpm = 3600;
174		lp->d_interleave = 1;
175		lp->d_flags = 0;
176		lp->d_bbsize = BBSIZE;
177		lp->d_sbsize = SBLOCKSIZE;
178		for (part = 0;
179		     part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
180			    MAXPARTITIONS : DEC_NUM_DISK_PARTS);
181		     part++) {
182			lp->d_partitions[part].p_size = dlp->map[part].num_blocks;
183			lp->d_partitions[part].p_offset = dlp->map[part].start_block;
184			lp->d_partitions[part].p_fsize = 1024;
185			lp->d_partitions[part].p_fstype =
186			  (part==1) ? FS_SWAP : FS_BSDFFS;
187			lp->d_npartitions += 1;
188
189#ifdef DIAGNOSTIC
190			printf(" Ultrix label rz%d%c: start %d len %d\n",
191			       DISKUNIT(dev), "abcdefgh"[part],
192			       lp->d_partitions[part].p_offset,
193			       lp->d_partitions[part].p_size);
194#endif
195		}
196		break;
197	}
198
199done:
200	brelse(bp);
201	return (msg);
202}
203#endif /* COMPAT_ULTRIX */
204
205
206/*
207 * Check new disk label for sensibility
208 * before setting it.
209 */
210int
211setdisklabel(olp, nlp, openmask, osdep)
212	struct disklabel *olp, *nlp;
213	u_long openmask;
214	struct cpu_disklabel *osdep;
215{
216	int i;
217	struct partition *opp, *npp;
218
219	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
220	    dkcksum(nlp) != 0)
221		return (EINVAL);
222	while ((i = ffs(openmask)) != 0) {
223		i--;
224		openmask &= ~(1 << i);
225		if (nlp->d_npartitions <= i)
226			return (EBUSY);
227		opp = &olp->d_partitions[i];
228		npp = &nlp->d_partitions[i];
229		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
230			return (EBUSY);
231		/*
232		 * Copy internally-set partition information
233		 * if new label doesn't include it.		XXX
234		 */
235		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
236			npp->p_fstype = opp->p_fstype;
237			npp->p_fsize = opp->p_fsize;
238			npp->p_frag = opp->p_frag;
239			npp->p_cpg = opp->p_cpg;
240		}
241	}
242 	nlp->d_checksum = 0;
243 	nlp->d_checksum = dkcksum(nlp);
244	*olp = *nlp;
245	return (0);
246}
247
248/*
249 * Write disk label back to device after modification.
250 */
251int
252writedisklabel(dev, strat, lp, osdep)
253	dev_t dev;
254	void (*strat) __P((struct buf *bp));
255	struct disklabel *lp;
256	struct cpu_disklabel *osdep;
257{
258	struct buf *bp;
259	struct disklabel *dlp;
260	int labelpart;
261	int error = 0;
262
263	labelpart = DISKPART(dev);
264	if (lp->d_partitions[labelpart].p_offset != 0) {
265		if (lp->d_partitions[0].p_offset != 0)
266			return (EXDEV);			/* not quite right */
267		labelpart = 0;
268	}
269	bp = geteblk((int)lp->d_secsize);
270	bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart));
271	bp->b_blkno = LABELSECTOR;
272	bp->b_bcount = lp->d_secsize;
273	bp->b_flags |= B_READ;
274	(*strat)(bp);
275	if ((error = biowait(bp)) != 0)
276		goto done;
277	for (dlp = (struct disklabel *)bp->b_data;
278	    dlp <= (struct disklabel *)
279	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
280	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
281		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
282		    dkcksum(dlp) == 0) {
283			*dlp = *lp;
284			bp->b_flags &= ~(B_READ|B_DONE);
285			bp->b_flags |= B_WRITE;
286			(*strat)(bp);
287			error = biowait(bp);
288			goto done;
289		}
290	}
291	error = ESRCH;
292done:
293	brelse(bp);
294	return (error);
295}
296