subr_disk.c revision 1.100.18.1 1 /* $NetBSD: subr_disk.c,v 1.100.18.1 2012/09/12 06:15:34 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.1 2012/09/12 06:15:34 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 /*
84 * Compute checksum for disk label.
85 */
86 u_int
87 dkcksum(struct disklabel *lp)
88 {
89
90 return dkcksum_sized(lp, lp->d_npartitions);
91 }
92
93 u_int
94 dkcksum_sized(struct disklabel *lp, size_t npartitions)
95 {
96 uint16_t *start, *end;
97 uint16_t sum = 0;
98
99 start = (uint16_t *)lp;
100 end = (uint16_t *)&lp->d_partitions[npartitions];
101 while (start < end)
102 sum ^= *start++;
103 return sum;
104 }
105
106 /*
107 * Disk error is the preface to plaintive error messages
108 * about failing disk transfers. It prints messages of the form
109
110 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
111
112 * if the offset of the error in the transfer and a disk label
113 * are both available. blkdone should be -1 if the position of the error
114 * is unknown; the disklabel pointer may be null from drivers that have not
115 * been converted to use them. The message is printed with printf
116 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
117 * The message should be completed (with at least a newline) with printf
118 * or addlog, respectively. There is no trailing space.
119 */
120 #ifndef PRIdaddr
121 #define PRIdaddr PRId64
122 #endif
123 void
124 diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
125 int blkdone, const struct disklabel *lp)
126 {
127 int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
128 void (*pr)(const char *, ...);
129 char partname = 'a' + part;
130 daddr_t sn;
131
132 if (/*CONSTCOND*/0)
133 /* Compiler will error this is the format is wrong... */
134 printf("%" PRIdaddr, bp->b_blkno);
135
136 if (pri != LOG_PRINTF) {
137 static const char fmt[] = "";
138 log(pri, fmt);
139 pr = addlog;
140 } else
141 pr = printf;
142 (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
143 bp->b_flags & B_READ ? "read" : "writ");
144 sn = bp->b_blkno;
145 if (bp->b_bcount <= DEV_BSIZE)
146 (*pr)("%" PRIdaddr, sn);
147 else {
148 if (blkdone >= 0) {
149 sn += blkdone;
150 (*pr)("%" PRIdaddr " of ", sn);
151 }
152 (*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
153 bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
154 }
155 if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
156 sn += lp->d_partitions[part].p_offset;
157 (*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
158 dname, unit, sn, sn / lp->d_secpercyl);
159 sn %= lp->d_secpercyl;
160 (*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
161 sn / lp->d_nsectors, sn % lp->d_nsectors);
162 }
163 }
164
165 /*
166 * Searches the iostatlist for the disk corresponding to the
167 * name provided.
168 */
169 struct disk *
170 disk_find(const char *name)
171 {
172 struct io_stats *stat;
173
174 stat = iostat_find(name);
175
176 if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
177 return stat->io_parent;
178
179 return (NULL);
180 }
181
182 /*
183 * Searches for the disk corresponding to a supplied block device
184 * using major, minor, unit.
185 */
186 struct disk *
187 disk_find_blk(dev_t dev)
188 {
189 devmajor_t major;
190 int unit;
191 char name[16]; /* XXX */
192 const char *swname;
193
194 major = major(dev);
195 unit = DISKUNIT(dev);
196
197 if ((swname = devsw_blk2name(major)) == NULL) {
198 return NULL;
199 }
200
201 if (snprintf(name, sizeof(name), "%s%d",
202 swname, unit) > sizeof(name)) {
203 return NULL;
204 }
205
206 return disk_find(name);
207 }
208
209 int disk_maxphys(const struct disk *const diskp)
210 {
211 struct buf b = { b_bcount: MACHINE_MAXPHYS };
212
213 diskp->dk_driver->d_minphys(&b);
214
215 return b.b_bcount;
216 }
217
218 void
219 disk_init(struct disk *diskp, const char *name, const struct dkdriver *driver)
220 {
221
222 /*
223 * Initialize the wedge-related locks and other fields.
224 */
225 mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
226 mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
227 LIST_INIT(&diskp->dk_wedges);
228 diskp->dk_nwedges = 0;
229 diskp->dk_labelsector = LABELSECTOR;
230 disk_blocksize(diskp, DEV_BSIZE);
231 diskp->dk_name = name;
232 diskp->dk_driver = driver;
233 }
234
235 /*
236 * Attach a disk.
237 */
238 void
239 disk_attach(struct disk *diskp)
240 {
241
242 /*
243 * Allocate and initialize the disklabel structures.
244 */
245 diskp->dk_label = kmem_zalloc(sizeof(struct disklabel), KM_SLEEP);
246 diskp->dk_cpulabel = kmem_zalloc(sizeof(struct cpu_disklabel),
247 KM_SLEEP);
248 if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
249 panic("disk_attach: can't allocate storage for disklabel");
250
251 /*
252 * Set up the stats collection.
253 */
254 diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
255 }
256
257 int
258 disk_begindetach(struct disk *dk, int (*lastclose)(device_t),
259 device_t self, int flags)
260 {
261 int rc;
262
263 rc = 0;
264 mutex_enter(&dk->dk_openlock);
265 if (dk->dk_openmask == 0)
266 ; /* nothing to do */
267 else if ((flags & DETACH_FORCE) == 0)
268 rc = EBUSY;
269 else if (lastclose != NULL)
270 rc = (*lastclose)(self);
271 mutex_exit(&dk->dk_openlock);
272
273 return rc;
274 }
275
276 /*
277 * Detach a disk.
278 */
279 void
280 disk_detach(struct disk *diskp)
281 {
282
283 /*
284 * Remove from the drivelist.
285 */
286 iostat_free(diskp->dk_stats);
287
288 /*
289 * Release the disk-info dictionary.
290 */
291 if (diskp->dk_info) {
292 prop_object_release(diskp->dk_info);
293 diskp->dk_info = NULL;
294 }
295
296 /*
297 * Free the space used by the disklabel structures.
298 */
299 kmem_free(diskp->dk_label, sizeof(*diskp->dk_label));
300 kmem_free(diskp->dk_cpulabel, sizeof(*diskp->dk_cpulabel));
301 }
302
303 void
304 disk_destroy(struct disk *diskp)
305 {
306
307 mutex_destroy(&diskp->dk_openlock);
308 mutex_destroy(&diskp->dk_rawlock);
309 }
310
311 /*
312 * Mark the disk as busy for metrics collection.
313 */
314 void
315 disk_busy(struct disk *diskp)
316 {
317
318 iostat_busy(diskp->dk_stats);
319 }
320
321 /*
322 * Finished disk operations, gather metrics.
323 */
324 void
325 disk_unbusy(struct disk *diskp, long bcount, int read)
326 {
327
328 iostat_unbusy(diskp->dk_stats, bcount, read);
329 }
330
331 /*
332 * Return true if disk has an I/O operation in flight.
333 */
334 bool
335 disk_isbusy(struct disk *diskp)
336 {
337
338 return iostat_isbusy(diskp->dk_stats);
339 }
340
341 /*
342 * Set the physical blocksize of a disk, in bytes.
343 * Only necessary if blocksize != DEV_BSIZE.
344 */
345 void
346 disk_blocksize(struct disk *diskp, int blocksize)
347 {
348
349 diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
350 diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
351 }
352
353 /*
354 * Bounds checking against the media size, used for the raw partition.
355 * secsize, mediasize and b_blkno must all be the same units.
356 * Possibly this has to be DEV_BSIZE (512).
357 */
358 int
359 bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
360 {
361 int64_t sz;
362
363 sz = howmany(bp->b_bcount, secsize);
364
365 if (bp->b_blkno + sz > mediasize) {
366 sz = mediasize - bp->b_blkno;
367 if (sz == 0) {
368 /* If exactly at end of disk, return EOF. */
369 bp->b_resid = bp->b_bcount;
370 return 0;
371 }
372 if (sz < 0) {
373 /* If past end of disk, return EINVAL. */
374 bp->b_error = EINVAL;
375 return 0;
376 }
377 /* Otherwise, truncate request. */
378 bp->b_bcount = sz * secsize;
379 }
380
381 return 1;
382 }
383
384 /*
385 * Determine the size of the transfer, and make sure it is
386 * within the boundaries of the partition. Adjust transfer
387 * if needed, and signal errors or early completion.
388 */
389 int
390 bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
391 {
392 struct disklabel *lp = dk->dk_label;
393 struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
394 uint64_t p_size, p_offset, labelsector;
395 int64_t sz;
396
397 /* Protect against division by zero. XXX: Should never happen?!?! */
398 if (lp->d_secpercyl == 0) {
399 bp->b_error = EINVAL;
400 return -1;
401 }
402
403 p_size = (uint64_t)p->p_size << dk->dk_blkshift;
404 p_offset = (uint64_t)p->p_offset << dk->dk_blkshift;
405 #if RAW_PART == 3
406 labelsector = lp->d_partitions[2].p_offset;
407 #else
408 labelsector = lp->d_partitions[RAW_PART].p_offset;
409 #endif
410 labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
411
412 sz = howmany(bp->b_bcount, DEV_BSIZE);
413 if ((bp->b_blkno + sz) > p_size) {
414 sz = p_size - bp->b_blkno;
415 if (sz == 0) {
416 /* If exactly at end of disk, return EOF. */
417 bp->b_resid = bp->b_bcount;
418 return 0;
419 }
420 if (sz < 0) {
421 /* If past end of disk, return EINVAL. */
422 bp->b_error = EINVAL;
423 return -1;
424 }
425 /* Otherwise, truncate request. */
426 bp->b_bcount = sz << DEV_BSHIFT;
427 }
428
429 /* Overwriting disk label? */
430 if (bp->b_blkno + p_offset <= labelsector &&
431 bp->b_blkno + p_offset + sz > labelsector &&
432 (bp->b_flags & B_READ) == 0 && !wlabel) {
433 bp->b_error = EROFS;
434 return -1;
435 }
436
437 /* calculate cylinder for disksort to order transfers with */
438 bp->b_cylinder = (bp->b_blkno + p->p_offset) /
439 (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
440 return 1;
441 }
442
443 int
444 disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
445 struct buf *bp, unsigned int sector, int count)
446 {
447 bp->b_blkno = sector;
448 bp->b_bcount = count * lp->d_secsize;
449 bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
450 bp->b_oflags &= ~BO_DONE;
451 bp->b_cylinder = sector / lp->d_secpercyl;
452 (*strat)(bp);
453 return biowait(bp);
454 }
455
456 const char *
457 convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
458 struct buf *bp, uint32_t secperunit)
459 {
460 struct partition rp, *altp, *p;
461 int geom_ok;
462
463 memset(&rp, 0, sizeof(rp));
464 rp.p_size = secperunit;
465 rp.p_fstype = FS_UNUSED;
466
467 /* If we can seek to d_secperunit - 1, believe the disk geometry. */
468 if (secperunit != 0 &&
469 disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
470 geom_ok = 1;
471 else
472 geom_ok = 0;
473
474 #if 0
475 printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
476 secperunit, geom_ok ? "ok" : "not ok");
477 #endif
478
479 p = &lp->d_partitions[RAW_PART];
480 if (RAW_PART == 'c' - 'a')
481 altp = &lp->d_partitions['d' - 'a'];
482 else
483 altp = &lp->d_partitions['c' - 'a'];
484
485 if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
486 ; /* already a raw partition */
487 else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
488 altp->p_offset == 0 && altp->p_size != 0) {
489 /* alternate partition ('c' or 'd') is suitable for raw slot,
490 * swap with 'd' or 'c'.
491 */
492 rp = *p;
493 *p = *altp;
494 *altp = rp;
495 } else if (lp->d_npartitions <= RAW_PART &&
496 lp->d_npartitions > 'c' - 'a') {
497 /* No raw partition is present, but the alternate is present.
498 * Copy alternate to raw partition.
499 */
500 lp->d_npartitions = RAW_PART + 1;
501 *p = *altp;
502 } else if (!geom_ok)
503 return "no raw partition and disk reports bad geometry";
504 else if (lp->d_npartitions <= RAW_PART) {
505 memset(&lp->d_partitions[lp->d_npartitions], 0,
506 sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
507 *p = rp;
508 lp->d_npartitions = RAW_PART + 1;
509 } else if (lp->d_npartitions < MAXPARTITIONS) {
510 memmove(p + 1, p,
511 sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
512 *p = rp;
513 lp->d_npartitions++;
514 } else
515 return "no raw partition and partition table is full";
516 return NULL;
517 }
518
519 /*
520 * disk_ioctl --
521 * Generic disk ioctl handling.
522 */
523 int
524 disk_ioctl(struct disk *diskp, u_long cmd, void *data, int flag,
525 struct lwp *l)
526 {
527 int error;
528
529 switch (cmd) {
530 case DIOCGDISKINFO:
531 {
532 struct plistref *pref = (struct plistref *) data;
533
534 if (diskp->dk_info == NULL)
535 error = ENOTSUP;
536 else
537 error = prop_dictionary_copyout_ioctl(pref, cmd,
538 diskp->dk_info);
539 break;
540 }
541
542 default:
543 error = EPASSTHROUGH;
544 }
545
546 return (error);
547 }
548