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