2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * ----------------------------------------------------------------------------
35 * "THE BEER-WARE LICENSE" (Revision 42):
36 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
37 * can do whatever you want with this stuff. If we meet some day, and you think
38 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
39 * ----------------------------------------------------------------------------
41 * Copyright (c) 1982, 1986, 1988, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 * must display the following acknowledgement:
59 * This product includes software developed by the University of
60 * California, Berkeley and its contributors.
61 * 4. Neither the name of the University nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
78 * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
79 * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
80 * $DragonFly: src/sys/kern/subr_disk.c,v 1.17 2005/08/26 12:45:53 hmp Exp $
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
87 #include <sys/sysctl.h>
90 #include <sys/disklabel.h>
91 #include <sys/diskslice.h>
93 #include <sys/malloc.h>
94 #include <sys/sysctl.h>
95 #include <machine/md_var.h>
96 #include <sys/ctype.h>
97 #include <sys/syslog.h>
98 #include <sys/device.h>
99 #include <sys/msgport.h>
100 #include <sys/msgport2.h>
101 #include <sys/buf2.h>
103 static MALLOC_DEFINE(M_DISK, "disk", "disk data");
105 static d_strategy_t diskstrategy;
106 static d_open_t diskopen;
107 static d_close_t diskclose;
108 static d_ioctl_t diskioctl;
109 static d_psize_t diskpsize;
110 static d_clone_t diskclone;
111 static int disk_putport(lwkt_port_t port, lwkt_msg_t msg);
113 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
116 * Create a slice and unit managed disk.
118 * Our port layer will be responsible for assigning pblkno and handling
119 * high level partition operations, then forwarding the requests to the
122 * The disk_create() function clones the provided rawsw for creating a
123 * managed disk device. In addition, the cdevsw intercept port is
124 * changed to disk_putport, which is used to transform requests for the
125 * managed disk device.
127 * The raw device (based on rawsw) is returned to the caller, NOT the
128 * slice and unit managed cdev. The caller typically sets various
129 * driver parameters and IO limits on the returned rawdev which we must
130 * inherit when our managed device is opened.
133 disk_create(int unit, struct disk *dp, int flags, struct cdevsw *rawsw)
136 struct cdevsw *devsw;
139 * Create the raw backing device
141 compile_devsw(rawsw);
142 rawdev = make_dev(rawsw, dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
143 UID_ROOT, GID_OPERATOR, 0640,
144 "%s%d", rawsw->d_name, unit);
147 * Initialize our intercept port
149 bzero(dp, sizeof(*dp));
150 lwkt_initport(&dp->d_port, NULL);
151 dp->d_port.mp_putport = disk_putport;
155 * We install a custom cdevsw rather then the passed cdevsw,
156 * and save our disk structure in d_data so we can get at it easily
157 * without any complex cloning code.
159 devsw = cdevsw_add_override(rawdev, dkunitmask(), dkmakeunit(unit));
160 devsw->d_port = &dp->d_port;
162 devsw->d_clone = diskclone;
164 dp->d_rawdev = rawdev;
165 dp->d_cdev = make_dev(devsw,
166 dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
167 UID_ROOT, GID_OPERATOR, 0640,
168 "%s%d", devsw->d_name, unit);
170 dp->d_dsflags = flags;
171 LIST_INSERT_HEAD(&disklist, dp, d_list);
172 return (dp->d_rawdev);
176 * This routine is called when an adapter detaches. The higher level
177 * managed disk device is destroyed while the lower level raw device is
181 disk_destroy(struct disk *disk)
184 cdevsw_remove(disk->d_devsw, dkunitmask(),
185 dkmakeunit(dkunit(disk->d_cdev)));
186 LIST_REMOVE(disk, d_list);
189 destroy_all_dev(disk->d_rawsw, dkunitmask(),
190 dkmakeunit(dkunit(disk->d_rawdev)));
192 bzero(disk, sizeof(*disk));
196 disk_dumpcheck(dev_t dev, u_int *count, u_int *blkno, u_int *secsize)
199 struct disklabel *dl;
207 dl = dsgetlabel(dev, dp->d_slice);
210 *count = Maxmem * (PAGE_SIZE / dl->d_secsize);
211 if (dumplo <= LABELSECTOR ||
212 (dumplo + *count > dl->d_partitions[dkpart(dev)].p_size))
214 boff = dl->d_partitions[dkpart(dev)].p_offset +
215 dp->d_slice->dss_slices[dkslice(dev)].ds_offset;
216 *blkno = boff + dumplo;
217 *secsize = dl->d_secsize;
223 disk_invalidate (struct disk *disk)
226 dsgone(&disk->d_slice);
230 disk_enumerate(struct disk *disk)
233 return (LIST_FIRST(&disklist));
235 return (LIST_NEXT(disk, d_list));
240 sysctl_disks(SYSCTL_HANDLER_ARGS)
248 while ((disk = disk_enumerate(disk))) {
250 error = SYSCTL_OUT(req, " ", 1);
256 error = SYSCTL_OUT(req, disk->d_rawdev->si_name, strlen(disk->d_rawdev->si_name));
260 error = SYSCTL_OUT(req, "", 1);
264 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, 0, NULL,
265 sysctl_disks, "A", "names of available disks");
268 * The port intercept functions
272 disk_putport(lwkt_port_t port, lwkt_msg_t lmsg)
274 struct disk *disk = (struct disk *)port;
275 cdevallmsg_t msg = (cdevallmsg_t)lmsg;
278 switch(msg->am_lmsg.ms_cmd.cm_op) {
281 msg->am_open.msg.dev,
283 msg->am_open.devtype,
288 msg->am_close.msg.dev,
290 msg->am_close.devtype,
295 msg->am_ioctl.msg.dev,
301 case CDEV_CMD_STRATEGY:
302 diskstrategy(msg->am_strategy.bp);
306 msg->am_psize.result = diskpsize(msg->am_psize.msg.dev);
310 error = physio(msg->am_read.msg.dev,
311 msg->am_read.uio, msg->am_read.ioflag);
314 error = physio(msg->am_write.msg.dev,
315 msg->am_write.uio, msg->am_write.ioflag);
318 case CDEV_CMD_KQFILTER:
324 error = disk_dumpcheck(msg->am_dump.msg.dev,
327 &msg->am_dump.secsize);
329 msg->am_dump.msg.dev = disk->d_rawdev;
330 error = lwkt_forwardmsg(disk->d_rawdev->si_port,
331 &msg->am_dump.msg.msg);
332 printf("error2 %d\n", error);
343 * When new device entries are instantiated, make sure they inherit our
344 * si_disk structure and block and iosize limits from the raw device.
346 * This routine is always called synchronously in the context of the
349 * XXX The various io and block size constraints are not always initialized
350 * properly by devices.
358 dp = dev->si_devsw->d_data;
359 KKASSERT(dp != NULL);
361 dev->si_iosize_max = dp->d_rawdev->si_iosize_max;
362 dev->si_bsize_phys = dp->d_rawdev->si_bsize_phys;
363 dev->si_bsize_best = dp->d_rawdev->si_bsize_best;
368 * Open a disk device or partition.
372 diskopen(dev_t dev, int oflags, int devtype, struct thread *td)
378 * dp can't be NULL here XXX.
386 * Deal with open races
388 while (dp->d_flags & DISKFLAG_LOCK) {
389 dp->d_flags |= DISKFLAG_WANTED;
390 error = tsleep(dp, PCATCH, "diskopen", hz);
394 dp->d_flags |= DISKFLAG_LOCK;
397 * Open the underlying raw device.
399 if (!dsisopen(dp->d_slice)) {
401 if (!pdev->si_iosize_max)
402 pdev->si_iosize_max = dev->si_iosize_max;
404 error = dev_dopen(dp->d_rawdev, oflags, devtype, td);
408 * Inherit properties from the underlying device now that it is
416 error = dsopen(dev, devtype, dp->d_dsflags, &dp->d_slice, &dp->d_label);
418 if (!dsisopen(dp->d_slice))
419 dev_dclose(dp->d_rawdev, oflags, devtype, td);
421 dp->d_flags &= ~DISKFLAG_LOCK;
422 if (dp->d_flags & DISKFLAG_WANTED) {
423 dp->d_flags &= ~DISKFLAG_WANTED;
431 * Close a disk device or partition
435 diskclose(dev_t dev, int fflag, int devtype, struct thread *td)
443 dsclose(dev, devtype, dp->d_slice);
444 if (!dsisopen(dp->d_slice))
445 error = dev_dclose(dp->d_rawdev, fflag, devtype, td);
450 * Execute strategy routine
454 diskstrategy(struct buf *bp)
458 dp = bp->b_dev->si_disk;
462 bp->b_flags |= B_ERROR;
466 KKASSERT(bp->b_dev->si_disk == dp);
468 if (dscheck(bp, dp->d_slice) <= 0) {
472 bp->b_dev = dp->d_rawdev;
473 dev_dstrategy(dp->d_rawdev, bp);
477 * First execute the ioctl on the disk device, and if it isn't supported
478 * try running it on the backing device.
482 diskioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
491 error = dsioctl(dev, cmd, data, fflag, &dp->d_slice);
492 if (error == ENOIOCTL)
493 error = dev_dioctl(dp->d_rawdev, cmd, data, fflag, td);
509 return(dssize(dev, &dp->d_slice));
511 if (dp != dev->si_disk) {
512 dev->si_drv1 = pdev->si_drv1;
513 dev->si_drv2 = pdev->si_drv2;
514 /* XXX: don't set bp->b_dev->si_disk (?) */
519 SYSCTL_INT(_debug_sizeof, OID_AUTO, disklabel, CTLFLAG_RD,
520 0, sizeof(struct disklabel), "sizeof(struct disklabel)");
522 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
523 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
525 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
526 0, sizeof(struct disk), "sizeof(struct disk)");
530 * Seek sort for disks.
532 * The buf_queue keep two queues, sorted in ascending block order. The first
533 * queue holds those requests which are positioned after the current block
534 * (in the first request); the second, which starts at queue->switch_point,
535 * holds requests which came in after their block number was passed. Thus
536 * we implement a one way scan, retracting after reaching the end of the drive
537 * to the first request on the second queue, at which time it becomes the
540 * A one-way scan is natural because of the way UNIX read-ahead blocks are
544 bufqdisksort(struct buf_queue_head *bufq, struct buf *bp)
550 be = TAILQ_LAST(&bufq->queue, buf_queue);
552 * If the queue is empty or we are an
553 * ordered transaction, then it's easy.
555 if ((bq = bufq_first(bufq)) == NULL ||
556 (bp->b_flags & B_ORDERED) != 0) {
557 bufq_insert_tail(bufq, bp);
559 } else if (bufq->insert_point != NULL) {
562 * A certain portion of the list is
563 * "locked" to preserve ordering, so
564 * we can only insert after the insert
567 bq = bufq->insert_point;
571 * If we lie before the last removed (currently active)
572 * request, and are not inserting ourselves into the
573 * "locked" portion of the list, then we must add ourselves
574 * to the second request list.
576 if (bp->b_pblkno < bufq->last_pblkno) {
578 bq = bufq->switch_point;
580 * If we are starting a new secondary list,
584 bufq->switch_point = bp;
585 bufq_insert_tail(bufq, bp);
589 * If we lie ahead of the current switch point,
590 * insert us before the switch point and move
593 if (bp->b_pblkno < bq->b_pblkno) {
594 bufq->switch_point = bp;
595 TAILQ_INSERT_BEFORE(bq, bp, b_act);
599 if (bufq->switch_point != NULL)
600 be = TAILQ_PREV(bufq->switch_point,
603 * If we lie between last_pblkno and bq,
606 if (bp->b_pblkno < bq->b_pblkno) {
607 TAILQ_INSERT_BEFORE(bq, bp, b_act);
614 * Request is at/after our current position in the list.
615 * Optimize for sequential I/O by seeing if we go at the tail.
617 if (bp->b_pblkno > be->b_pblkno) {
618 TAILQ_INSERT_AFTER(&bufq->queue, be, bp, b_act);
622 /* Otherwise, insertion sort */
623 while ((bn = TAILQ_NEXT(bq, b_act)) != NULL) {
626 * We want to go after the current request if it is the end
627 * of the first request list, or if the next request is a
628 * larger cylinder than our request.
630 if (bn == bufq->switch_point
631 || bp->b_pblkno < bn->b_pblkno)
635 TAILQ_INSERT_AFTER(&bufq->queue, bq, bp, b_act);
640 * Attempt to read a disk label from a device using the indicated strategy
641 * routine. The label must be partly set up before this: secpercyl, secsize
642 * and anything required in the strategy routine (e.g., dummy bounds for the
643 * partition containing the label) must be filled in before calling us.
644 * Returns NULL on success and an error string on failure.
647 readdisklabel(dev_t dev, struct disklabel *lp)
650 struct disklabel *dlp;
653 bp = geteblk((int)lp->d_secsize);
655 bp->b_blkno = LABELSECTOR * ((int)lp->d_secsize/DEV_BSIZE);
656 bp->b_bcount = lp->d_secsize;
657 bp->b_flags &= ~B_INVAL;
658 bp->b_flags |= B_READ;
662 else for (dlp = (struct disklabel *)bp->b_data;
663 dlp <= (struct disklabel *)((char *)bp->b_data +
664 lp->d_secsize - sizeof(*dlp));
665 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
666 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
668 msg = "no disk label";
669 } else if (dlp->d_npartitions > MAXPARTITIONS ||
671 msg = "disk label corrupted";
678 bp->b_flags |= B_INVAL | B_AGE;
684 * Check new disk label for sensibility before setting it.
687 setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask)
690 struct partition *opp, *npp;
693 * Check it is actually a disklabel we are looking at.
695 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
699 * For each partition that we think is open,
701 while ((i = ffs((long)openmask)) != 0) {
704 * Check it is not changing....
706 openmask &= ~(1 << i);
707 if (nlp->d_npartitions <= i)
709 opp = &olp->d_partitions[i];
710 npp = &nlp->d_partitions[i];
711 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
714 * Copy internally-set partition information
715 * if new label doesn't include it. XXX
716 * (If we are using it then we had better stay the same type)
717 * This is possibly dubious, as someone else noted (XXX)
719 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
720 npp->p_fstype = opp->p_fstype;
721 npp->p_fsize = opp->p_fsize;
722 npp->p_frag = opp->p_frag;
723 npp->p_cpg = opp->p_cpg;
727 nlp->d_checksum = dkcksum(nlp);
733 * Write disk label back to device after modification.
736 writedisklabel(dev_t dev, struct disklabel *lp)
739 struct disklabel *dlp;
742 if (lp->d_partitions[RAW_PART].p_offset != 0)
743 return (EXDEV); /* not quite right */
744 bp = geteblk((int)lp->d_secsize);
745 bp->b_dev = dkmodpart(dev, RAW_PART);
746 bp->b_blkno = LABELSECTOR * ((int)lp->d_secsize/DEV_BSIZE);
747 bp->b_bcount = lp->d_secsize;
750 * We read the label first to see if it's there,
751 * in which case we will put ours at the same offset into the block..
752 * (I think this is stupid [Julian])
753 * Note that you can't write a label out over a corrupted label!
754 * (also stupid.. how do you write the first one? by raw writes?)
756 bp->b_flags &= ~B_INVAL;
757 bp->b_flags |= B_READ;
762 for (dlp = (struct disklabel *)bp->b_data;
763 dlp <= (struct disklabel *)
764 ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
765 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
766 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
769 bp->b_flags &= ~(B_DONE | B_READ);
770 bp->b_flags |= B_WRITE;
771 bp->b_dev = dkmodpart(dev, RAW_PART);
780 bzero(bp->b_data, lp->d_secsize);
781 dlp = (struct disklabel *)bp->b_data;
783 bp->b_flags &= ~B_INVAL;
784 bp->b_flags |= B_WRITE;
788 bp->b_flags |= B_INVAL | B_AGE;
794 * Disk error is the preface to plaintive error messages
795 * about failing disk transfers. It prints messages of the form
797 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
799 * if the offset of the error in the transfer and a disk label
800 * are both available. blkdone should be -1 if the position of the error
801 * is unknown; the disklabel pointer may be null from drivers that have not
802 * been converted to use them. The message is printed with printf
803 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
804 * The message should be completed (with at least a newline) with printf
805 * or addlog, respectively. There is no trailing space.
808 diskerr(struct buf *bp, dev_t dev, char *what, int pri,
809 int blkdone, struct disklabel *lp)
811 int unit = dkunit(dev);
812 int slice = dkslice(dev);
813 int part = dkpart(dev);
818 sname = dsname(dev, unit, slice, part, partname);
819 printf("%s%s: %s %sing fsbn ", sname, partname, what,
820 bp->b_flags & B_READ ? "read" : "writ");
822 if (bp->b_bcount <= DEV_BSIZE) {
823 printf("%ld", (long)sn);
827 printf("%ld of ", (long)sn);
829 printf("%ld-%ld", (long)bp->b_blkno,
830 (long)(bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE));
832 if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
834 sn *= DEV_BSIZE / lp->d_secsize; /* XXX */
836 sn += lp->d_partitions[part].p_offset;
838 * XXX should add slice offset and not print the slice,
839 * but we don't know the slice pointer.
840 * XXX should print bp->b_pblkno so that this will work
841 * independent of slices, labels and bad sector remapping,
842 * but some drivers don't set bp->b_pblkno.
844 printf(" (%s bn %ld; cn %ld", sname, (long)sn,
845 (long)(sn / lp->d_secpercyl));
846 sn %= (long)lp->d_secpercyl;
847 printf(" tn %ld sn %ld)", (long)(sn / lp->d_nsectors),
848 (long)(sn % lp->d_nsectors));