xref: /netbsd/sys/arch/next68k/next68k/disksubr.c (revision bf9ec67e)
1 /*	$NetBSD: disksubr.c,v 1.8 2002/05/20 20:49:16 jdolecek 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. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/disklabel.h>
47 #include <sys/syslog.h>
48 
49 #include <sys/disk.h>
50 
51 #define	b_cylinder	b_resid
52 
53 /*
54  * Attempt to read a disk label from a device using the indicated strategy
55  * routine.  The label must be partly set up before this: secpercyl and
56  * anything required in the strategy routine (e.g., sector size) must be
57  * filled in before calling us.  Returns null on success and an error
58  * string on failure.
59  */
60 char *
61 readdisklabel(dev, strat, lp, osdep)
62 	dev_t dev;
63 	void (*strat) __P((struct buf *));
64 	struct disklabel *lp;
65 	struct cpu_disklabel *osdep;
66 {
67 	struct buf *bp;
68 	struct disklabel *dlp;
69 	char *msg = NULL;
70 	int i;
71 
72 	/* minimal requirements for archtypal disk label */
73 	if (lp->d_secsize == 0)
74 		lp->d_secsize = DEV_BSIZE;
75 	if (lp->d_secperunit == 0)
76 		lp->d_secperunit = 0x1fffffff;
77 	lp->d_npartitions = RAW_PART + 1;
78 	for (i = 0; i < RAW_PART; i++) {
79 		lp->d_partitions[i].p_size = 0;
80 		lp->d_partitions[i].p_offset = 0;
81 	}
82 	if (lp->d_partitions[i].p_size == 0)
83 		lp->d_partitions[i].p_size = 0x1fffffff;
84 	lp->d_partitions[i].p_offset = 0;
85 
86 	bp = geteblk((int)lp->d_secsize);
87 	bp->b_dev = dev;
88 	bp->b_blkno = LABELSECTOR;
89 	bp->b_bcount = lp->d_secsize;
90 	bp->b_flags |= B_READ;
91 	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
92 	(*strat)(bp);
93 	if (biowait(bp))
94 		msg = "I/O error";
95 	else for (dlp = (struct disklabel *)bp->b_data;
96 	    dlp <= (struct disklabel *)((char *)bp->b_data +
97 	    DEV_BSIZE - sizeof(*dlp));
98 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
99 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
100 			if (msg == NULL)
101 				msg = "no disk label";
102 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
103 			   dkcksum(dlp) != 0)
104 			msg = "disk label corrupted";
105 		else {
106 			*lp = *dlp;
107 			msg = NULL;
108 			break;
109 		}
110 	}
111 	brelse(bp);
112 	return (msg);
113 }
114 
115 /*
116  * Check new disk label for sensibility before setting it.
117  */
118 int
119 setdisklabel(olp, nlp, openmask, osdep)
120 	struct disklabel *olp, *nlp;
121 	u_long openmask;
122 	struct cpu_disklabel *osdep;
123 {
124 	int i;
125 	struct partition *opp, *npp;
126 
127 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
128 	    dkcksum(nlp) != 0)
129 		return (EINVAL);
130 	while ((i = ffs(openmask)) != 0) {
131 		i--;
132 		openmask &= ~(1 << i);
133 		if (nlp->d_npartitions <= i)
134 			return (EBUSY);
135 		opp = &olp->d_partitions[i];
136 		npp = &nlp->d_partitions[i];
137 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
138 			return (EBUSY);
139 		/*
140 		 * Copy internally-set partition information
141 		 * if new label doesn't include it.		XXX
142 		 */
143 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
144 			npp->p_fstype = opp->p_fstype;
145 			npp->p_fsize = opp->p_fsize;
146 			npp->p_frag = opp->p_frag;
147 			npp->p_cpg = opp->p_cpg;
148 		}
149 	}
150 	nlp->d_checksum = 0;
151 	nlp->d_checksum = dkcksum(nlp);
152 	*olp = *nlp;
153 	return (0);
154 }
155 
156 /*
157  * Write disk label back to device after modification.
158  */
159 int
160 writedisklabel(dev, strat, lp, osdep)
161 	dev_t dev;
162 	void (*strat) __P((struct buf *));
163 	struct disklabel *lp;
164 	struct cpu_disklabel *osdep;
165 {
166 	struct buf *bp;
167 	struct disklabel *dlp;
168 	int labelpart;
169 	int error = 0;
170 
171 	labelpart = DISKPART(dev);
172 	if (lp->d_partitions[labelpart].p_offset != 0) {
173 		if (lp->d_partitions[0].p_offset != 0)
174 			return (EXDEV);			/* not quite right */
175 		labelpart = 0;
176 	}
177 	bp = geteblk((int)lp->d_secsize);
178 	bp->b_dev = MAKEDISKDEV(major(dev), DISKUNIT(dev), labelpart);
179 	bp->b_blkno = LABELSECTOR;
180 	bp->b_bcount = lp->d_secsize;
181 	bp->b_flags |= B_READ;
182 	(*strat)(bp);
183 	if ((error = biowait(bp)))
184 		goto done;
185 	for (dlp = (struct disklabel *)bp->b_data;
186 	    dlp <= (struct disklabel *)
187 	      ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
188 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
189 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
190 		    dkcksum(dlp) == 0) {
191 			*dlp = *lp;
192 			bp->b_flags &= ~(B_READ|B_DONE);
193 			bp->b_flags |= B_WRITE;
194 			(*strat)(bp);
195 			error = biowait(bp);
196 			goto done;
197 		}
198 	}
199 	error = ESRCH;
200 done:
201 	brelse(bp);
202 	return (error);
203 }
204 
205 /*
206  * Determine the size of the transfer, and make sure it is
207  * within the boundaries of the partition. Adjust transfer
208  * if needed, and signal errors or early completion.
209  */
210 int
211 bounds_check_with_label(bp, lp, wlabel)
212 	struct buf *bp;
213 	struct disklabel *lp;
214 	int wlabel;
215 {
216 	struct partition *p = &lp->d_partitions[DISKPART(bp->b_dev)];
217 	int labelsect = lp->d_partitions[0].p_offset;
218 	int maxsz = p->p_size;
219 	int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
220 
221 	/* Overwriting disk label? */
222 	if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
223 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
224 		bp->b_error = EROFS;
225 		goto bad;
226 	}
227 
228 	/* beyond partition? */
229 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
230 		if (bp->b_blkno == maxsz) {
231 			/* If exactly at end of disk, return EOF. */
232 			bp->b_resid = bp->b_bcount;
233 			return (0);
234 		}
235 		/* ...or truncate if part of it fits */
236 		sz = maxsz - bp->b_blkno;
237 		if (sz <= 0) {
238 			bp->b_error = EINVAL;
239 			goto bad;
240 		}
241 		bp->b_bcount = sz << DEV_BSHIFT;
242 	}
243 
244 	/* calculate cylinder for disksort to order transfers with */
245 	bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
246 	return (1);
247 
248  bad:
249 	bp->b_flags |= B_ERROR;
250 	return (-1);
251 }
252