disksubr.c revision 1.27
1/*	$NetBSD: disksubr.c,v 1.27 2008/01/02 11:48:25 ad Exp $	*/
2
3/*
4 * Copyright (c) 1982, 1986, 1988, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
37 */
38
39#include <sys/cdefs.h>
40__KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.27 2008/01/02 11:48:25 ad Exp $");
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/buf.h>
45#include <sys/disk.h>
46#include <sys/disklabel.h>
47
48/*
49 * Attempt to read a disk label from a device using the indicated strategy
50 * routine.  The label must be partly set up before this: secpercyl and
51 * anything required in the strategy routine (e.g., sector size) must be
52 * filled in before calling us.  Returns null on success and an error
53 * string on failure.
54 */
55const char *
56readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
57    struct cpu_disklabel *osdep)
58{
59	struct buf *bp;
60	struct disklabel *dlp;
61	const char *msg = NULL;
62	int i;
63
64	if (lp->d_secsize == 0)
65		lp->d_secsize = DEV_BSIZE;
66	if (lp->d_secperunit == 0)
67		lp->d_secperunit = 0x1fffffff;
68	if (lp->d_npartitions < RAW_PART + 1)
69		lp->d_npartitions = RAW_PART + 1;
70	for (i = 0; i < RAW_PART; i++) {
71		lp->d_partitions[i].p_size = 0;
72		lp->d_partitions[i].p_offset = 0;
73	}
74	if (lp->d_partitions[RAW_PART].p_size == 0)
75		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
76	lp->d_partitions[RAW_PART].p_offset = 0;
77
78	lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
79	lp->d_partitions[0].p_fstype = FS_BSDFFS;
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 *)((char *)bp->b_data +
92	    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, 0);
107	return msg;
108}
109
110/*
111 * Check new disk label for sensibility before setting it.
112 */
113int
114setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
115    struct cpu_disklabel *osdep)
116{
117	int i;
118	struct partition *opp, *npp;
119
120	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
121	    dkcksum(nlp) != 0)
122		return EINVAL;
123	while ((i = ffs(openmask)) != 0) {
124		i--;
125		openmask &= ~(1 << i);
126		if (nlp->d_npartitions <= i)
127			return EBUSY;
128		opp = &olp->d_partitions[i];
129		npp = &nlp->d_partitions[i];
130		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
131			return EBUSY;
132		/*
133		 * Copy internally-set partition information
134		 * if new label doesn't include it.		XXX
135		 */
136		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
137			npp->p_fstype = opp->p_fstype;
138			npp->p_fsize = opp->p_fsize;
139			npp->p_frag = opp->p_frag;
140			npp->p_cpg = opp->p_cpg;
141		}
142	}
143	nlp->d_checksum = 0;
144	nlp->d_checksum = dkcksum(nlp);
145	*olp = *nlp;
146	return 0;
147}
148
149/*
150 * Write disk label back to device after modification.
151 */
152int
153writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
154    struct cpu_disklabel *osdep)
155{
156	struct buf *bp;
157	struct disklabel *dlp;
158	int labelpart;
159	int error = 0;
160
161	labelpart = DISKPART(dev);
162	if (lp->d_partitions[labelpart].p_offset != 0) {
163		if (lp->d_partitions[0].p_offset != 0)
164			return EXDEV;			/* not quite right */
165		labelpart = 0;
166	}
167	bp = geteblk((int)lp->d_secsize);
168	bp->b_dev = MAKEDISKDEV(major(dev), DISKUNIT(dev), labelpart);
169	bp->b_blkno = LABELSECTOR;
170	bp->b_bcount = lp->d_secsize;
171	bp->b_flags |= B_READ;
172	(*strat)(bp);
173	if ((error = biowait(bp)) != 0)
174		goto done;
175	for (dlp = (struct disklabel *)bp->b_data;
176	    dlp <= (struct disklabel *)
177	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
178	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
179		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
180		    dkcksum(dlp) == 0) {
181			*dlp = *lp;
182			bp->b_oflags &= ~(BO_DONE);
183			bp->b_flags &= ~(B_READ);
184			bp->b_flags |= B_WRITE;
185			(*strat)(bp);
186			error = biowait(bp);
187			goto done;
188		}
189	}
190	error = ESRCH;
191 done:
192	brelse(bp, 0);
193	return error;
194}
195