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