subr_disk.c revision 1.85.2.6 1 1.85.2.6 ad /* $NetBSD: subr_disk.c,v 1.85.2.6 2007/08/24 23:28:40 ad Exp $ */
2 1.84 martin
3 1.85.2.5 ad /*-
4 1.85.2.5 ad * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
5 1.85.2.5 ad * All rights reserved.
6 1.85.2.5 ad *
7 1.85.2.5 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.85.2.5 ad * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.85.2.5 ad * NASA Ames Research Center.
10 1.85.2.5 ad *
11 1.85.2.5 ad * Redistribution and use in source and binary forms, with or without
12 1.85.2.5 ad * modification, are permitted provided that the following conditions
13 1.85.2.5 ad * are met:
14 1.85.2.5 ad * 1. Redistributions of source code must retain the above copyright
15 1.85.2.5 ad * notice, this list of conditions and the following disclaimer.
16 1.85.2.5 ad * 2. Redistributions in binary form must reproduce the above copyright
17 1.85.2.5 ad * notice, this list of conditions and the following disclaimer in the
18 1.85.2.5 ad * documentation and/or other materials provided with the distribution.
19 1.85.2.5 ad * 3. All advertising materials mentioning features or use of this software
20 1.85.2.5 ad * must display the following acknowledgement:
21 1.85.2.5 ad * This product includes software developed by the NetBSD
22 1.85.2.5 ad * Foundation, Inc. and its contributors.
23 1.85.2.5 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.85.2.5 ad * contributors may be used to endorse or promote products derived
25 1.85.2.5 ad * from this software without specific prior written permission.
26 1.85.2.5 ad *
27 1.85.2.5 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.85.2.5 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.85.2.5 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.85.2.5 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.85.2.5 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.85.2.5 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.85.2.5 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.85.2.5 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.85.2.5 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.85.2.5 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.85.2.5 ad * POSSIBILITY OF SUCH DAMAGE.
38 1.85.2.5 ad */
39 1.11 mycroft
40 1.85.2.5 ad /*
41 1.85.2.5 ad * Copyright (c) 1982, 1986, 1988, 1993
42 1.85.2.5 ad * The Regents of the University of California. All rights reserved.
43 1.85.2.5 ad * (c) UNIX System Laboratories, Inc.
44 1.85.2.5 ad * All or some portions of this file are derived from material licensed
45 1.85.2.5 ad * to the University of California by American Telephone and Telegraph
46 1.85.2.5 ad * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 1.85.2.5 ad * the permission of UNIX System Laboratories, Inc.
48 1.85.2.5 ad *
49 1.85.2.5 ad * Redistribution and use in source and binary forms, with or without
50 1.85.2.5 ad * modification, are permitted provided that the following conditions
51 1.85.2.5 ad * are met:
52 1.85.2.5 ad * 1. Redistributions of source code must retain the above copyright
53 1.85.2.5 ad * notice, this list of conditions and the following disclaimer.
54 1.85.2.5 ad * 2. Redistributions in binary form must reproduce the above copyright
55 1.85.2.5 ad * notice, this list of conditions and the following disclaimer in the
56 1.85.2.5 ad * documentation and/or other materials provided with the distribution.
57 1.85.2.5 ad * 3. Neither the name of the University nor the names of its contributors
58 1.85.2.5 ad * may be used to endorse or promote products derived from this software
59 1.85.2.5 ad * without specific prior written permission.
60 1.85.2.5 ad *
61 1.85.2.5 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 1.85.2.5 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 1.85.2.5 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 1.85.2.5 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 1.85.2.5 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 1.85.2.5 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 1.85.2.5 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 1.85.2.5 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 1.85.2.5 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 1.85.2.5 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 1.85.2.5 ad * SUCH DAMAGE.
72 1.85.2.5 ad *
73 1.85.2.5 ad * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
74 1.85.2.5 ad */
75 1.11 mycroft
76 1.85.2.5 ad #include <sys/cdefs.h>
77 1.85.2.6 ad __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.85.2.6 2007/08/24 23:28:40 ad Exp $");
78 1.11 mycroft
79 1.85.2.5 ad #include <sys/param.h>
80 1.85.2.5 ad #include <sys/kernel.h>
81 1.85.2.5 ad #include <sys/malloc.h>
82 1.85.2.5 ad #include <sys/buf.h>
83 1.85.2.5 ad #include <sys/syslog.h>
84 1.85.2.5 ad #include <sys/disklabel.h>
85 1.85.2.5 ad #include <sys/disk.h>
86 1.85.2.5 ad #include <sys/sysctl.h>
87 1.85.2.5 ad #include <lib/libkern/libkern.h>
88 1.11 mycroft
89 1.85.2.5 ad /*
90 1.85.2.5 ad * Compute checksum for disk label.
91 1.85.2.5 ad */
92 1.85.2.5 ad u_int
93 1.85.2.5 ad dkcksum(struct disklabel *lp)
94 1.85.2.5 ad {
95 1.85.2.5 ad return dkcksum_sized(lp, lp->d_npartitions);
96 1.85.2.5 ad }
97 1.15 thorpej
98 1.85.2.5 ad u_int
99 1.85.2.5 ad dkcksum_sized(struct disklabel *lp, size_t npartitions)
100 1.85.2.5 ad {
101 1.85.2.5 ad u_short *start, *end;
102 1.85.2.5 ad u_short sum = 0;
103 1.15 thorpej
104 1.85.2.5 ad start = (u_short *)lp;
105 1.85.2.5 ad end = (u_short *)&lp->d_partitions[npartitions];
106 1.85.2.5 ad while (start < end)
107 1.85.2.5 ad sum ^= *start++;
108 1.85.2.5 ad return (sum);
109 1.85.2.5 ad }
110 1.15 thorpej
111 1.85.2.5 ad /*
112 1.85.2.5 ad * Disk error is the preface to plaintive error messages
113 1.85.2.5 ad * about failing disk transfers. It prints messages of the form
114 1.15 thorpej
115 1.85.2.5 ad hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
116 1.70 yamt
117 1.85.2.5 ad * if the offset of the error in the transfer and a disk label
118 1.85.2.5 ad * are both available. blkdone should be -1 if the position of the error
119 1.85.2.5 ad * is unknown; the disklabel pointer may be null from drivers that have not
120 1.85.2.5 ad * been converted to use them. The message is printed with printf
121 1.85.2.5 ad * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
122 1.85.2.5 ad * The message should be completed (with at least a newline) with printf
123 1.85.2.5 ad * or addlog, respectively. There is no trailing space.
124 1.85.2.5 ad */
125 1.85.2.5 ad #ifndef PRIdaddr
126 1.85.2.5 ad #define PRIdaddr PRId64
127 1.85.2.5 ad #endif
128 1.85.2.5 ad void
129 1.85.2.5 ad diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
130 1.85.2.5 ad int blkdone, const struct disklabel *lp)
131 1.85.2.5 ad {
132 1.85.2.5 ad int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
133 1.85.2.5 ad void (*pr)(const char *, ...);
134 1.85.2.5 ad char partname = 'a' + part;
135 1.85.2.5 ad daddr_t sn;
136 1.85.2.5 ad
137 1.85.2.5 ad if (/*CONSTCOND*/0)
138 1.85.2.5 ad /* Compiler will error this is the format is wrong... */
139 1.85.2.5 ad printf("%" PRIdaddr, bp->b_blkno);
140 1.85.2.5 ad
141 1.85.2.5 ad if (pri != LOG_PRINTF) {
142 1.85.2.5 ad static const char fmt[] = "";
143 1.85.2.5 ad log(pri, fmt);
144 1.85.2.5 ad pr = addlog;
145 1.85.2.5 ad } else
146 1.85.2.5 ad pr = printf;
147 1.85.2.5 ad (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
148 1.85.2.5 ad bp->b_flags & B_READ ? "read" : "writ");
149 1.85.2.5 ad sn = bp->b_blkno;
150 1.85.2.5 ad if (bp->b_bcount <= DEV_BSIZE)
151 1.85.2.5 ad (*pr)("%" PRIdaddr, sn);
152 1.85.2.5 ad else {
153 1.85.2.5 ad if (blkdone >= 0) {
154 1.85.2.5 ad sn += blkdone;
155 1.85.2.5 ad (*pr)("%" PRIdaddr " of ", sn);
156 1.85.2.5 ad }
157 1.85.2.5 ad (*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
158 1.85.2.5 ad bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
159 1.85.2.5 ad }
160 1.85.2.5 ad if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
161 1.85.2.5 ad sn += lp->d_partitions[part].p_offset;
162 1.85.2.5 ad (*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
163 1.85.2.5 ad dname, unit, sn, sn / lp->d_secpercyl);
164 1.85.2.5 ad sn %= lp->d_secpercyl;
165 1.85.2.5 ad (*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
166 1.85.2.5 ad sn / lp->d_nsectors, sn % lp->d_nsectors);
167 1.85.2.5 ad }
168 1.70 yamt }
169 1.70 yamt
170 1.85.2.3 ad /*
171 1.85.2.5 ad * Searches the iostatlist for the disk corresponding to the
172 1.85.2.5 ad * name provided.
173 1.85.2.3 ad */
174 1.85.2.5 ad struct disk *
175 1.85.2.5 ad disk_find(const char *name)
176 1.85.2.5 ad {
177 1.85.2.5 ad struct io_stats *stat;
178 1.85.2.4 ad
179 1.85.2.5 ad stat = iostat_find(name);
180 1.85.2.4 ad
181 1.85.2.5 ad if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
182 1.85.2.5 ad return stat->io_parent;
183 1.85.2.4 ad
184 1.85.2.5 ad return (NULL);
185 1.85.2.5 ad }
186 1.85.2.5 ad
187 1.85.2.5 ad void
188 1.85.2.5 ad disk_init(struct disk *diskp, char *name, struct dkdriver *driver)
189 1.85.2.5 ad {
190 1.79 kardel
191 1.15 thorpej /*
192 1.85.2.5 ad * Initialize the wedge-related locks and other fields.
193 1.15 thorpej */
194 1.85.2.5 ad mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
195 1.85.2.5 ad mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
196 1.85.2.5 ad LIST_INIT(&diskp->dk_wedges);
197 1.85.2.5 ad diskp->dk_nwedges = 0;
198 1.85.2.5 ad diskp->dk_labelsector = LABELSECTOR;
199 1.85.2.5 ad disk_blocksize(diskp, DEV_BSIZE);
200 1.85.2.5 ad diskp->dk_name = name;
201 1.85.2.5 ad diskp->dk_driver = driver;
202 1.85.2.5 ad }
203 1.15 thorpej
204 1.85.2.5 ad /*
205 1.85.2.5 ad * Attach a disk.
206 1.85.2.5 ad */
207 1.85.2.5 ad void
208 1.85.2.5 ad disk_attach(struct disk *diskp)
209 1.85.2.5 ad {
210 1.15 thorpej
211 1.15 thorpej /*
212 1.85.2.5 ad * Allocate and initialize the disklabel structures. Note that
213 1.85.2.5 ad * it's not safe to sleep here, since we're probably going to be
214 1.85.2.5 ad * called during autoconfiguration.
215 1.15 thorpej */
216 1.85.2.5 ad diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
217 1.85.2.5 ad diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
218 1.85.2.5 ad M_NOWAIT);
219 1.85.2.5 ad if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
220 1.85.2.5 ad panic("disk_attach: can't allocate storage for disklabel");
221 1.15 thorpej
222 1.85.2.5 ad memset(diskp->dk_label, 0, sizeof(struct disklabel));
223 1.85.2.5 ad memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
224 1.15 thorpej
225 1.15 thorpej /*
226 1.85.2.5 ad * Set up the stats collection.
227 1.23 thorpej */
228 1.85.2.5 ad diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
229 1.85.2.5 ad }
230 1.85.2.4 ad
231 1.85.2.5 ad /*
232 1.85.2.5 ad * Detach a disk.
233 1.85.2.5 ad */
234 1.85.2.5 ad void
235 1.85.2.5 ad disk_detach(struct disk *diskp)
236 1.85.2.5 ad {
237 1.23 thorpej
238 1.23 thorpej /*
239 1.85.2.5 ad * Remove from the drivelist.
240 1.81 thorpej */
241 1.85.2.5 ad iostat_free(diskp->dk_stats);
242 1.85.2.4 ad
243 1.85.2.5 ad /*
244 1.85.2.5 ad * Release the disk-info dictionary.
245 1.85.2.5 ad */
246 1.85.2.5 ad if (diskp->dk_info) {
247 1.85.2.5 ad prop_object_release(diskp->dk_info);
248 1.85.2.5 ad diskp->dk_info = NULL;
249 1.81 thorpej }
250 1.81 thorpej
251 1.81 thorpej /*
252 1.85.2.5 ad * Free the space used by the disklabel structures.
253 1.15 thorpej */
254 1.85.2.5 ad free(diskp->dk_label, M_DEVBUF);
255 1.85.2.5 ad free(diskp->dk_cpulabel, M_DEVBUF);
256 1.85.2.5 ad }
257 1.15 thorpej
258 1.85.2.5 ad void
259 1.85.2.5 ad disk_destroy(struct disk *diskp)
260 1.85.2.5 ad {
261 1.70 yamt
262 1.85.2.5 ad mutex_destroy(&diskp->dk_openlock);
263 1.85.2.5 ad mutex_destroy(&diskp->dk_rawlock);
264 1.70 yamt }
265 1.70 yamt
266 1.70 yamt /*
267 1.85.2.5 ad * Mark the disk as busy for metrics collection.
268 1.15 thorpej */
269 1.15 thorpej void
270 1.85.2.5 ad disk_busy(struct disk *diskp)
271 1.15 thorpej {
272 1.76 yamt
273 1.85.2.5 ad iostat_busy(diskp->dk_stats);
274 1.85.2.5 ad }
275 1.76 yamt
276 1.85.2.5 ad /*
277 1.85.2.5 ad * Finished disk operations, gather metrics.
278 1.85.2.5 ad */
279 1.85.2.5 ad void
280 1.85.2.5 ad disk_unbusy(struct disk *diskp, long bcount, int read)
281 1.85.2.5 ad {
282 1.85.2.4 ad
283 1.85.2.5 ad iostat_unbusy(diskp->dk_stats, bcount, read);
284 1.39 hannken }
285 1.39 hannken
286 1.40 hannken /*
287 1.85.2.5 ad * Set the physical blocksize of a disk, in bytes.
288 1.85.2.5 ad * Only necessary if blocksize != DEV_BSIZE.
289 1.83 scw */
290 1.85.2.5 ad void
291 1.85.2.5 ad disk_blocksize(struct disk *diskp, int blocksize)
292 1.85.2.5 ad {
293 1.85.2.5 ad
294 1.85.2.5 ad diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
295 1.85.2.5 ad diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
296 1.85.2.5 ad }
297 1.83 scw
298 1.83 scw /*
299 1.85.2.5 ad * Bounds checking against the media size, used for the raw partition.
300 1.85.2.5 ad * The sector size passed in should currently always be DEV_BSIZE,
301 1.85.2.5 ad * and the media size the size of the device in DEV_BSIZE sectors.
302 1.50 fvdl */
303 1.50 fvdl int
304 1.85.2.5 ad bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
305 1.50 fvdl {
306 1.85.2.5 ad int64_t sz;
307 1.50 fvdl
308 1.85.2.5 ad sz = howmany(bp->b_bcount, secsize);
309 1.85.2.4 ad
310 1.85.2.5 ad if (bp->b_blkno + sz > mediasize) {
311 1.85.2.5 ad sz = mediasize - bp->b_blkno;
312 1.85.2.5 ad if (sz == 0) {
313 1.85.2.5 ad /* If exactly at end of disk, return EOF. */
314 1.85.2.5 ad bp->b_resid = bp->b_bcount;
315 1.85.2.5 ad return 0;
316 1.85.2.5 ad }
317 1.85.2.5 ad if (sz < 0) {
318 1.85.2.5 ad /* If past end of disk, return EINVAL. */
319 1.85.2.5 ad bp->b_error = EINVAL;
320 1.85.2.5 ad return 0;
321 1.85.2.5 ad }
322 1.85.2.5 ad /* Otherwise, truncate request. */
323 1.85.2.5 ad bp->b_bcount = sz << DEV_BSHIFT;
324 1.50 fvdl }
325 1.50 fvdl
326 1.85.2.5 ad return 1;
327 1.11 mycroft }
328 1.82 thorpej
329 1.82 thorpej /*
330 1.85.2.5 ad * Determine the size of the transfer, and make sure it is
331 1.85.2.5 ad * within the boundaries of the partition. Adjust transfer
332 1.85.2.5 ad * if needed, and signal errors or early completion.
333 1.83 scw */
334 1.83 scw int
335 1.85.2.5 ad bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
336 1.83 scw {
337 1.85.2.5 ad struct disklabel *lp = dk->dk_label;
338 1.85.2.5 ad struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
339 1.85.2.5 ad uint64_t p_size, p_offset, labelsector;
340 1.85.2.5 ad int64_t sz;
341 1.85.2.5 ad
342 1.85.2.5 ad /* Protect against division by zero. XXX: Should never happen?!?! */
343 1.85.2.5 ad if (lp->d_secpercyl == 0) {
344 1.85.2.5 ad bp->b_error = EINVAL;
345 1.85.2.5 ad return -1;
346 1.85.2.5 ad }
347 1.85.2.5 ad
348 1.85.2.5 ad p_size = p->p_size << dk->dk_blkshift;
349 1.85.2.5 ad p_offset = p->p_offset << dk->dk_blkshift;
350 1.85.2.5 ad #if RAW_PART == 3
351 1.85.2.5 ad labelsector = lp->d_partitions[2].p_offset;
352 1.85.2.5 ad #else
353 1.85.2.5 ad labelsector = lp->d_partitions[RAW_PART].p_offset;
354 1.85.2.5 ad #endif
355 1.85.2.5 ad labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
356 1.85.2.1 ad
357 1.85.2.5 ad sz = howmany(bp->b_bcount, DEV_BSIZE);
358 1.85.2.5 ad if ((bp->b_blkno + sz) > p_size) {
359 1.85.2.5 ad sz = p_size - bp->b_blkno;
360 1.85.2.5 ad if (sz == 0) {
361 1.85.2.5 ad /* If exactly at end of disk, return EOF. */
362 1.85.2.5 ad bp->b_resid = bp->b_bcount;
363 1.85.2.5 ad return 0;
364 1.85.2.5 ad }
365 1.85.2.5 ad if (sz < 0) {
366 1.85.2.5 ad /* If past end of disk, return EINVAL. */
367 1.85.2.5 ad bp->b_error = EINVAL;
368 1.85.2.5 ad return -1;
369 1.85.2.5 ad }
370 1.85.2.5 ad /* Otherwise, truncate request. */
371 1.85.2.5 ad bp->b_bcount = sz << DEV_BSHIFT;
372 1.85.2.4 ad }
373 1.85.2.1 ad
374 1.85.2.5 ad /* Overwriting disk label? */
375 1.85.2.5 ad if (bp->b_blkno + p_offset <= labelsector &&
376 1.85.2.5 ad bp->b_blkno + p_offset + sz > labelsector &&
377 1.85.2.5 ad (bp->b_flags & B_READ) == 0 && !wlabel) {
378 1.85.2.5 ad bp->b_error = EROFS;
379 1.85.2.5 ad return -1;
380 1.85.2.5 ad }
381 1.85.2.5 ad
382 1.85.2.5 ad /* calculate cylinder for disksort to order transfers with */
383 1.85.2.5 ad bp->b_cylinder = (bp->b_blkno + p->p_offset) /
384 1.85.2.5 ad (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
385 1.85.2.5 ad return 1;
386 1.85.2.5 ad }
387 1.85.2.1 ad
388 1.85.2.5 ad int
389 1.85.2.5 ad disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
390 1.85.2.5 ad struct buf *bp, unsigned int sector, int count)
391 1.85.2.5 ad {
392 1.85.2.5 ad bp->b_blkno = sector;
393 1.85.2.5 ad bp->b_bcount = count * lp->d_secsize;
394 1.85.2.6 ad bp->b_oflags = (bp->b_oflags & ~BO_DONE);
395 1.85.2.6 ad bp->b_flags = B_READ;
396 1.85.2.5 ad bp->b_cylinder = sector / lp->d_secpercyl;
397 1.85.2.5 ad (*strat)(bp);
398 1.85.2.5 ad return biowait(bp);
399 1.85.2.5 ad }
400 1.85.2.1 ad
401 1.85.2.5 ad const char *
402 1.85.2.5 ad convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
403 1.85.2.5 ad struct buf *bp, uint32_t secperunit)
404 1.85.2.5 ad {
405 1.85.2.5 ad struct partition rp, *altp, *p;
406 1.85.2.5 ad int geom_ok;
407 1.85.2.1 ad
408 1.85.2.5 ad memset(&rp, 0, sizeof(rp));
409 1.85.2.5 ad rp.p_size = secperunit;
410 1.85.2.5 ad rp.p_fstype = FS_UNUSED;
411 1.85.2.5 ad
412 1.85.2.5 ad /* If we can seek to d_secperunit - 1, believe the disk geometry. */
413 1.85.2.5 ad if (secperunit != 0 &&
414 1.85.2.5 ad disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
415 1.85.2.5 ad geom_ok = 1;
416 1.85.2.5 ad else
417 1.85.2.5 ad geom_ok = 0;
418 1.85.2.1 ad
419 1.85.2.5 ad #if 0
420 1.85.2.5 ad printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
421 1.85.2.5 ad secperunit, geom_ok ? "ok" : "not ok");
422 1.85.2.4 ad #endif
423 1.82 thorpej
424 1.85.2.5 ad p = &lp->d_partitions[RAW_PART];
425 1.85.2.5 ad if (RAW_PART == 'c' - 'a')
426 1.85.2.5 ad altp = &lp->d_partitions['d' - 'a'];
427 1.85.2.5 ad else
428 1.85.2.5 ad altp = &lp->d_partitions['c' - 'a'];
429 1.82 thorpej
430 1.85.2.5 ad if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
431 1.85.2.5 ad ; /* already a raw partition */
432 1.85.2.5 ad else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
433 1.85.2.5 ad altp->p_offset == 0 && altp->p_size != 0) {
434 1.85.2.5 ad /* alternate partition ('c' or 'd') is suitable for raw slot,
435 1.85.2.5 ad * swap with 'd' or 'c'.
436 1.85.2.5 ad */
437 1.85.2.5 ad rp = *p;
438 1.85.2.5 ad *p = *altp;
439 1.85.2.5 ad *altp = rp;
440 1.85.2.5 ad } else if (lp->d_npartitions <= RAW_PART &&
441 1.85.2.5 ad lp->d_npartitions > 'c' - 'a') {
442 1.85.2.5 ad /* No raw partition is present, but the alternate is present.
443 1.85.2.5 ad * Copy alternate to raw partition.
444 1.85.2.5 ad */
445 1.85.2.5 ad lp->d_npartitions = RAW_PART + 1;
446 1.85.2.5 ad *p = *altp;
447 1.85.2.5 ad } else if (!geom_ok)
448 1.85.2.5 ad return "no raw partition and disk reports bad geometry";
449 1.85.2.5 ad else if (lp->d_npartitions <= RAW_PART) {
450 1.85.2.5 ad memset(&lp->d_partitions[lp->d_npartitions], 0,
451 1.85.2.5 ad sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
452 1.85.2.5 ad *p = rp;
453 1.85.2.5 ad lp->d_npartitions = RAW_PART + 1;
454 1.85.2.5 ad } else if (lp->d_npartitions < MAXPARTITIONS) {
455 1.85.2.5 ad memmove(p + 1, p,
456 1.85.2.5 ad sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
457 1.85.2.5 ad *p = rp;
458 1.85.2.5 ad lp->d_npartitions++;
459 1.85.2.5 ad } else
460 1.85.2.5 ad return "no raw partition and partition table is full";
461 1.85.2.5 ad return NULL;
462 1.85.2.5 ad }
463 1.82 thorpej
464 1.85.2.5 ad /*
465 1.85.2.5 ad * disk_ioctl --
466 1.85.2.5 ad * Generic disk ioctl handling.
467 1.85.2.5 ad */
468 1.85.2.5 ad int
469 1.85.2.5 ad disk_ioctl(struct disk *diskp, u_long cmd, void *data, int flag,
470 1.85.2.5 ad struct lwp *l)
471 1.85.2.5 ad {
472 1.85.2.5 ad int error;
473 1.85.2.5 ad
474 1.85.2.5 ad switch (cmd) {
475 1.85.2.5 ad case DIOCGDISKINFO:
476 1.85.2.5 ad {
477 1.85.2.5 ad struct plistref *pref = (struct plistref *) data;
478 1.85.2.5 ad
479 1.85.2.5 ad if (diskp->dk_info == NULL)
480 1.85.2.5 ad error = ENOTSUP;
481 1.85.2.5 ad else
482 1.85.2.5 ad error = prop_dictionary_copyout_ioctl(pref, cmd,
483 1.85.2.5 ad diskp->dk_info);
484 1.85.2.5 ad break;
485 1.85.2.5 ad }
486 1.85.2.5 ad
487 1.85.2.5 ad default:
488 1.85.2.5 ad error = EPASSTHROUGH;
489 1.82 thorpej }
490 1.82 thorpej
491 1.85.2.5 ad return (error);
492 1.85.2.5 ad }
493