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