subr_disk.c revision 1.103.4.2 1 1.103.4.2 snj /* $NetBSD: subr_disk.c,v 1.103.4.2 2015/06/01 19:19:44 snj Exp $ */
2 1.22 thorpej
3 1.22 thorpej /*-
4 1.95 ad * 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.103.4.2 snj __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.103.4.2 2015/06/01 19:19:44 snj Exp $");
71 1.48 mrg
72 1.11 mycroft #include <sys/param.h>
73 1.15 thorpej #include <sys/kernel.h>
74 1.94 yamt #include <sys/kmem.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.47 mrg #include <lib/libkern/libkern.h>
81 1.14 thorpej
82 1.14 thorpej /*
83 1.11 mycroft * Compute checksum for disk label.
84 1.11 mycroft */
85 1.11 mycroft u_int
86 1.30 simonb dkcksum(struct disklabel *lp)
87 1.11 mycroft {
88 1.98 tsutsui
89 1.84 martin return dkcksum_sized(lp, lp->d_npartitions);
90 1.84 martin }
91 1.84 martin
92 1.84 martin u_int
93 1.84 martin dkcksum_sized(struct disklabel *lp, size_t npartitions)
94 1.84 martin {
95 1.98 tsutsui uint16_t *start, *end;
96 1.98 tsutsui uint16_t sum = 0;
97 1.11 mycroft
98 1.98 tsutsui start = (uint16_t *)lp;
99 1.98 tsutsui end = (uint16_t *)&lp->d_partitions[npartitions];
100 1.11 mycroft while (start < end)
101 1.11 mycroft sum ^= *start++;
102 1.98 tsutsui return sum;
103 1.11 mycroft }
104 1.11 mycroft
105 1.11 mycroft /*
106 1.11 mycroft * Disk error is the preface to plaintive error messages
107 1.11 mycroft * about failing disk transfers. It prints messages of the form
108 1.11 mycroft
109 1.11 mycroft hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
110 1.11 mycroft
111 1.11 mycroft * if the offset of the error in the transfer and a disk label
112 1.11 mycroft * are both available. blkdone should be -1 if the position of the error
113 1.11 mycroft * is unknown; the disklabel pointer may be null from drivers that have not
114 1.20 christos * been converted to use them. The message is printed with printf
115 1.11 mycroft * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
116 1.20 christos * The message should be completed (with at least a newline) with printf
117 1.11 mycroft * or addlog, respectively. There is no trailing space.
118 1.11 mycroft */
119 1.51 dsl #ifndef PRIdaddr
120 1.51 dsl #define PRIdaddr PRId64
121 1.51 dsl #endif
122 1.11 mycroft void
123 1.38 yamt diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
124 1.38 yamt int blkdone, const struct disklabel *lp)
125 1.11 mycroft {
126 1.25 drochner int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
127 1.101 christos void (*pr)(const char *, ...) __printflike(1, 2);
128 1.11 mycroft char partname = 'a' + part;
129 1.51 dsl daddr_t sn;
130 1.51 dsl
131 1.52 dsl if (/*CONSTCOND*/0)
132 1.51 dsl /* Compiler will error this is the format is wrong... */
133 1.51 dsl printf("%" PRIdaddr, bp->b_blkno);
134 1.11 mycroft
135 1.11 mycroft if (pri != LOG_PRINTF) {
136 1.17 christos static const char fmt[] = "";
137 1.17 christos log(pri, fmt);
138 1.11 mycroft pr = addlog;
139 1.11 mycroft } else
140 1.20 christos pr = printf;
141 1.11 mycroft (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
142 1.11 mycroft bp->b_flags & B_READ ? "read" : "writ");
143 1.11 mycroft sn = bp->b_blkno;
144 1.11 mycroft if (bp->b_bcount <= DEV_BSIZE)
145 1.51 dsl (*pr)("%" PRIdaddr, sn);
146 1.11 mycroft else {
147 1.11 mycroft if (blkdone >= 0) {
148 1.11 mycroft sn += blkdone;
149 1.51 dsl (*pr)("%" PRIdaddr " of ", sn);
150 1.11 mycroft }
151 1.51 dsl (*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
152 1.11 mycroft bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
153 1.11 mycroft }
154 1.11 mycroft if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
155 1.11 mycroft sn += lp->d_partitions[part].p_offset;
156 1.51 dsl (*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
157 1.51 dsl dname, unit, sn, sn / lp->d_secpercyl);
158 1.11 mycroft sn %= lp->d_secpercyl;
159 1.51 dsl (*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
160 1.51 dsl sn / lp->d_nsectors, sn % lp->d_nsectors);
161 1.11 mycroft }
162 1.15 thorpej }
163 1.15 thorpej
164 1.15 thorpej /*
165 1.74 blymn * Searches the iostatlist for the disk corresponding to the
166 1.15 thorpej * name provided.
167 1.15 thorpej */
168 1.15 thorpej struct disk *
169 1.78 yamt disk_find(const char *name)
170 1.15 thorpej {
171 1.74 blymn struct io_stats *stat;
172 1.15 thorpej
173 1.74 blymn stat = iostat_find(name);
174 1.15 thorpej
175 1.75 blymn if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
176 1.75 blymn return stat->io_parent;
177 1.15 thorpej
178 1.76 yamt return (NULL);
179 1.15 thorpej }
180 1.15 thorpej
181 1.89 ad void
182 1.92 matt disk_init(struct disk *diskp, const char *name, const struct dkdriver *driver)
183 1.70 yamt {
184 1.70 yamt
185 1.70 yamt /*
186 1.70 yamt * Initialize the wedge-related locks and other fields.
187 1.70 yamt */
188 1.87 ad mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
189 1.87 ad mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
190 1.70 yamt LIST_INIT(&diskp->dk_wedges);
191 1.70 yamt diskp->dk_nwedges = 0;
192 1.83 scw diskp->dk_labelsector = LABELSECTOR;
193 1.83 scw disk_blocksize(diskp, DEV_BSIZE);
194 1.89 ad diskp->dk_name = name;
195 1.89 ad diskp->dk_driver = driver;
196 1.70 yamt }
197 1.70 yamt
198 1.89 ad /*
199 1.89 ad * Attach a disk.
200 1.89 ad */
201 1.89 ad void
202 1.89 ad disk_attach(struct disk *diskp)
203 1.15 thorpej {
204 1.79 kardel
205 1.15 thorpej /*
206 1.94 yamt * Allocate and initialize the disklabel structures.
207 1.15 thorpej */
208 1.94 yamt diskp->dk_label = kmem_zalloc(sizeof(struct disklabel), KM_SLEEP);
209 1.94 yamt diskp->dk_cpulabel = kmem_zalloc(sizeof(struct cpu_disklabel),
210 1.94 yamt KM_SLEEP);
211 1.15 thorpej if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
212 1.15 thorpej panic("disk_attach: can't allocate storage for disklabel");
213 1.15 thorpej
214 1.15 thorpej /*
215 1.74 blymn * Set up the stats collection.
216 1.15 thorpej */
217 1.80 christos diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
218 1.15 thorpej }
219 1.15 thorpej
220 1.96 dyoung int
221 1.97 dyoung disk_begindetach(struct disk *dk, int (*lastclose)(device_t),
222 1.96 dyoung device_t self, int flags)
223 1.96 dyoung {
224 1.96 dyoung int rc;
225 1.96 dyoung
226 1.96 dyoung rc = 0;
227 1.96 dyoung mutex_enter(&dk->dk_openlock);
228 1.96 dyoung if (dk->dk_openmask == 0)
229 1.96 dyoung ; /* nothing to do */
230 1.96 dyoung else if ((flags & DETACH_FORCE) == 0)
231 1.96 dyoung rc = EBUSY;
232 1.96 dyoung else if (lastclose != NULL)
233 1.96 dyoung rc = (*lastclose)(self);
234 1.96 dyoung mutex_exit(&dk->dk_openlock);
235 1.96 dyoung
236 1.96 dyoung return rc;
237 1.96 dyoung }
238 1.96 dyoung
239 1.89 ad /*
240 1.89 ad * Detach a disk.
241 1.89 ad */
242 1.89 ad void
243 1.89 ad disk_detach(struct disk *diskp)
244 1.15 thorpej {
245 1.15 thorpej
246 1.15 thorpej /*
247 1.74 blymn * Remove from the drivelist.
248 1.23 thorpej */
249 1.76 yamt iostat_free(diskp->dk_stats);
250 1.23 thorpej
251 1.23 thorpej /*
252 1.81 thorpej * Release the disk-info dictionary.
253 1.81 thorpej */
254 1.81 thorpej if (diskp->dk_info) {
255 1.81 thorpej prop_object_release(diskp->dk_info);
256 1.81 thorpej diskp->dk_info = NULL;
257 1.81 thorpej }
258 1.81 thorpej
259 1.81 thorpej /*
260 1.15 thorpej * Free the space used by the disklabel structures.
261 1.15 thorpej */
262 1.94 yamt kmem_free(diskp->dk_label, sizeof(*diskp->dk_label));
263 1.94 yamt kmem_free(diskp->dk_cpulabel, sizeof(*diskp->dk_cpulabel));
264 1.15 thorpej }
265 1.15 thorpej
266 1.70 yamt void
267 1.89 ad disk_destroy(struct disk *diskp)
268 1.70 yamt {
269 1.70 yamt
270 1.87 ad mutex_destroy(&diskp->dk_openlock);
271 1.87 ad mutex_destroy(&diskp->dk_rawlock);
272 1.70 yamt }
273 1.70 yamt
274 1.70 yamt /*
275 1.74 blymn * Mark the disk as busy for metrics collection.
276 1.15 thorpej */
277 1.15 thorpej void
278 1.30 simonb disk_busy(struct disk *diskp)
279 1.15 thorpej {
280 1.76 yamt
281 1.74 blymn iostat_busy(diskp->dk_stats);
282 1.15 thorpej }
283 1.15 thorpej
284 1.15 thorpej /*
285 1.74 blymn * Finished disk operations, gather metrics.
286 1.15 thorpej */
287 1.15 thorpej void
288 1.45 mrg disk_unbusy(struct disk *diskp, long bcount, int read)
289 1.15 thorpej {
290 1.76 yamt
291 1.74 blymn iostat_unbusy(diskp->dk_stats, bcount, read);
292 1.39 hannken }
293 1.39 hannken
294 1.40 hannken /*
295 1.95 ad * Return true if disk has an I/O operation in flight.
296 1.95 ad */
297 1.95 ad bool
298 1.95 ad disk_isbusy(struct disk *diskp)
299 1.95 ad {
300 1.95 ad
301 1.95 ad return iostat_isbusy(diskp->dk_stats);
302 1.95 ad }
303 1.95 ad
304 1.95 ad /*
305 1.83 scw * Set the physical blocksize of a disk, in bytes.
306 1.83 scw * Only necessary if blocksize != DEV_BSIZE.
307 1.83 scw */
308 1.83 scw void
309 1.83 scw disk_blocksize(struct disk *diskp, int blocksize)
310 1.83 scw {
311 1.83 scw
312 1.83 scw diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
313 1.83 scw diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
314 1.83 scw }
315 1.83 scw
316 1.83 scw /*
317 1.50 fvdl * Bounds checking against the media size, used for the raw partition.
318 1.99 dsl * secsize, mediasize and b_blkno must all be the same units.
319 1.99 dsl * Possibly this has to be DEV_BSIZE (512).
320 1.50 fvdl */
321 1.50 fvdl int
322 1.73 perry bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
323 1.50 fvdl {
324 1.67 fvdl int64_t sz;
325 1.50 fvdl
326 1.103.4.1 snj if (bp->b_blkno < 0) {
327 1.103.4.1 snj /* Reject negative offsets immediately. */
328 1.103.4.1 snj bp->b_error = EINVAL;
329 1.103.4.1 snj return 0;
330 1.103.4.1 snj }
331 1.103.4.1 snj
332 1.103.4.1 snj sz = howmany((int64_t)bp->b_bcount, secsize);
333 1.103.4.1 snj
334 1.103.4.1 snj /*
335 1.103.4.1 snj * bp->b_bcount is a 32-bit value, and we rejected a negative
336 1.103.4.1 snj * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
337 1.103.4.1 snj */
338 1.50 fvdl
339 1.50 fvdl if (bp->b_blkno + sz > mediasize) {
340 1.50 fvdl sz = mediasize - bp->b_blkno;
341 1.50 fvdl if (sz == 0) {
342 1.50 fvdl /* If exactly at end of disk, return EOF. */
343 1.50 fvdl bp->b_resid = bp->b_bcount;
344 1.88 ad return 0;
345 1.50 fvdl }
346 1.50 fvdl if (sz < 0) {
347 1.50 fvdl /* If past end of disk, return EINVAL. */
348 1.50 fvdl bp->b_error = EINVAL;
349 1.88 ad return 0;
350 1.50 fvdl }
351 1.50 fvdl /* Otherwise, truncate request. */
352 1.99 dsl bp->b_bcount = sz * secsize;
353 1.50 fvdl }
354 1.50 fvdl
355 1.50 fvdl return 1;
356 1.11 mycroft }
357 1.82 thorpej
358 1.82 thorpej /*
359 1.83 scw * Determine the size of the transfer, and make sure it is
360 1.83 scw * within the boundaries of the partition. Adjust transfer
361 1.83 scw * if needed, and signal errors or early completion.
362 1.83 scw */
363 1.83 scw int
364 1.83 scw bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
365 1.83 scw {
366 1.83 scw struct disklabel *lp = dk->dk_label;
367 1.83 scw struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
368 1.83 scw uint64_t p_size, p_offset, labelsector;
369 1.83 scw int64_t sz;
370 1.83 scw
371 1.103.4.1 snj if (bp->b_blkno < 0) {
372 1.103.4.1 snj /* Reject negative offsets immediately. */
373 1.103.4.1 snj bp->b_error = EINVAL;
374 1.103.4.1 snj return -1;
375 1.103.4.1 snj }
376 1.103.4.1 snj
377 1.83 scw /* Protect against division by zero. XXX: Should never happen?!?! */
378 1.83 scw if (lp->d_secpercyl == 0) {
379 1.83 scw bp->b_error = EINVAL;
380 1.88 ad return -1;
381 1.83 scw }
382 1.83 scw
383 1.100 mrg p_size = (uint64_t)p->p_size << dk->dk_blkshift;
384 1.100 mrg p_offset = (uint64_t)p->p_offset << dk->dk_blkshift;
385 1.83 scw #if RAW_PART == 3
386 1.83 scw labelsector = lp->d_partitions[2].p_offset;
387 1.83 scw #else
388 1.83 scw labelsector = lp->d_partitions[RAW_PART].p_offset;
389 1.83 scw #endif
390 1.83 scw labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
391 1.83 scw
392 1.103.4.1 snj sz = howmany((int64_t)bp->b_bcount, DEV_BSIZE);
393 1.103.4.1 snj
394 1.103.4.1 snj /*
395 1.103.4.1 snj * bp->b_bcount is a 32-bit value, and we rejected a negative
396 1.103.4.1 snj * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
397 1.103.4.1 snj */
398 1.103.4.1 snj
399 1.103.4.1 snj if (bp->b_blkno + sz > p_size) {
400 1.83 scw sz = p_size - bp->b_blkno;
401 1.83 scw if (sz == 0) {
402 1.83 scw /* If exactly at end of disk, return EOF. */
403 1.83 scw bp->b_resid = bp->b_bcount;
404 1.88 ad return 0;
405 1.83 scw }
406 1.83 scw if (sz < 0) {
407 1.83 scw /* If past end of disk, return EINVAL. */
408 1.83 scw bp->b_error = EINVAL;
409 1.88 ad return -1;
410 1.83 scw }
411 1.83 scw /* Otherwise, truncate request. */
412 1.83 scw bp->b_bcount = sz << DEV_BSHIFT;
413 1.83 scw }
414 1.83 scw
415 1.83 scw /* Overwriting disk label? */
416 1.83 scw if (bp->b_blkno + p_offset <= labelsector &&
417 1.83 scw bp->b_blkno + p_offset + sz > labelsector &&
418 1.83 scw (bp->b_flags & B_READ) == 0 && !wlabel) {
419 1.83 scw bp->b_error = EROFS;
420 1.88 ad return -1;
421 1.83 scw }
422 1.83 scw
423 1.83 scw /* calculate cylinder for disksort to order transfers with */
424 1.83 scw bp->b_cylinder = (bp->b_blkno + p->p_offset) /
425 1.83 scw (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
426 1.88 ad return 1;
427 1.83 scw }
428 1.83 scw
429 1.86 dyoung int
430 1.86 dyoung disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
431 1.86 dyoung struct buf *bp, unsigned int sector, int count)
432 1.86 dyoung {
433 1.86 dyoung bp->b_blkno = sector;
434 1.86 dyoung bp->b_bcount = count * lp->d_secsize;
435 1.90 ad bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
436 1.90 ad bp->b_oflags &= ~BO_DONE;
437 1.86 dyoung bp->b_cylinder = sector / lp->d_secpercyl;
438 1.86 dyoung (*strat)(bp);
439 1.86 dyoung return biowait(bp);
440 1.86 dyoung }
441 1.86 dyoung
442 1.86 dyoung const char *
443 1.86 dyoung convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
444 1.86 dyoung struct buf *bp, uint32_t secperunit)
445 1.86 dyoung {
446 1.86 dyoung struct partition rp, *altp, *p;
447 1.86 dyoung int geom_ok;
448 1.86 dyoung
449 1.86 dyoung memset(&rp, 0, sizeof(rp));
450 1.86 dyoung rp.p_size = secperunit;
451 1.86 dyoung rp.p_fstype = FS_UNUSED;
452 1.86 dyoung
453 1.86 dyoung /* If we can seek to d_secperunit - 1, believe the disk geometry. */
454 1.86 dyoung if (secperunit != 0 &&
455 1.86 dyoung disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
456 1.86 dyoung geom_ok = 1;
457 1.86 dyoung else
458 1.86 dyoung geom_ok = 0;
459 1.86 dyoung
460 1.86 dyoung #if 0
461 1.86 dyoung printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
462 1.86 dyoung secperunit, geom_ok ? "ok" : "not ok");
463 1.86 dyoung #endif
464 1.86 dyoung
465 1.86 dyoung p = &lp->d_partitions[RAW_PART];
466 1.86 dyoung if (RAW_PART == 'c' - 'a')
467 1.86 dyoung altp = &lp->d_partitions['d' - 'a'];
468 1.86 dyoung else
469 1.86 dyoung altp = &lp->d_partitions['c' - 'a'];
470 1.86 dyoung
471 1.86 dyoung if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
472 1.86 dyoung ; /* already a raw partition */
473 1.86 dyoung else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
474 1.86 dyoung altp->p_offset == 0 && altp->p_size != 0) {
475 1.86 dyoung /* alternate partition ('c' or 'd') is suitable for raw slot,
476 1.86 dyoung * swap with 'd' or 'c'.
477 1.86 dyoung */
478 1.86 dyoung rp = *p;
479 1.86 dyoung *p = *altp;
480 1.86 dyoung *altp = rp;
481 1.86 dyoung } else if (lp->d_npartitions <= RAW_PART &&
482 1.86 dyoung lp->d_npartitions > 'c' - 'a') {
483 1.86 dyoung /* No raw partition is present, but the alternate is present.
484 1.86 dyoung * Copy alternate to raw partition.
485 1.86 dyoung */
486 1.86 dyoung lp->d_npartitions = RAW_PART + 1;
487 1.86 dyoung *p = *altp;
488 1.86 dyoung } else if (!geom_ok)
489 1.86 dyoung return "no raw partition and disk reports bad geometry";
490 1.86 dyoung else if (lp->d_npartitions <= RAW_PART) {
491 1.86 dyoung memset(&lp->d_partitions[lp->d_npartitions], 0,
492 1.86 dyoung sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
493 1.86 dyoung *p = rp;
494 1.86 dyoung lp->d_npartitions = RAW_PART + 1;
495 1.86 dyoung } else if (lp->d_npartitions < MAXPARTITIONS) {
496 1.86 dyoung memmove(p + 1, p,
497 1.86 dyoung sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
498 1.86 dyoung *p = rp;
499 1.86 dyoung lp->d_npartitions++;
500 1.86 dyoung } else
501 1.86 dyoung return "no raw partition and partition table is full";
502 1.86 dyoung return NULL;
503 1.86 dyoung }
504 1.86 dyoung
505 1.83 scw /*
506 1.82 thorpej * disk_ioctl --
507 1.82 thorpej * Generic disk ioctl handling.
508 1.82 thorpej */
509 1.82 thorpej int
510 1.85 christos disk_ioctl(struct disk *diskp, u_long cmd, void *data, int flag,
511 1.82 thorpej struct lwp *l)
512 1.82 thorpej {
513 1.103.4.2 snj int error = 0;
514 1.82 thorpej
515 1.82 thorpej switch (cmd) {
516 1.82 thorpej case DIOCGDISKINFO:
517 1.82 thorpej {
518 1.82 thorpej struct plistref *pref = (struct plistref *) data;
519 1.82 thorpej
520 1.82 thorpej if (diskp->dk_info == NULL)
521 1.82 thorpej error = ENOTSUP;
522 1.82 thorpej else
523 1.82 thorpej error = prop_dictionary_copyout_ioctl(pref, cmd,
524 1.82 thorpej diskp->dk_info);
525 1.82 thorpej break;
526 1.82 thorpej }
527 1.82 thorpej
528 1.103.4.2 snj case DIOCGSECTORSIZE:
529 1.103.4.2 snj *(u_int *)data = diskp->dk_geom.dg_secsize;
530 1.103.4.2 snj break;
531 1.103.4.2 snj
532 1.103.4.2 snj case DIOCGMEDIASIZE:
533 1.103.4.2 snj *(off_t *)data = (off_t)diskp->dk_geom.dg_secsize *
534 1.103.4.2 snj diskp->dk_geom.dg_secperunit;
535 1.103.4.2 snj break;
536 1.103.4.2 snj
537 1.82 thorpej default:
538 1.82 thorpej error = EPASSTHROUGH;
539 1.82 thorpej }
540 1.82 thorpej
541 1.82 thorpej return (error);
542 1.82 thorpej }
543 1.102 christos
544 1.102 christos void
545 1.102 christos disk_set_info(device_t dev, struct disk *dk, const char *type)
546 1.102 christos {
547 1.102 christos struct disk_geom *dg = &dk->dk_geom;
548 1.102 christos
549 1.102 christos if (dg->dg_secperunit == 0 && dg->dg_ncylinders == 0) {
550 1.102 christos #ifdef DIAGNOSTIC
551 1.102 christos printf("%s: secperunit and ncylinders are zero\n", dk->dk_name);
552 1.102 christos #endif
553 1.102 christos return;
554 1.102 christos }
555 1.102 christos
556 1.102 christos if (dg->dg_secperunit == 0) {
557 1.102 christos if (dg->dg_nsectors == 0 || dg->dg_ntracks == 0) {
558 1.102 christos #ifdef DIAGNOSTIC
559 1.102 christos printf("%s: secperunit and (sectors or tracks) "
560 1.102 christos "are zero\n", dk->dk_name);
561 1.102 christos #endif
562 1.102 christos return;
563 1.102 christos }
564 1.103 mlelstv dg->dg_secperunit = (int64_t) dg->dg_nsectors *
565 1.102 christos dg->dg_ntracks * dg->dg_ncylinders;
566 1.102 christos }
567 1.102 christos
568 1.102 christos if (dg->dg_ncylinders == 0) {
569 1.102 christos if (dg->dg_ntracks && dg->dg_nsectors)
570 1.102 christos dg->dg_ncylinders = dg->dg_secperunit /
571 1.102 christos (dg->dg_ntracks * dg->dg_nsectors);
572 1.102 christos }
573 1.102 christos
574 1.102 christos if (dg->dg_secsize == 0) {
575 1.102 christos #ifdef DIAGNOSTIC
576 1.102 christos printf("%s: fixing 0 sector size\n", dk->dk_name);
577 1.102 christos #endif
578 1.102 christos dg->dg_secsize = DEV_BSIZE;
579 1.102 christos }
580 1.102 christos
581 1.102 christos prop_dictionary_t disk_info, odisk_info, geom;
582 1.102 christos
583 1.102 christos disk_info = prop_dictionary_create();
584 1.102 christos geom = prop_dictionary_create();
585 1.102 christos
586 1.102 christos prop_dictionary_set_uint64(geom, "sectors-per-unit",
587 1.102 christos dg->dg_secperunit);
588 1.102 christos
589 1.102 christos prop_dictionary_set_uint32(geom, "sector-size", dg->dg_secsize);
590 1.102 christos
591 1.102 christos if (dg->dg_nsectors)
592 1.102 christos prop_dictionary_set_uint16(geom, "sectors-per-track",
593 1.102 christos dg->dg_nsectors);
594 1.102 christos
595 1.102 christos if (dg->dg_ntracks)
596 1.102 christos prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
597 1.102 christos dg->dg_ntracks);
598 1.102 christos
599 1.102 christos if (dg->dg_ncylinders)
600 1.102 christos prop_dictionary_set_uint64(geom, "cylinders-per-unit",
601 1.102 christos dg->dg_ncylinders);
602 1.102 christos
603 1.102 christos prop_dictionary_set(disk_info, "geometry", geom);
604 1.102 christos
605 1.102 christos if (type)
606 1.102 christos prop_dictionary_set_cstring_nocopy(disk_info, "type", type);
607 1.102 christos
608 1.102 christos prop_object_release(geom);
609 1.102 christos
610 1.102 christos odisk_info = dk->dk_info;
611 1.102 christos dk->dk_info = disk_info;
612 1.102 christos
613 1.102 christos if (dev)
614 1.102 christos prop_dictionary_set(device_properties(dev), "disk-info",
615 1.102 christos disk_info);
616 1.102 christos
617 1.102 christos /*
618 1.102 christos * Don't release disk_info here; we keep a reference to it.
619 1.102 christos * disk_detach() will release it when we go away.
620 1.102 christos */
621 1.102 christos if (odisk_info)
622 1.102 christos prop_object_release(odisk_info);
623 1.102 christos }
624