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