2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * Copyright (c) 2004 Matthew Dillon.
10 * Copyright (c) 1982, 1986, 1988, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
47 * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
48 * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
49 * $DragonFly: src/sys/kern/subr_disk.c,v 1.12 2004/06/02 19:31:02 dillon Exp $
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/sysctl.h>
59 #include <sys/disklabel.h>
60 #include <sys/diskslice.h>
62 #include <sys/malloc.h>
63 #include <sys/sysctl.h>
64 #include <machine/md_var.h>
65 #include <sys/ctype.h>
66 #include <sys/syslog.h>
67 #include <sys/device.h>
68 #include <sys/msgport.h>
69 #include <sys/msgport2.h>
72 static MALLOC_DEFINE(M_DISK, "disk", "disk data");
74 static d_strategy_t diskstrategy;
75 static d_open_t diskopen;
76 static d_close_t diskclose;
77 static d_ioctl_t diskioctl;
78 static d_psize_t diskpsize;
79 static d_clone_t diskclone;
80 static int disk_putport(lwkt_port_t port, lwkt_msg_t msg);
82 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
85 * Create a slice and unit managed disk.
87 * Our port layer will be responsible for assigning pblkno and handling
88 * high level partition operations, then forwarding the requests to the
91 * The raw device (based on rawsw) is returned to the caller, NOT the
92 * slice and unit managed cdev. The caller typically sets various
93 * driver parameters and IO limits on the returned rawdev which we must
94 * inherit when our managed device is opened.
97 disk_create(int unit, struct disk *dp, int flags, struct cdevsw *rawsw)
100 struct cdevsw *devsw;
103 * Create the raw backing device
105 compile_devsw(rawsw);
106 rawdev = make_dev(rawsw, dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
107 UID_ROOT, GID_OPERATOR, 0640,
108 "%s%d", rawsw->d_name, unit);
111 * Initialize our intercept port
113 bzero(dp, sizeof(*dp));
114 lwkt_initport(&dp->d_port, NULL);
115 dp->d_port.mp_putport = disk_putport;
119 * We install a custom cdevsw rather then the passed cdevsw,
120 * and save our disk structure in d_data so we can get at it easily
121 * without any complex cloning code.
123 devsw = cdevsw_add_override(rawdev, dkunitmask(), dkmakeunit(unit));
124 devsw->d_port = &dp->d_port;
126 devsw->d_clone = diskclone;
128 dp->d_rawdev = rawdev;
129 dp->d_cdev = make_dev(devsw,
130 dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
131 UID_ROOT, GID_OPERATOR, 0640,
132 "%s%d", devsw->d_name, unit);
134 dp->d_dsflags = flags;
135 LIST_INSERT_HEAD(&disklist, dp, d_list);
136 return (dp->d_rawdev);
140 * This routine is called when an adapter detaches. The higher level
141 * managed disk device is destroyed while the lower level raw device is
145 disk_destroy(struct disk *disk)
148 cdevsw_remove(disk->d_devsw, dkunitmask(), dkunit(disk->d_cdev));
149 LIST_REMOVE(disk, d_list);
152 destroy_all_dev(disk->d_rawsw, dkunitmask(), dkunit(disk->d_rawdev));
153 bzero(disk, sizeof(*disk));
157 disk_dumpcheck(dev_t dev, u_int *count, u_int *blkno, u_int *secsize)
160 struct disklabel *dl;
168 dl = dsgetlabel(dev, dp->d_slice);
171 *count = Maxmem * (PAGE_SIZE / dl->d_secsize);
172 if (dumplo <= LABELSECTOR ||
173 (dumplo + *count > dl->d_partitions[dkpart(dev)].p_size))
175 boff = dl->d_partitions[dkpart(dev)].p_offset +
176 dp->d_slice->dss_slices[dkslice(dev)].ds_offset;
177 *blkno = boff + dumplo;
178 *secsize = dl->d_secsize;
184 disk_invalidate (struct disk *disk)
187 dsgone(&disk->d_slice);
191 disk_enumerate(struct disk *disk)
194 return (LIST_FIRST(&disklist));
196 return (LIST_NEXT(disk, d_list));
201 sysctl_disks(SYSCTL_HANDLER_ARGS)
209 while ((disk = disk_enumerate(disk))) {
211 error = SYSCTL_OUT(req, " ", 1);
217 error = SYSCTL_OUT(req, disk->d_rawdev->si_name, strlen(disk->d_rawdev->si_name));
221 error = SYSCTL_OUT(req, "", 1);
225 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, 0, NULL,
226 sysctl_disks, "A", "names of available disks");
229 * The port intercept functions
233 disk_putport(lwkt_port_t port, lwkt_msg_t lmsg)
235 struct disk *disk = (struct disk *)port;
236 cdevallmsg_t msg = (cdevallmsg_t)lmsg;
239 switch(msg->am_lmsg.ms_cmd.cm_op) {
242 msg->am_open.msg.dev,
244 msg->am_open.devtype,
249 msg->am_close.msg.dev,
251 msg->am_close.devtype,
256 msg->am_ioctl.msg.dev,
262 case CDEV_CMD_STRATEGY:
263 diskstrategy(msg->am_strategy.bp);
267 msg->am_psize.result = diskpsize(msg->am_psize.msg.dev);
271 error = physio(msg->am_read.msg.dev,
272 msg->am_read.uio, msg->am_read.ioflag);
275 error = physio(msg->am_write.msg.dev,
276 msg->am_write.uio, msg->am_write.ioflag);
279 case CDEV_CMD_KQFILTER:
285 error = disk_dumpcheck(msg->am_dump.msg.dev,
288 &msg->am_dump.secsize);
290 msg->am_dump.msg.dev = disk->d_rawdev;
291 error = lwkt_forwardmsg(disk->d_rawdev->si_port,
292 &msg->am_dump.msg.msg);
293 printf("error2 %d\n", error);
304 * When new device entries are instantiated, make sure they inherit our
305 * si_disk structure and block and iosize limits from the raw device.
307 * This routine is always called synchronously in the context of the
310 * XXX The various io and block size constraints are not always initialized
311 * properly by devices.
319 dp = dev->si_devsw->d_data;
320 KKASSERT(dp != NULL);
322 dev->si_iosize_max = dp->d_rawdev->si_iosize_max;
323 dev->si_bsize_phys = dp->d_rawdev->si_bsize_phys;
324 dev->si_bsize_best = dp->d_rawdev->si_bsize_best;
329 * Open a disk device or partition.
333 diskopen(dev_t dev, int oflags, int devtype, struct thread *td)
339 * dp can't be NULL here XXX.
347 * Deal with open races
349 while (dp->d_flags & DISKFLAG_LOCK) {
350 dp->d_flags |= DISKFLAG_WANTED;
351 error = tsleep(dp, PCATCH, "diskopen", hz);
355 dp->d_flags |= DISKFLAG_LOCK;
358 * Open the underlying raw device.
360 if (!dsisopen(dp->d_slice)) {
362 if (!pdev->si_iosize_max)
363 pdev->si_iosize_max = dev->si_iosize_max;
365 error = dev_dopen(dp->d_rawdev, oflags, devtype, td);
369 * Inherit properties from the underlying device now that it is
377 error = dsopen(dev, devtype, dp->d_dsflags, &dp->d_slice, &dp->d_label);
379 if (!dsisopen(dp->d_slice))
380 dev_dclose(dp->d_rawdev, oflags, devtype, td);
382 dp->d_flags &= ~DISKFLAG_LOCK;
383 if (dp->d_flags & DISKFLAG_WANTED) {
384 dp->d_flags &= ~DISKFLAG_WANTED;
392 * Close a disk device or partition
396 diskclose(dev_t dev, int fflag, int devtype, struct thread *td)
404 dsclose(dev, devtype, dp->d_slice);
405 if (!dsisopen(dp->d_slice))
406 error = dev_dclose(dp->d_rawdev, fflag, devtype, td);
411 * Execute strategy routine
415 diskstrategy(struct buf *bp)
419 dp = bp->b_dev->si_disk;
423 bp->b_flags |= B_ERROR;
427 KKASSERT(bp->b_dev->si_disk == dp);
429 if (dscheck(bp, dp->d_slice) <= 0) {
433 bp->b_dev = dp->d_rawdev;
434 dev_dstrategy(dp->d_rawdev, bp);
438 * First execute the ioctl on the disk device, and if it isn't supported
439 * try running it on the backing device.
443 diskioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
452 error = dsioctl(dev, cmd, data, fflag, &dp->d_slice);
453 if (error == ENOIOCTL)
454 error = dev_dioctl(dp->d_rawdev, cmd, data, fflag, td);
470 return(dssize(dev, &dp->d_slice));
472 if (dp != dev->si_disk) {
473 dev->si_drv1 = pdev->si_drv1;
474 dev->si_drv2 = pdev->si_drv2;
475 /* XXX: don't set bp->b_dev->si_disk (?) */
480 SYSCTL_DECL(_debug_sizeof);
482 SYSCTL_INT(_debug_sizeof, OID_AUTO, disklabel, CTLFLAG_RD,
483 0, sizeof(struct disklabel), "sizeof(struct disklabel)");
485 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
486 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
488 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
489 0, sizeof(struct disk), "sizeof(struct disk)");
493 * Seek sort for disks.
495 * The buf_queue keep two queues, sorted in ascending block order. The first
496 * queue holds those requests which are positioned after the current block
497 * (in the first request); the second, which starts at queue->switch_point,
498 * holds requests which came in after their block number was passed. Thus
499 * we implement a one way scan, retracting after reaching the end of the drive
500 * to the first request on the second queue, at which time it becomes the
503 * A one-way scan is natural because of the way UNIX read-ahead blocks are
507 bufqdisksort(struct buf_queue_head *bufq, struct buf *bp)
513 be = TAILQ_LAST(&bufq->queue, buf_queue);
515 * If the queue is empty or we are an
516 * ordered transaction, then it's easy.
518 if ((bq = bufq_first(bufq)) == NULL ||
519 (bp->b_flags & B_ORDERED) != 0) {
520 bufq_insert_tail(bufq, bp);
522 } else if (bufq->insert_point != NULL) {
525 * A certain portion of the list is
526 * "locked" to preserve ordering, so
527 * we can only insert after the insert
530 bq = bufq->insert_point;
534 * If we lie before the last removed (currently active)
535 * request, and are not inserting ourselves into the
536 * "locked" portion of the list, then we must add ourselves
537 * to the second request list.
539 if (bp->b_pblkno < bufq->last_pblkno) {
541 bq = bufq->switch_point;
543 * If we are starting a new secondary list,
547 bufq->switch_point = bp;
548 bufq_insert_tail(bufq, bp);
552 * If we lie ahead of the current switch point,
553 * insert us before the switch point and move
556 if (bp->b_pblkno < bq->b_pblkno) {
557 bufq->switch_point = bp;
558 TAILQ_INSERT_BEFORE(bq, bp, b_act);
562 if (bufq->switch_point != NULL)
563 be = TAILQ_PREV(bufq->switch_point,
566 * If we lie between last_pblkno and bq,
569 if (bp->b_pblkno < bq->b_pblkno) {
570 TAILQ_INSERT_BEFORE(bq, bp, b_act);
577 * Request is at/after our current position in the list.
578 * Optimize for sequential I/O by seeing if we go at the tail.
580 if (bp->b_pblkno > be->b_pblkno) {
581 TAILQ_INSERT_AFTER(&bufq->queue, be, bp, b_act);
585 /* Otherwise, insertion sort */
586 while ((bn = TAILQ_NEXT(bq, b_act)) != NULL) {
589 * We want to go after the current request if it is the end
590 * of the first request list, or if the next request is a
591 * larger cylinder than our request.
593 if (bn == bufq->switch_point
594 || bp->b_pblkno < bn->b_pblkno)
598 TAILQ_INSERT_AFTER(&bufq->queue, bq, bp, b_act);
603 * Attempt to read a disk label from a device using the indicated strategy
604 * routine. The label must be partly set up before this: secpercyl, secsize
605 * and anything required in the strategy routine (e.g., dummy bounds for the
606 * partition containing the label) must be filled in before calling us.
607 * Returns NULL on success and an error string on failure.
610 readdisklabel(dev_t dev, struct disklabel *lp)
613 struct disklabel *dlp;
616 bp = geteblk((int)lp->d_secsize);
618 bp->b_blkno = LABELSECTOR * ((int)lp->d_secsize/DEV_BSIZE);
619 bp->b_bcount = lp->d_secsize;
620 bp->b_flags &= ~B_INVAL;
621 bp->b_flags |= B_READ;
625 else for (dlp = (struct disklabel *)bp->b_data;
626 dlp <= (struct disklabel *)((char *)bp->b_data +
627 lp->d_secsize - sizeof(*dlp));
628 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
629 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
631 msg = "no disk label";
632 } else if (dlp->d_npartitions > MAXPARTITIONS ||
634 msg = "disk label corrupted";
641 bp->b_flags |= B_INVAL | B_AGE;
647 * Check new disk label for sensibility before setting it.
650 setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask)
653 struct partition *opp, *npp;
656 * Check it is actually a disklabel we are looking at.
658 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
662 * For each partition that we think is open,
664 while ((i = ffs((long)openmask)) != 0) {
667 * Check it is not changing....
669 openmask &= ~(1 << i);
670 if (nlp->d_npartitions <= i)
672 opp = &olp->d_partitions[i];
673 npp = &nlp->d_partitions[i];
674 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
677 * Copy internally-set partition information
678 * if new label doesn't include it. XXX
679 * (If we are using it then we had better stay the same type)
680 * This is possibly dubious, as someone else noted (XXX)
682 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
683 npp->p_fstype = opp->p_fstype;
684 npp->p_fsize = opp->p_fsize;
685 npp->p_frag = opp->p_frag;
686 npp->p_cpg = opp->p_cpg;
690 nlp->d_checksum = dkcksum(nlp);
696 * Write disk label back to device after modification.
699 writedisklabel(dev_t dev, struct disklabel *lp)
702 struct disklabel *dlp;
705 if (lp->d_partitions[RAW_PART].p_offset != 0)
706 return (EXDEV); /* not quite right */
707 bp = geteblk((int)lp->d_secsize);
708 bp->b_dev = dkmodpart(dev, RAW_PART);
709 bp->b_blkno = LABELSECTOR * ((int)lp->d_secsize/DEV_BSIZE);
710 bp->b_bcount = lp->d_secsize;
713 * We read the label first to see if it's there,
714 * in which case we will put ours at the same offset into the block..
715 * (I think this is stupid [Julian])
716 * Note that you can't write a label out over a corrupted label!
717 * (also stupid.. how do you write the first one? by raw writes?)
719 bp->b_flags &= ~B_INVAL;
720 bp->b_flags |= B_READ;
725 for (dlp = (struct disklabel *)bp->b_data;
726 dlp <= (struct disklabel *)
727 ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
728 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
729 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
732 bp->b_flags &= ~(B_DONE | B_READ);
733 bp->b_flags |= B_WRITE;
734 bp->b_dev = dkmodpart(dev, RAW_PART);
736 alpha_fix_srm_checksum(bp);
746 bzero(bp->b_data, lp->d_secsize);
747 dlp = (struct disklabel *)bp->b_data;
749 bp->b_flags &= ~B_INVAL;
750 bp->b_flags |= B_WRITE;
754 bp->b_flags |= B_INVAL | B_AGE;
760 * Disk error is the preface to plaintive error messages
761 * about failing disk transfers. It prints messages of the form
763 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
765 * if the offset of the error in the transfer and a disk label
766 * are both available. blkdone should be -1 if the position of the error
767 * is unknown; the disklabel pointer may be null from drivers that have not
768 * been converted to use them. The message is printed with printf
769 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
770 * The message should be completed (with at least a newline) with printf
771 * or addlog, respectively. There is no trailing space.
774 diskerr(struct buf *bp, dev_t dev, char *what, int pri,
775 int blkdone, struct disklabel *lp)
777 int unit = dkunit(dev);
778 int slice = dkslice(dev);
779 int part = dkpart(dev);
784 sname = dsname(dev, unit, slice, part, partname);
785 printf("%s%s: %s %sing fsbn ", sname, partname, what,
786 bp->b_flags & B_READ ? "read" : "writ");
788 if (bp->b_bcount <= DEV_BSIZE) {
789 printf("%ld", (long)sn);
793 printf("%ld of ", (long)sn);
795 printf("%ld-%ld", (long)bp->b_blkno,
796 (long)(bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE));
798 if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
800 sn *= DEV_BSIZE / lp->d_secsize; /* XXX */
802 sn += lp->d_partitions[part].p_offset;
804 * XXX should add slice offset and not print the slice,
805 * but we don't know the slice pointer.
806 * XXX should print bp->b_pblkno so that this will work
807 * independent of slices, labels and bad sector remapping,
808 * but some drivers don't set bp->b_pblkno.
810 printf(" (%s bn %ld; cn %ld", sname, (long)sn,
811 (long)(sn / lp->d_secpercyl));
812 sn %= (long)lp->d_secpercyl;
813 printf(" tn %ld sn %ld)", (long)(sn / lp->d_nsectors),
814 (long)(sn % lp->d_nsectors));