subr_disk.c revision 1.100.18.7 1 1.100.18.5 tls /* $NetBSD: subr_disk.c,v 1.100.18.7 2017/12/03 11:38:45 jdolecek 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.100.18.5 tls __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.100.18.7 2017/12/03 11:38:45 jdolecek 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.100.18.7 jdolecek #include <sys/fcntl.h>
77 1.15 thorpej #include <sys/syslog.h>
78 1.11 mycroft #include <sys/disklabel.h>
79 1.100.18.1 tls #include <sys/conf.h>
80 1.15 thorpej #include <sys/disk.h>
81 1.33 simonb #include <sys/sysctl.h>
82 1.47 mrg #include <lib/libkern/libkern.h>
83 1.14 thorpej
84 1.100.18.3 tls unsigned int disk_serial;
85 1.100.18.3 tls
86 1.14 thorpej /*
87 1.11 mycroft * Compute checksum for disk label.
88 1.11 mycroft */
89 1.11 mycroft u_int
90 1.30 simonb dkcksum(struct disklabel *lp)
91 1.11 mycroft {
92 1.98 tsutsui
93 1.84 martin return dkcksum_sized(lp, lp->d_npartitions);
94 1.84 martin }
95 1.84 martin
96 1.84 martin u_int
97 1.84 martin dkcksum_sized(struct disklabel *lp, size_t npartitions)
98 1.84 martin {
99 1.98 tsutsui uint16_t *start, *end;
100 1.98 tsutsui uint16_t sum = 0;
101 1.11 mycroft
102 1.98 tsutsui start = (uint16_t *)lp;
103 1.98 tsutsui end = (uint16_t *)&lp->d_partitions[npartitions];
104 1.11 mycroft while (start < end)
105 1.11 mycroft sum ^= *start++;
106 1.98 tsutsui return sum;
107 1.11 mycroft }
108 1.11 mycroft
109 1.11 mycroft /*
110 1.11 mycroft * Disk error is the preface to plaintive error messages
111 1.11 mycroft * about failing disk transfers. It prints messages of the form
112 1.11 mycroft
113 1.11 mycroft hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
114 1.11 mycroft
115 1.11 mycroft * if the offset of the error in the transfer and a disk label
116 1.11 mycroft * are both available. blkdone should be -1 if the position of the error
117 1.11 mycroft * is unknown; the disklabel pointer may be null from drivers that have not
118 1.20 christos * been converted to use them. The message is printed with printf
119 1.11 mycroft * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
120 1.20 christos * The message should be completed (with at least a newline) with printf
121 1.11 mycroft * or addlog, respectively. There is no trailing space.
122 1.11 mycroft */
123 1.51 dsl #ifndef PRIdaddr
124 1.51 dsl #define PRIdaddr PRId64
125 1.51 dsl #endif
126 1.11 mycroft void
127 1.38 yamt diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
128 1.38 yamt int blkdone, const struct disklabel *lp)
129 1.11 mycroft {
130 1.25 drochner int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
131 1.100.18.4 tls void (*pr)(const char *, ...) __printflike(1, 2);
132 1.11 mycroft char partname = 'a' + part;
133 1.51 dsl daddr_t sn;
134 1.51 dsl
135 1.52 dsl if (/*CONSTCOND*/0)
136 1.51 dsl /* Compiler will error this is the format is wrong... */
137 1.51 dsl printf("%" PRIdaddr, bp->b_blkno);
138 1.11 mycroft
139 1.11 mycroft if (pri != LOG_PRINTF) {
140 1.17 christos static const char fmt[] = "";
141 1.17 christos log(pri, fmt);
142 1.11 mycroft pr = addlog;
143 1.11 mycroft } else
144 1.20 christos pr = printf;
145 1.11 mycroft (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
146 1.11 mycroft bp->b_flags & B_READ ? "read" : "writ");
147 1.11 mycroft sn = bp->b_blkno;
148 1.11 mycroft if (bp->b_bcount <= DEV_BSIZE)
149 1.51 dsl (*pr)("%" PRIdaddr, sn);
150 1.11 mycroft else {
151 1.11 mycroft if (blkdone >= 0) {
152 1.11 mycroft sn += blkdone;
153 1.51 dsl (*pr)("%" PRIdaddr " of ", sn);
154 1.11 mycroft }
155 1.51 dsl (*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
156 1.11 mycroft bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
157 1.11 mycroft }
158 1.11 mycroft if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
159 1.11 mycroft sn += lp->d_partitions[part].p_offset;
160 1.51 dsl (*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
161 1.51 dsl dname, unit, sn, sn / lp->d_secpercyl);
162 1.11 mycroft sn %= lp->d_secpercyl;
163 1.51 dsl (*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
164 1.51 dsl sn / lp->d_nsectors, sn % lp->d_nsectors);
165 1.11 mycroft }
166 1.15 thorpej }
167 1.15 thorpej
168 1.15 thorpej /*
169 1.74 blymn * Searches the iostatlist for the disk corresponding to the
170 1.15 thorpej * name provided.
171 1.15 thorpej */
172 1.15 thorpej struct disk *
173 1.78 yamt disk_find(const char *name)
174 1.15 thorpej {
175 1.74 blymn struct io_stats *stat;
176 1.15 thorpej
177 1.74 blymn stat = iostat_find(name);
178 1.15 thorpej
179 1.75 blymn if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
180 1.75 blymn return stat->io_parent;
181 1.15 thorpej
182 1.76 yamt return (NULL);
183 1.15 thorpej }
184 1.15 thorpej
185 1.100.18.1 tls /*
186 1.100.18.1 tls * Searches for the disk corresponding to a supplied block device
187 1.100.18.1 tls * using major, minor, unit.
188 1.100.18.1 tls */
189 1.100.18.1 tls struct disk *
190 1.100.18.1 tls disk_find_blk(dev_t dev)
191 1.100.18.1 tls {
192 1.100.18.1 tls devmajor_t major;
193 1.100.18.1 tls int unit;
194 1.100.18.1 tls char name[16]; /* XXX */
195 1.100.18.1 tls const char *swname;
196 1.100.18.1 tls
197 1.100.18.1 tls major = major(dev);
198 1.100.18.1 tls unit = DISKUNIT(dev);
199 1.100.18.1 tls
200 1.100.18.1 tls if ((swname = devsw_blk2name(major)) == NULL) {
201 1.100.18.1 tls return NULL;
202 1.100.18.1 tls }
203 1.100.18.1 tls
204 1.100.18.1 tls if (snprintf(name, sizeof(name), "%s%d",
205 1.100.18.1 tls swname, unit) > sizeof(name)) {
206 1.100.18.1 tls return NULL;
207 1.100.18.1 tls }
208 1.100.18.1 tls
209 1.100.18.1 tls return disk_find(name);
210 1.100.18.1 tls }
211 1.100.18.1 tls
212 1.100.18.1 tls int disk_maxphys(const struct disk *const diskp)
213 1.100.18.1 tls {
214 1.100.18.1 tls struct buf b = { b_bcount: MACHINE_MAXPHYS };
215 1.100.18.1 tls
216 1.100.18.1 tls diskp->dk_driver->d_minphys(&b);
217 1.100.18.1 tls
218 1.100.18.1 tls return b.b_bcount;
219 1.100.18.1 tls }
220 1.100.18.1 tls
221 1.89 ad void
222 1.92 matt disk_init(struct disk *diskp, const char *name, const struct dkdriver *driver)
223 1.70 yamt {
224 1.100.18.2 tls KASSERT(diskp != NULL);
225 1.100.18.2 tls KASSERT(name != NULL);
226 1.100.18.2 tls KASSERT(driver != NULL);
227 1.100.18.7 jdolecek u_int blocksize = DEV_BSIZE;
228 1.70 yamt
229 1.70 yamt /*
230 1.70 yamt * Initialize the wedge-related locks and other fields.
231 1.70 yamt */
232 1.87 ad mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
233 1.87 ad mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
234 1.70 yamt LIST_INIT(&diskp->dk_wedges);
235 1.70 yamt diskp->dk_nwedges = 0;
236 1.83 scw diskp->dk_labelsector = LABELSECTOR;
237 1.100.18.7 jdolecek diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
238 1.100.18.7 jdolecek diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
239 1.89 ad diskp->dk_name = name;
240 1.89 ad diskp->dk_driver = driver;
241 1.70 yamt }
242 1.70 yamt
243 1.89 ad /*
244 1.89 ad * Attach a disk.
245 1.89 ad */
246 1.89 ad void
247 1.89 ad disk_attach(struct disk *diskp)
248 1.15 thorpej {
249 1.79 kardel
250 1.15 thorpej /*
251 1.94 yamt * Allocate and initialize the disklabel structures.
252 1.15 thorpej */
253 1.94 yamt diskp->dk_label = kmem_zalloc(sizeof(struct disklabel), KM_SLEEP);
254 1.94 yamt diskp->dk_cpulabel = kmem_zalloc(sizeof(struct cpu_disklabel),
255 1.94 yamt KM_SLEEP);
256 1.15 thorpej
257 1.15 thorpej /*
258 1.74 blymn * Set up the stats collection.
259 1.15 thorpej */
260 1.80 christos diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
261 1.15 thorpej }
262 1.15 thorpej
263 1.96 dyoung int
264 1.97 dyoung disk_begindetach(struct disk *dk, int (*lastclose)(device_t),
265 1.96 dyoung device_t self, int flags)
266 1.96 dyoung {
267 1.96 dyoung int rc;
268 1.96 dyoung
269 1.96 dyoung rc = 0;
270 1.96 dyoung mutex_enter(&dk->dk_openlock);
271 1.96 dyoung if (dk->dk_openmask == 0)
272 1.96 dyoung ; /* nothing to do */
273 1.96 dyoung else if ((flags & DETACH_FORCE) == 0)
274 1.96 dyoung rc = EBUSY;
275 1.96 dyoung else if (lastclose != NULL)
276 1.96 dyoung rc = (*lastclose)(self);
277 1.96 dyoung mutex_exit(&dk->dk_openlock);
278 1.96 dyoung
279 1.96 dyoung return rc;
280 1.96 dyoung }
281 1.96 dyoung
282 1.89 ad /*
283 1.89 ad * Detach a disk.
284 1.89 ad */
285 1.89 ad void
286 1.89 ad disk_detach(struct disk *diskp)
287 1.15 thorpej {
288 1.15 thorpej
289 1.15 thorpej /*
290 1.74 blymn * Remove from the drivelist.
291 1.23 thorpej */
292 1.76 yamt iostat_free(diskp->dk_stats);
293 1.23 thorpej
294 1.23 thorpej /*
295 1.81 thorpej * Release the disk-info dictionary.
296 1.81 thorpej */
297 1.81 thorpej if (diskp->dk_info) {
298 1.81 thorpej prop_object_release(diskp->dk_info);
299 1.81 thorpej diskp->dk_info = NULL;
300 1.81 thorpej }
301 1.81 thorpej
302 1.81 thorpej /*
303 1.15 thorpej * Free the space used by the disklabel structures.
304 1.15 thorpej */
305 1.94 yamt kmem_free(diskp->dk_label, sizeof(*diskp->dk_label));
306 1.94 yamt kmem_free(diskp->dk_cpulabel, sizeof(*diskp->dk_cpulabel));
307 1.15 thorpej }
308 1.15 thorpej
309 1.70 yamt void
310 1.89 ad disk_destroy(struct disk *diskp)
311 1.70 yamt {
312 1.70 yamt
313 1.87 ad mutex_destroy(&diskp->dk_openlock);
314 1.87 ad mutex_destroy(&diskp->dk_rawlock);
315 1.70 yamt }
316 1.70 yamt
317 1.70 yamt /*
318 1.100.18.7 jdolecek * Mark the disk as having work queued for metrics collection.
319 1.100.18.7 jdolecek */
320 1.100.18.7 jdolecek void
321 1.100.18.7 jdolecek disk_wait(struct disk *diskp)
322 1.100.18.7 jdolecek {
323 1.100.18.7 jdolecek
324 1.100.18.7 jdolecek iostat_wait(diskp->dk_stats);
325 1.100.18.7 jdolecek }
326 1.100.18.7 jdolecek
327 1.100.18.7 jdolecek /*
328 1.74 blymn * Mark the disk as busy for metrics collection.
329 1.15 thorpej */
330 1.15 thorpej void
331 1.30 simonb disk_busy(struct disk *diskp)
332 1.15 thorpej {
333 1.76 yamt
334 1.74 blymn iostat_busy(diskp->dk_stats);
335 1.15 thorpej }
336 1.15 thorpej
337 1.15 thorpej /*
338 1.74 blymn * Finished disk operations, gather metrics.
339 1.15 thorpej */
340 1.15 thorpej void
341 1.45 mrg disk_unbusy(struct disk *diskp, long bcount, int read)
342 1.15 thorpej {
343 1.76 yamt
344 1.74 blymn iostat_unbusy(diskp->dk_stats, bcount, read);
345 1.39 hannken }
346 1.39 hannken
347 1.40 hannken /*
348 1.95 ad * Return true if disk has an I/O operation in flight.
349 1.95 ad */
350 1.95 ad bool
351 1.95 ad disk_isbusy(struct disk *diskp)
352 1.95 ad {
353 1.95 ad
354 1.95 ad return iostat_isbusy(diskp->dk_stats);
355 1.95 ad }
356 1.95 ad
357 1.95 ad /*
358 1.50 fvdl * Bounds checking against the media size, used for the raw partition.
359 1.99 dsl * secsize, mediasize and b_blkno must all be the same units.
360 1.99 dsl * Possibly this has to be DEV_BSIZE (512).
361 1.50 fvdl */
362 1.50 fvdl int
363 1.73 perry bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
364 1.50 fvdl {
365 1.67 fvdl int64_t sz;
366 1.50 fvdl
367 1.100.18.7 jdolecek if (bp->b_blkno < 0) {
368 1.100.18.7 jdolecek /* Reject negative offsets immediately. */
369 1.100.18.7 jdolecek bp->b_error = EINVAL;
370 1.100.18.7 jdolecek return 0;
371 1.100.18.7 jdolecek }
372 1.100.18.7 jdolecek
373 1.100.18.7 jdolecek sz = howmany((int64_t)bp->b_bcount, secsize);
374 1.100.18.7 jdolecek
375 1.100.18.7 jdolecek /*
376 1.100.18.7 jdolecek * bp->b_bcount is a 32-bit value, and we rejected a negative
377 1.100.18.7 jdolecek * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
378 1.100.18.7 jdolecek */
379 1.50 fvdl
380 1.50 fvdl if (bp->b_blkno + sz > mediasize) {
381 1.50 fvdl sz = mediasize - bp->b_blkno;
382 1.50 fvdl if (sz == 0) {
383 1.50 fvdl /* If exactly at end of disk, return EOF. */
384 1.50 fvdl bp->b_resid = bp->b_bcount;
385 1.88 ad return 0;
386 1.50 fvdl }
387 1.50 fvdl if (sz < 0) {
388 1.50 fvdl /* If past end of disk, return EINVAL. */
389 1.50 fvdl bp->b_error = EINVAL;
390 1.88 ad return 0;
391 1.50 fvdl }
392 1.50 fvdl /* Otherwise, truncate request. */
393 1.99 dsl bp->b_bcount = sz * secsize;
394 1.50 fvdl }
395 1.50 fvdl
396 1.50 fvdl return 1;
397 1.11 mycroft }
398 1.82 thorpej
399 1.82 thorpej /*
400 1.83 scw * Determine the size of the transfer, and make sure it is
401 1.83 scw * within the boundaries of the partition. Adjust transfer
402 1.83 scw * if needed, and signal errors or early completion.
403 1.83 scw */
404 1.83 scw int
405 1.83 scw bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
406 1.83 scw {
407 1.83 scw struct disklabel *lp = dk->dk_label;
408 1.83 scw struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
409 1.83 scw uint64_t p_size, p_offset, labelsector;
410 1.83 scw int64_t sz;
411 1.83 scw
412 1.100.18.7 jdolecek if (bp->b_blkno < 0) {
413 1.100.18.7 jdolecek /* Reject negative offsets immediately. */
414 1.100.18.7 jdolecek bp->b_error = EINVAL;
415 1.100.18.7 jdolecek return -1;
416 1.100.18.7 jdolecek }
417 1.100.18.7 jdolecek
418 1.83 scw /* Protect against division by zero. XXX: Should never happen?!?! */
419 1.83 scw if (lp->d_secpercyl == 0) {
420 1.83 scw bp->b_error = EINVAL;
421 1.88 ad return -1;
422 1.83 scw }
423 1.83 scw
424 1.100 mrg p_size = (uint64_t)p->p_size << dk->dk_blkshift;
425 1.100 mrg p_offset = (uint64_t)p->p_offset << dk->dk_blkshift;
426 1.83 scw #if RAW_PART == 3
427 1.83 scw labelsector = lp->d_partitions[2].p_offset;
428 1.83 scw #else
429 1.83 scw labelsector = lp->d_partitions[RAW_PART].p_offset;
430 1.83 scw #endif
431 1.83 scw labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
432 1.83 scw
433 1.100.18.7 jdolecek sz = howmany((int64_t)bp->b_bcount, DEV_BSIZE);
434 1.100.18.7 jdolecek
435 1.100.18.7 jdolecek /*
436 1.100.18.7 jdolecek * bp->b_bcount is a 32-bit value, and we rejected a negative
437 1.100.18.7 jdolecek * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
438 1.100.18.7 jdolecek */
439 1.100.18.7 jdolecek
440 1.100.18.7 jdolecek if (bp->b_blkno + sz > p_size) {
441 1.83 scw sz = p_size - bp->b_blkno;
442 1.83 scw if (sz == 0) {
443 1.83 scw /* If exactly at end of disk, return EOF. */
444 1.83 scw bp->b_resid = bp->b_bcount;
445 1.88 ad return 0;
446 1.83 scw }
447 1.83 scw if (sz < 0) {
448 1.83 scw /* If past end of disk, return EINVAL. */
449 1.83 scw bp->b_error = EINVAL;
450 1.88 ad return -1;
451 1.83 scw }
452 1.83 scw /* Otherwise, truncate request. */
453 1.83 scw bp->b_bcount = sz << DEV_BSHIFT;
454 1.83 scw }
455 1.83 scw
456 1.83 scw /* Overwriting disk label? */
457 1.83 scw if (bp->b_blkno + p_offset <= labelsector &&
458 1.83 scw bp->b_blkno + p_offset + sz > labelsector &&
459 1.83 scw (bp->b_flags & B_READ) == 0 && !wlabel) {
460 1.83 scw bp->b_error = EROFS;
461 1.88 ad return -1;
462 1.83 scw }
463 1.83 scw
464 1.83 scw /* calculate cylinder for disksort to order transfers with */
465 1.83 scw bp->b_cylinder = (bp->b_blkno + p->p_offset) /
466 1.83 scw (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
467 1.88 ad return 1;
468 1.83 scw }
469 1.83 scw
470 1.86 dyoung int
471 1.86 dyoung disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
472 1.86 dyoung struct buf *bp, unsigned int sector, int count)
473 1.86 dyoung {
474 1.100.18.7 jdolecek bp->b_blkno = btodb((off_t)sector * lp->d_secsize);
475 1.86 dyoung bp->b_bcount = count * lp->d_secsize;
476 1.90 ad bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
477 1.90 ad bp->b_oflags &= ~BO_DONE;
478 1.86 dyoung bp->b_cylinder = sector / lp->d_secpercyl;
479 1.86 dyoung (*strat)(bp);
480 1.86 dyoung return biowait(bp);
481 1.86 dyoung }
482 1.86 dyoung
483 1.86 dyoung const char *
484 1.86 dyoung convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
485 1.86 dyoung struct buf *bp, uint32_t secperunit)
486 1.86 dyoung {
487 1.86 dyoung struct partition rp, *altp, *p;
488 1.86 dyoung int geom_ok;
489 1.100.18.7 jdolecek const char *str;
490 1.86 dyoung
491 1.86 dyoung memset(&rp, 0, sizeof(rp));
492 1.86 dyoung rp.p_size = secperunit;
493 1.86 dyoung rp.p_fstype = FS_UNUSED;
494 1.86 dyoung
495 1.86 dyoung /* If we can seek to d_secperunit - 1, believe the disk geometry. */
496 1.86 dyoung if (secperunit != 0 &&
497 1.86 dyoung disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
498 1.86 dyoung geom_ok = 1;
499 1.86 dyoung else
500 1.86 dyoung geom_ok = 0;
501 1.86 dyoung
502 1.86 dyoung #if 0
503 1.86 dyoung printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
504 1.86 dyoung secperunit, geom_ok ? "ok" : "not ok");
505 1.86 dyoung #endif
506 1.86 dyoung
507 1.86 dyoung p = &lp->d_partitions[RAW_PART];
508 1.86 dyoung if (RAW_PART == 'c' - 'a')
509 1.86 dyoung altp = &lp->d_partitions['d' - 'a'];
510 1.86 dyoung else
511 1.86 dyoung altp = &lp->d_partitions['c' - 'a'];
512 1.86 dyoung
513 1.86 dyoung if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
514 1.100.18.7 jdolecek return NULL; /* already a raw partition */
515 1.86 dyoung else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
516 1.86 dyoung altp->p_offset == 0 && altp->p_size != 0) {
517 1.86 dyoung /* alternate partition ('c' or 'd') is suitable for raw slot,
518 1.86 dyoung * swap with 'd' or 'c'.
519 1.86 dyoung */
520 1.86 dyoung rp = *p;
521 1.86 dyoung *p = *altp;
522 1.86 dyoung *altp = rp;
523 1.100.18.7 jdolecek return NULL;
524 1.86 dyoung } else if (lp->d_npartitions <= RAW_PART &&
525 1.86 dyoung lp->d_npartitions > 'c' - 'a') {
526 1.86 dyoung /* No raw partition is present, but the alternate is present.
527 1.86 dyoung * Copy alternate to raw partition.
528 1.86 dyoung */
529 1.86 dyoung lp->d_npartitions = RAW_PART + 1;
530 1.86 dyoung *p = *altp;
531 1.100.18.7 jdolecek return NULL;
532 1.86 dyoung } else if (!geom_ok)
533 1.100.18.7 jdolecek str = "no raw partition and disk reports bad geometry";
534 1.86 dyoung else if (lp->d_npartitions <= RAW_PART) {
535 1.86 dyoung memset(&lp->d_partitions[lp->d_npartitions], 0,
536 1.86 dyoung sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
537 1.86 dyoung *p = rp;
538 1.86 dyoung lp->d_npartitions = RAW_PART + 1;
539 1.100.18.7 jdolecek return NULL;
540 1.86 dyoung } else if (lp->d_npartitions < MAXPARTITIONS) {
541 1.86 dyoung memmove(p + 1, p,
542 1.86 dyoung sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
543 1.86 dyoung *p = rp;
544 1.86 dyoung lp->d_npartitions++;
545 1.100.18.7 jdolecek return NULL;
546 1.86 dyoung } else
547 1.100.18.7 jdolecek str = "no raw partition and partition table is full";
548 1.100.18.7 jdolecek #ifdef DIAGNOSTIC
549 1.100.18.7 jdolecek printf("Bad partition: %s\n", str);
550 1.100.18.7 jdolecek printf("type = %u, subtype = %u, typename = %s\n",
551 1.100.18.7 jdolecek lp->d_type, lp->d_subtype, lp->d_typename);
552 1.100.18.7 jdolecek printf("secsize = %u, nsectors = %u, ntracks = %u\n",
553 1.100.18.7 jdolecek lp->d_secsize, lp->d_nsectors, lp->d_ntracks);
554 1.100.18.7 jdolecek printf("ncylinders = %u, secpercyl = %u, secperunit = %u\n",
555 1.100.18.7 jdolecek lp->d_ncylinders, lp->d_secpercyl, lp->d_secperunit);
556 1.100.18.7 jdolecek printf("npartitions = %u\n", lp->d_npartitions);
557 1.100.18.7 jdolecek
558 1.100.18.7 jdolecek for (size_t i = 0; i < MIN(lp->d_npartitions, MAXPARTITIONS); i++) {
559 1.100.18.7 jdolecek p = &lp->d_partitions[i];
560 1.100.18.7 jdolecek printf("\t%c: offset = %u size = %u fstype = %u\n",
561 1.100.18.7 jdolecek (char)(i + 'a'), p->p_offset, p->p_size, p->p_fstype);
562 1.100.18.7 jdolecek }
563 1.100.18.7 jdolecek #endif
564 1.100.18.7 jdolecek return str;
565 1.86 dyoung }
566 1.86 dyoung
567 1.83 scw /*
568 1.82 thorpej * disk_ioctl --
569 1.82 thorpej * Generic disk ioctl handling.
570 1.82 thorpej */
571 1.82 thorpej int
572 1.100.18.7 jdolecek disk_ioctl(struct disk *dk, dev_t dev, u_long cmd, void *data, int flag,
573 1.100.18.7 jdolecek struct lwp *l)
574 1.82 thorpej {
575 1.100.18.7 jdolecek struct dkwedge_info *dkw;
576 1.100.18.7 jdolecek struct partinfo *pi;
577 1.100.18.7 jdolecek struct partition *dp;
578 1.100.18.7 jdolecek #ifdef __HAVE_OLD_DISKLABEL
579 1.100.18.7 jdolecek struct disklabel newlabel;
580 1.100.18.7 jdolecek #endif
581 1.82 thorpej
582 1.82 thorpej switch (cmd) {
583 1.82 thorpej case DIOCGDISKINFO:
584 1.100.18.7 jdolecek if (dk->dk_info == NULL)
585 1.100.18.7 jdolecek return ENOTSUP;
586 1.100.18.7 jdolecek return prop_dictionary_copyout_ioctl(data, cmd, dk->dk_info);
587 1.100.18.7 jdolecek
588 1.100.18.7 jdolecek case DIOCGSECTORSIZE:
589 1.100.18.7 jdolecek *(u_int *)data = dk->dk_geom.dg_secsize;
590 1.100.18.7 jdolecek return 0;
591 1.100.18.7 jdolecek
592 1.100.18.7 jdolecek case DIOCGMEDIASIZE:
593 1.100.18.7 jdolecek *(off_t *)data = (off_t)dk->dk_geom.dg_secsize *
594 1.100.18.7 jdolecek dk->dk_geom.dg_secperunit;
595 1.100.18.7 jdolecek return 0;
596 1.100.18.7 jdolecek default:
597 1.82 thorpej break;
598 1.100.18.7 jdolecek }
599 1.100.18.7 jdolecek
600 1.100.18.7 jdolecek if (dev == NODEV)
601 1.100.18.7 jdolecek return EPASSTHROUGH;
602 1.100.18.7 jdolecek
603 1.100.18.7 jdolecek /* The following should be moved to dk_ioctl */
604 1.100.18.7 jdolecek switch (cmd) {
605 1.100.18.7 jdolecek case DIOCGDINFO:
606 1.100.18.7 jdolecek if (dk->dk_label == NULL)
607 1.100.18.7 jdolecek return EBUSY;
608 1.100.18.7 jdolecek memcpy(data, dk->dk_label, sizeof (*dk->dk_label));
609 1.100.18.7 jdolecek return 0;
610 1.100.18.7 jdolecek
611 1.100.18.7 jdolecek #ifdef __HAVE_OLD_DISKLABEL
612 1.100.18.7 jdolecek case ODIOCGDINFO:
613 1.100.18.7 jdolecek if (dk->dk_label == NULL)
614 1.100.18.7 jdolecek return EBUSY;
615 1.100.18.7 jdolecek memcpy(&newlabel, dk->dk_label, sizeof(newlabel));
616 1.100.18.7 jdolecek if (newlabel.d_npartitions > OLDMAXPARTITIONS)
617 1.100.18.7 jdolecek return ENOTTY;
618 1.100.18.7 jdolecek memcpy(data, &newlabel, sizeof(struct olddisklabel));
619 1.100.18.7 jdolecek return 0;
620 1.100.18.7 jdolecek #endif
621 1.100.18.7 jdolecek
622 1.100.18.7 jdolecek case DIOCGPARTINFO:
623 1.100.18.7 jdolecek pi = data;
624 1.100.18.7 jdolecek memset(pi, 0, sizeof(*pi));
625 1.100.18.7 jdolecek pi->pi_secsize = dk->dk_geom.dg_secsize;
626 1.100.18.7 jdolecek pi->pi_bsize = MAX(BLKDEV_IOSIZE, pi->pi_secsize);
627 1.100.18.7 jdolecek
628 1.100.18.7 jdolecek if (DISKPART(dev) == RAW_PART) {
629 1.100.18.7 jdolecek pi->pi_size = dk->dk_geom.dg_secperunit;
630 1.100.18.7 jdolecek return 0;
631 1.100.18.7 jdolecek }
632 1.100.18.7 jdolecek
633 1.100.18.7 jdolecek if (dk->dk_label == NULL)
634 1.100.18.7 jdolecek return EBUSY;
635 1.100.18.7 jdolecek
636 1.100.18.7 jdolecek dp = &dk->dk_label->d_partitions[DISKPART(dev)];
637 1.100.18.7 jdolecek pi->pi_offset = dp->p_offset;
638 1.100.18.7 jdolecek pi->pi_size = dp->p_size;
639 1.100.18.7 jdolecek
640 1.100.18.7 jdolecek pi->pi_fstype = dp->p_fstype;
641 1.100.18.7 jdolecek pi->pi_frag = dp->p_frag;
642 1.100.18.7 jdolecek pi->pi_fsize = dp->p_fsize;
643 1.100.18.7 jdolecek pi->pi_cpg = dp->p_cpg;
644 1.100.18.7 jdolecek
645 1.100.18.7 jdolecek /*
646 1.100.18.7 jdolecek * dholland 20130616: XXX this logic should not be
647 1.100.18.7 jdolecek * here. It is here because the old buffer cache
648 1.100.18.7 jdolecek * demands that all accesses to the same blocks need
649 1.100.18.7 jdolecek * to be the same size; but it only works for FFS and
650 1.100.18.7 jdolecek * nowadays I think it'll fail silently if the size
651 1.100.18.7 jdolecek * info in the disklabel is wrong. (Or missing.) The
652 1.100.18.7 jdolecek * buffer cache needs to be smarter; or failing that
653 1.100.18.7 jdolecek * we need a reliable way here to get the right block
654 1.100.18.7 jdolecek * size; or a reliable way to guarantee that (a) the
655 1.100.18.7 jdolecek * fs is not mounted when we get here and (b) any
656 1.100.18.7 jdolecek * buffers generated here will get purged when the fs
657 1.100.18.7 jdolecek * does get mounted.
658 1.100.18.7 jdolecek */
659 1.100.18.7 jdolecek if (dp->p_fstype == FS_BSDFFS &&
660 1.100.18.7 jdolecek dp->p_frag != 0 && dp->p_fsize != 0)
661 1.100.18.7 jdolecek pi->pi_bsize = dp->p_frag * dp->p_fsize;
662 1.100.18.7 jdolecek return 0;
663 1.100.18.7 jdolecek
664 1.100.18.7 jdolecek case DIOCAWEDGE:
665 1.100.18.7 jdolecek if ((flag & FWRITE) == 0)
666 1.100.18.7 jdolecek return EBADF;
667 1.100.18.7 jdolecek
668 1.100.18.7 jdolecek dkw = data;
669 1.100.18.7 jdolecek strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
670 1.100.18.7 jdolecek return dkwedge_add(dkw);
671 1.100.18.7 jdolecek
672 1.100.18.7 jdolecek case DIOCDWEDGE:
673 1.100.18.7 jdolecek if ((flag & FWRITE) == 0)
674 1.100.18.7 jdolecek return EBADF;
675 1.100.18.7 jdolecek
676 1.100.18.7 jdolecek dkw = data;
677 1.100.18.7 jdolecek strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
678 1.100.18.7 jdolecek return dkwedge_del(dkw);
679 1.100.18.7 jdolecek
680 1.100.18.7 jdolecek case DIOCLWEDGES:
681 1.100.18.7 jdolecek return dkwedge_list(dk, data, l);
682 1.100.18.7 jdolecek
683 1.100.18.7 jdolecek case DIOCMWEDGES:
684 1.100.18.7 jdolecek if ((flag & FWRITE) == 0)
685 1.100.18.7 jdolecek return EBADF;
686 1.100.18.7 jdolecek
687 1.100.18.7 jdolecek dkwedge_discover(dk);
688 1.100.18.7 jdolecek return 0;
689 1.82 thorpej
690 1.82 thorpej default:
691 1.100.18.7 jdolecek return EPASSTHROUGH;
692 1.82 thorpej }
693 1.82 thorpej }
694 1.100.18.5 tls
695 1.100.18.5 tls void
696 1.100.18.5 tls disk_set_info(device_t dev, struct disk *dk, const char *type)
697 1.100.18.5 tls {
698 1.100.18.5 tls struct disk_geom *dg = &dk->dk_geom;
699 1.100.18.5 tls
700 1.100.18.7 jdolecek if (dg->dg_secsize == 0) {
701 1.100.18.7 jdolecek #ifdef DIAGNOSTIC
702 1.100.18.7 jdolecek printf("%s: fixing 0 sector size\n", dk->dk_name);
703 1.100.18.7 jdolecek #endif
704 1.100.18.7 jdolecek dg->dg_secsize = DEV_BSIZE;
705 1.100.18.7 jdolecek }
706 1.100.18.7 jdolecek
707 1.100.18.7 jdolecek dk->dk_blkshift = DK_BSIZE2BLKSHIFT(dg->dg_secsize);
708 1.100.18.7 jdolecek dk->dk_byteshift = DK_BSIZE2BYTESHIFT(dg->dg_secsize);
709 1.100.18.7 jdolecek
710 1.100.18.5 tls if (dg->dg_secperunit == 0 && dg->dg_ncylinders == 0) {
711 1.100.18.5 tls #ifdef DIAGNOSTIC
712 1.100.18.5 tls printf("%s: secperunit and ncylinders are zero\n", dk->dk_name);
713 1.100.18.5 tls #endif
714 1.100.18.5 tls return;
715 1.100.18.5 tls }
716 1.100.18.5 tls
717 1.100.18.5 tls if (dg->dg_secperunit == 0) {
718 1.100.18.5 tls if (dg->dg_nsectors == 0 || dg->dg_ntracks == 0) {
719 1.100.18.5 tls #ifdef DIAGNOSTIC
720 1.100.18.5 tls printf("%s: secperunit and (sectors or tracks) "
721 1.100.18.5 tls "are zero\n", dk->dk_name);
722 1.100.18.5 tls #endif
723 1.100.18.5 tls return;
724 1.100.18.5 tls }
725 1.100.18.6 tls dg->dg_secperunit = (int64_t) dg->dg_nsectors *
726 1.100.18.5 tls dg->dg_ntracks * dg->dg_ncylinders;
727 1.100.18.5 tls }
728 1.100.18.5 tls
729 1.100.18.5 tls if (dg->dg_ncylinders == 0) {
730 1.100.18.5 tls if (dg->dg_ntracks && dg->dg_nsectors)
731 1.100.18.5 tls dg->dg_ncylinders = dg->dg_secperunit /
732 1.100.18.5 tls (dg->dg_ntracks * dg->dg_nsectors);
733 1.100.18.5 tls }
734 1.100.18.5 tls
735 1.100.18.5 tls prop_dictionary_t disk_info, odisk_info, geom;
736 1.100.18.5 tls
737 1.100.18.5 tls disk_info = prop_dictionary_create();
738 1.100.18.5 tls geom = prop_dictionary_create();
739 1.100.18.5 tls
740 1.100.18.5 tls prop_dictionary_set_uint64(geom, "sectors-per-unit",
741 1.100.18.5 tls dg->dg_secperunit);
742 1.100.18.5 tls
743 1.100.18.5 tls prop_dictionary_set_uint32(geom, "sector-size", dg->dg_secsize);
744 1.100.18.5 tls
745 1.100.18.5 tls if (dg->dg_nsectors)
746 1.100.18.5 tls prop_dictionary_set_uint16(geom, "sectors-per-track",
747 1.100.18.5 tls dg->dg_nsectors);
748 1.100.18.5 tls
749 1.100.18.5 tls if (dg->dg_ntracks)
750 1.100.18.5 tls prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
751 1.100.18.5 tls dg->dg_ntracks);
752 1.100.18.5 tls
753 1.100.18.5 tls if (dg->dg_ncylinders)
754 1.100.18.5 tls prop_dictionary_set_uint64(geom, "cylinders-per-unit",
755 1.100.18.5 tls dg->dg_ncylinders);
756 1.100.18.5 tls
757 1.100.18.5 tls prop_dictionary_set(disk_info, "geometry", geom);
758 1.100.18.5 tls
759 1.100.18.5 tls if (type)
760 1.100.18.5 tls prop_dictionary_set_cstring_nocopy(disk_info, "type", type);
761 1.100.18.5 tls
762 1.100.18.5 tls prop_object_release(geom);
763 1.100.18.5 tls
764 1.100.18.5 tls odisk_info = dk->dk_info;
765 1.100.18.5 tls dk->dk_info = disk_info;
766 1.100.18.5 tls
767 1.100.18.5 tls if (dev)
768 1.100.18.5 tls prop_dictionary_set(device_properties(dev), "disk-info",
769 1.100.18.5 tls disk_info);
770 1.100.18.5 tls
771 1.100.18.5 tls /*
772 1.100.18.5 tls * Don't release disk_info here; we keep a reference to it.
773 1.100.18.5 tls * disk_detach() will release it when we go away.
774 1.100.18.5 tls */
775 1.100.18.5 tls if (odisk_info)
776 1.100.18.5 tls prop_object_release(odisk_info);
777 1.100.18.5 tls }
778