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.10 2004/05/19 22:52:58 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,
107 dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
108 UID_ROOT, GID_OPERATOR, 0640,
109 "%s%d", rawsw->d_name, unit);
112 * Initialize our intercept port
114 bzero(dp, sizeof(*dp));
115 lwkt_initport(&dp->d_port, NULL);
116 dp->d_port.mp_putport = disk_putport;
120 * We install a custom cdevsw rather then the passed cdevsw,
121 * and save our disk structure in d_data so we can get at it easily
122 * without any complex cloning code.
124 devsw = cdevsw_add_override(rawdev, dkunitmask(), dkmakeunit(unit));
125 devsw->d_port = &dp->d_port;
127 devsw->d_clone = diskclone;
129 dp->d_rawdev = rawdev;
130 dp->d_cdev = make_dev(devsw,
131 dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
132 UID_ROOT, GID_OPERATOR, 0640,
133 "%s%d", devsw->d_name, unit);
135 dp->d_dsflags = flags;
136 LIST_INSERT_HEAD(&disklist, dp, d_list);
137 return (dp->d_rawdev);
141 * This routine is called when an adapter detaches. The higher level
142 * managed disk device is destroyed while the lower level raw device is
146 disk_destroy(struct disk *disk)
149 cdevsw_remove(disk->d_devsw, dkunitmask(), dkunit(disk->d_cdev));
150 LIST_REMOVE(disk, d_list);
153 destroy_all_dev(disk->d_rawsw, dkunitmask(), dkunit(disk->d_rawdev));
154 bzero(disk, sizeof(*disk));
158 disk_dumpcheck(dev_t dev, u_int *count, u_int *blkno, u_int *secsize)
161 struct disklabel *dl;
169 dl = dsgetlabel(dev, dp->d_slice);
172 *count = Maxmem * (PAGE_SIZE / dl->d_secsize);
173 if (dumplo <= LABELSECTOR ||
174 (dumplo + *count > dl->d_partitions[dkpart(dev)].p_size))
176 boff = dl->d_partitions[dkpart(dev)].p_offset +
177 dp->d_slice->dss_slices[dkslice(dev)].ds_offset;
178 *blkno = boff + dumplo;
179 *secsize = dl->d_secsize;
185 disk_invalidate (struct disk *disk)
188 dsgone(&disk->d_slice);
192 disk_enumerate(struct disk *disk)
195 return (LIST_FIRST(&disklist));
197 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.
332 diskopen(dev_t dev, int oflags, int devtype, struct thread *td)
338 * dp can't be NULL here XXX.
346 * Deal with open races
348 while (dp->d_flags & DISKFLAG_LOCK) {
349 dp->d_flags |= DISKFLAG_WANTED;
350 error = tsleep(dp, PCATCH, "diskopen", hz);
354 dp->d_flags |= DISKFLAG_LOCK;
357 * Open the underlying raw device.
359 if (!dsisopen(dp->d_slice)) {
361 if (!pdev->si_iosize_max)
362 pdev->si_iosize_max = dev->si_iosize_max;
364 error = dev_dopen(dp->d_rawdev, oflags, devtype, td);
368 * Inherit properties from the underlying device now that it is
376 error = dsopen(dev, devtype, dp->d_dsflags, &dp->d_slice, &dp->d_label);
378 if (!dsisopen(dp->d_slice))
379 dev_dclose(dp->d_rawdev, oflags, devtype, td);
381 dp->d_flags &= ~DISKFLAG_LOCK;
382 if (dp->d_flags & DISKFLAG_WANTED) {
383 dp->d_flags &= ~DISKFLAG_WANTED;
391 * Close a disk device or partition
394 diskclose(dev_t dev, int fflag, int devtype, struct thread *td)
402 dsclose(dev, devtype, dp->d_slice);
403 if (!dsisopen(dp->d_slice))
404 error = dev_dclose(dp->d_rawdev, fflag, devtype, td);
409 * Execute strategy routine
412 diskstrategy(struct buf *bp)
416 dp = bp->b_dev->si_disk;
420 bp->b_flags |= B_ERROR;
424 KKASSERT(bp->b_dev->si_disk == dp);
426 if (dscheck(bp, dp->d_slice) <= 0) {
430 bp->b_dev = dp->d_rawdev;
431 dev_dstrategy(dp->d_rawdev, bp);
435 * First execute the ioctl on the disk device, and if it isn't supported
436 * try running it on the backing device.
439 diskioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
448 error = dsioctl(dev, cmd, data, fflag, &dp->d_slice);
449 if (error == ENOIOCTL)
450 error = dev_dioctl(dp->d_rawdev, cmd, data, fflag, td);
465 return(dssize(dev, &dp->d_slice));
467 if (dp != dev->si_disk) {
468 dev->si_drv1 = pdev->si_drv1;
469 dev->si_drv2 = pdev->si_drv2;
470 /* XXX: don't set bp->b_dev->si_disk (?) */
475 SYSCTL_DECL(_debug_sizeof);
477 SYSCTL_INT(_debug_sizeof, OID_AUTO, disklabel, CTLFLAG_RD,
478 0, sizeof(struct disklabel), "sizeof(struct disklabel)");
480 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
481 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
483 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
484 0, sizeof(struct disk), "sizeof(struct disk)");
488 * Seek sort for disks.
490 * The buf_queue keep two queues, sorted in ascending block order. The first
491 * queue holds those requests which are positioned after the current block
492 * (in the first request); the second, which starts at queue->switch_point,
493 * holds requests which came in after their block number was passed. Thus
494 * we implement a one way scan, retracting after reaching the end of the drive
495 * to the first request on the second queue, at which time it becomes the
498 * A one-way scan is natural because of the way UNIX read-ahead blocks are
502 bufqdisksort(bufq, bp)
503 struct buf_queue_head *bufq;
510 be = TAILQ_LAST(&bufq->queue, buf_queue);
512 * If the queue is empty or we are an
513 * ordered transaction, then it's easy.
515 if ((bq = bufq_first(bufq)) == NULL
516 || (bp->b_flags & B_ORDERED) != 0) {
517 bufq_insert_tail(bufq, bp);
519 } else if (bufq->insert_point != NULL) {
522 * A certain portion of the list is
523 * "locked" to preserve ordering, so
524 * we can only insert after the insert
527 bq = bufq->insert_point;
531 * If we lie before the last removed (currently active)
532 * request, and are not inserting ourselves into the
533 * "locked" portion of the list, then we must add ourselves
534 * to the second request list.
536 if (bp->b_pblkno < bufq->last_pblkno) {
538 bq = bufq->switch_point;
540 * If we are starting a new secondary list,
544 bufq->switch_point = bp;
545 bufq_insert_tail(bufq, bp);
549 * If we lie ahead of the current switch point,
550 * insert us before the switch point and move
553 if (bp->b_pblkno < bq->b_pblkno) {
554 bufq->switch_point = bp;
555 TAILQ_INSERT_BEFORE(bq, bp, b_act);
559 if (bufq->switch_point != NULL)
560 be = TAILQ_PREV(bufq->switch_point,
563 * If we lie between last_pblkno and bq,
566 if (bp->b_pblkno < bq->b_pblkno) {
567 TAILQ_INSERT_BEFORE(bq, bp, b_act);
574 * Request is at/after our current position in the list.
575 * Optimize for sequential I/O by seeing if we go at the tail.
577 if (bp->b_pblkno > be->b_pblkno) {
578 TAILQ_INSERT_AFTER(&bufq->queue, be, bp, b_act);
582 /* Otherwise, insertion sort */
583 while ((bn = TAILQ_NEXT(bq, b_act)) != NULL) {
586 * We want to go after the current request if it is the end
587 * of the first request list, or if the next request is a
588 * larger cylinder than our request.
590 if (bn == bufq->switch_point
591 || bp->b_pblkno < bn->b_pblkno)
595 TAILQ_INSERT_AFTER(&bufq->queue, bq, bp, b_act);
600 * Attempt to read a disk label from a device using the indicated strategy
601 * routine. The label must be partly set up before this: secpercyl, secsize
602 * and anything required in the strategy routine (e.g., dummy bounds for the
603 * partition containing the label) must be filled in before calling us.
604 * Returns NULL on success and an error string on failure.
607 readdisklabel(dev_t dev, struct disklabel *lp)
610 struct disklabel *dlp;
613 bp = geteblk((int)lp->d_secsize);
615 bp->b_blkno = LABELSECTOR * ((int)lp->d_secsize/DEV_BSIZE);
616 bp->b_bcount = lp->d_secsize;
617 bp->b_flags &= ~B_INVAL;
618 bp->b_flags |= B_READ;
622 else for (dlp = (struct disklabel *)bp->b_data;
623 dlp <= (struct disklabel *)((char *)bp->b_data +
624 lp->d_secsize - sizeof(*dlp));
625 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
626 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
628 msg = "no disk label";
629 } else if (dlp->d_npartitions > MAXPARTITIONS ||
631 msg = "disk label corrupted";
638 bp->b_flags |= B_INVAL | B_AGE;
644 * Check new disk label for sensibility before setting it.
647 setdisklabel(olp, nlp, openmask)
648 struct disklabel *olp, *nlp;
652 struct partition *opp, *npp;
655 * Check it is actually a disklabel we are looking at.
657 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
661 * For each partition that we think is open,
663 while ((i = ffs((long)openmask)) != 0) {
666 * Check it is not changing....
668 openmask &= ~(1 << i);
669 if (nlp->d_npartitions <= i)
671 opp = &olp->d_partitions[i];
672 npp = &nlp->d_partitions[i];
673 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
676 * Copy internally-set partition information
677 * if new label doesn't include it. XXX
678 * (If we are using it then we had better stay the same type)
679 * This is possibly dubious, as someone else noted (XXX)
681 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
682 npp->p_fstype = opp->p_fstype;
683 npp->p_fsize = opp->p_fsize;
684 npp->p_frag = opp->p_frag;
685 npp->p_cpg = opp->p_cpg;
689 nlp->d_checksum = dkcksum(nlp);
695 * Write disk label back to device after modification.
698 writedisklabel(dev_t dev, struct disklabel *lp)
701 struct disklabel *dlp;
704 if (lp->d_partitions[RAW_PART].p_offset != 0)
705 return (EXDEV); /* not quite right */
706 bp = geteblk((int)lp->d_secsize);
707 bp->b_dev = dkmodpart(dev, RAW_PART);
708 bp->b_blkno = LABELSECTOR * ((int)lp->d_secsize/DEV_BSIZE);
709 bp->b_bcount = lp->d_secsize;
712 * We read the label first to see if it's there,
713 * in which case we will put ours at the same offset into the block..
714 * (I think this is stupid [Julian])
715 * Note that you can't write a label out over a corrupted label!
716 * (also stupid.. how do you write the first one? by raw writes?)
718 bp->b_flags &= ~B_INVAL;
719 bp->b_flags |= B_READ;
724 for (dlp = (struct disklabel *)bp->b_data;
725 dlp <= (struct disklabel *)
726 ((char *)bp->b_data + lp->d_secsize - sizeof(*dlp));
727 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
728 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
731 bp->b_flags &= ~(B_DONE | B_READ);
732 bp->b_flags |= B_WRITE;
733 bp->b_dev = dkmodpart(dev, RAW_PART);
735 alpha_fix_srm_checksum(bp);
745 bzero(bp->b_data, lp->d_secsize);
746 dlp = (struct disklabel *)bp->b_data;
748 bp->b_flags &= ~B_INVAL;
749 bp->b_flags |= B_WRITE;
753 bp->b_flags |= B_INVAL | B_AGE;
759 * Disk error is the preface to plaintive error messages
760 * about failing disk transfers. It prints messages of the form
762 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
764 * if the offset of the error in the transfer and a disk label
765 * are both available. blkdone should be -1 if the position of the error
766 * is unknown; the disklabel pointer may be null from drivers that have not
767 * been converted to use them. The message is printed with printf
768 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
769 * The message should be completed (with at least a newline) with printf
770 * or addlog, respectively. There is no trailing space.
773 diskerr(bp, what, pri, blkdone, lp)
777 struct disklabel *lp;
779 int unit = dkunit(bp->b_dev);
780 int slice = dkslice(bp->b_dev);
781 int part = dkpart(bp->b_dev);
786 sname = dsname(bp->b_dev, unit, slice, part, partname);
787 printf("%s%s: %s %sing fsbn ", sname, partname, what,
788 bp->b_flags & B_READ ? "read" : "writ");
790 if (bp->b_bcount <= DEV_BSIZE)
791 printf("%ld", (long)sn);
795 printf("%ld of ", (long)sn);
797 printf("%ld-%ld", (long)bp->b_blkno,
798 (long)(bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE));
800 if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
802 sn *= DEV_BSIZE / lp->d_secsize; /* XXX */
804 sn += lp->d_partitions[part].p_offset;
806 * XXX should add slice offset and not print the slice,
807 * but we don't know the slice pointer.
808 * XXX should print bp->b_pblkno so that this will work
809 * independent of slices, labels and bad sector remapping,
810 * but some drivers don't set bp->b_pblkno.
812 printf(" (%s bn %ld; cn %ld", sname, (long)sn,
813 (long)(sn / lp->d_secpercyl));
814 sn %= (long)lp->d_secpercyl;
815 printf(" tn %ld sn %ld)", (long)(sn / lp->d_nsectors),
816 (long)(sn % lp->d_nsectors));