disksubr.c revision 1.50
1/*	$NetBSD: disksubr.c,v 1.50 2009/03/14 14:46:04 dsl 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.50 2009/03/14 14:46:04 dsl Exp $");
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/buf.h>
40#include <sys/disk.h>
41#include <sys/disklabel.h>
42
43#include <dev/dec/dec_boot.h>
44#include <ufs/ufs/dinode.h>		/* XXX for fs.h */
45#include <ufs/ffs/fs.h>			/* XXX for BBSIZE & SBSIZE */
46
47const char *compat_label(dev_t dev, void (*strat)(struct buf *bp),
48	struct disklabel *lp, struct cpu_disklabel *osdep);	/* XXX */
49
50/*
51 * Attempt to read a disk label from a device
52 * using the indicated strategy 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 */
58const char *
59readdisklabel(dev, strat, lp, osdep)
60	dev_t dev;
61	void (*strat)(struct buf *bp);
62	struct disklabel *lp;
63	struct cpu_disklabel *osdep;
64{
65	struct buf *bp;
66	struct disklabel *dlp;
67	const 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_READ;
81	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
82	(*strat)(bp);
83	if (biowait(bp)) {
84		msg = "I/O error";
85	} else for (dlp = (struct disklabel *)bp->b_data;
86	    dlp <= (struct disklabel *)
87	    ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp));
88	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
89		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
90			if (msg == NULL)
91				msg = "no disk label";
92		} else if (dlp->d_npartitions > MAXPARTITIONS ||
93			   dkcksum(dlp) != 0)
94			msg = "disk label corrupted";
95		else {
96			*lp = *dlp;
97			msg = NULL;
98			break;
99		}
100	}
101	brelse(bp, 0);
102	/*
103	 * If no NetBSD label was found, check for an Ultrix label and
104	 * construct tne incore label from the Ultrix partition information.
105	 */
106	if (msg != NULL) {
107		msg = compat_label(dev, strat, lp, osdep);
108		if (msg == NULL) {
109			printf("WARNING: using Ultrix partition information\n");
110			/* set geometry? */
111		}
112	}
113/* XXX If no NetBSD label or Ultrix label found, generate default label here */
114	return (msg);
115}
116
117/*
118 * Given a buffer bp, try and interpret it as an Ultrix disk label,
119 * putting the partition info into a native NetBSD label
120 */
121const char *
122compat_label(dev, strat, lp, osdep)
123	dev_t dev;
124	void (*strat)(struct buf *bp);
125	struct disklabel *lp;
126	struct cpu_disklabel *osdep;
127{
128	dec_disklabel *dlp;
129	struct buf *bp = NULL;
130	const char *msg = NULL;
131
132	bp = geteblk((int)lp->d_secsize);
133	bp->b_dev = dev;
134	bp->b_blkno = DEC_LABEL_SECTOR;
135	bp->b_bcount = lp->d_secsize;
136	bp->b_flags |= B_READ;
137	bp->b_cylinder = DEC_LABEL_SECTOR / lp->d_secpercyl;
138	(*strat)(bp);
139
140	if (biowait(bp)) {
141                msg = "I/O error";
142		goto done;
143	}
144
145	for (dlp = (dec_disklabel *)bp->b_data;
146	     dlp <= (dec_disklabel *)
147	     ((char *)bp->b_data + DEV_BSIZE - sizeof(*dlp));
148	     dlp = (dec_disklabel *)((char *)dlp + sizeof(long))) {
149
150		int part;
151
152		if (dlp->magic != DEC_LABEL_MAGIC) {
153			printf("label: %x\n",dlp->magic);
154			msg = ((msg != NULL) ? msg: "no disk label");
155			goto done;
156		}
157
158#ifdef DIAGNOSTIC
159/*XXX*/		printf("Interpreting Ultrix label\n");
160#endif
161
162		lp->d_magic = DEC_LABEL_MAGIC;
163		lp->d_npartitions = 0;
164		strncpy(lp->d_packname, "Ultrix label", 16);
165		lp->d_rpm = 3600;
166		lp->d_interleave = 1;
167		lp->d_flags = 0;
168		lp->d_bbsize = BBSIZE;
169		lp->d_sbsize = SBLOCKSIZE;
170		for (part = 0;
171		     part <((MAXPARTITIONS<DEC_NUM_DISK_PARTS) ?
172			    MAXPARTITIONS : DEC_NUM_DISK_PARTS);
173		     part++) {
174			lp->d_partitions[part].p_size = dlp->map[part].num_blocks;
175			lp->d_partitions[part].p_offset = dlp->map[part].start_block;
176			lp->d_partitions[part].p_fsize = 1024;
177			lp->d_partitions[part].p_fstype =
178			  (part==1) ? FS_SWAP : FS_BSDFFS;
179			lp->d_npartitions += 1;
180
181#ifdef DIAGNOSTIC
182			printf(" Ultrix label rz%d%c: start %d len %d\n",
183			       DISKUNIT(dev), "abcdefgh"[part],
184			       lp->d_partitions[part].p_offset,
185			       lp->d_partitions[part].p_size);
186#endif
187		}
188		break;
189	}
190
191done:
192	brelse(bp, 0);
193	return (msg);
194}
195
196/*
197 * Check new disk label for sensibility
198 * before setting it.
199 */
200int
201setdisklabel(olp, nlp, openmask, osdep)
202	struct disklabel *olp, *nlp;
203	u_long openmask;
204	struct cpu_disklabel *osdep;
205{
206	int i;
207	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(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/*
239 * Write disk label back to device after modification.
240 */
241int
242writedisklabel(dev, strat, lp, osdep)
243	dev_t dev;
244	void (*strat)(struct buf *bp);
245	struct disklabel *lp;
246	struct cpu_disklabel *osdep;
247{
248	struct buf *bp;
249	struct disklabel *dlp;
250	int labelpart;
251	int error = 0;
252
253	labelpart = DISKPART(dev);
254	if (lp->d_partitions[labelpart].p_offset != 0) {
255		if (lp->d_partitions[0].p_offset != 0)
256			return (EXDEV);			/* not quite right */
257		labelpart = 0;
258	}
259	bp = geteblk((int)lp->d_secsize);
260	bp->b_dev = makedev(major(dev), DISKMINOR(DISKUNIT(dev), labelpart));
261	bp->b_blkno = LABELSECTOR;
262	bp->b_bcount = lp->d_secsize;
263	bp->b_flags |= B_READ;
264	(*strat)(bp);
265	if ((error = biowait(bp)) != 0)
266		goto done;
267	for (dlp = (struct disklabel *)bp->b_data;
268	    dlp <= (struct disklabel *)
269	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
270	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
271		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
272		    dkcksum(dlp) == 0) {
273			*dlp = *lp;
274			bp->b_oflags &= ~(BO_DONE);
275			bp->b_flags &= ~(B_READ);
276			bp->b_flags |= B_WRITE;
277			(*strat)(bp);
278			error = biowait(bp);
279			goto done;
280		}
281	}
282	error = ESRCH;
283done:
284	brelse(bp, 0);
285	return (error);
286}
287