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 * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
10 * $DragonFly: src/sys/kern/subr_disk.c,v 1.7 2003/11/24 20:46:01 dillon Exp $
14 #include <sys/param.h>
15 #include <sys/systm.h>
16 #include <sys/kernel.h>
17 #include <sys/sysctl.h>
21 #include <sys/malloc.h>
22 #include <sys/sysctl.h>
23 #include <machine/md_var.h>
24 #include <sys/ctype.h>
25 #include <sys/msgport.h>
26 #include <sys/msgport2.h>
28 static MALLOC_DEFINE(M_DISK, "disk", "disk data");
30 static d_strategy_t diskstrategy;
31 static d_open_t diskopen;
32 static d_close_t diskclose;
33 static d_ioctl_t diskioctl;
34 static d_psize_t diskpsize;
35 static int disk_putport(lwkt_port_t port, lwkt_msg_t msg);
37 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
40 inherit_raw(dev_t pdev, dev_t dev)
42 dev->si_disk = pdev->si_disk;
43 dev->si_drv1 = pdev->si_drv1;
44 dev->si_drv2 = pdev->si_drv2;
45 dev->si_iosize_max = pdev->si_iosize_max;
46 dev->si_bsize_phys = pdev->si_bsize_phys;
47 dev->si_bsize_best = pdev->si_bsize_best;
51 * Create a slice and unit managed disk. The underlying raw disk device
52 * is specified by cdevsw. We create the device as a managed device by
53 * first creating it normally then overriding the message port with our
54 * own frontend (which will be responsible for assigning pblkno).
57 disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw)
61 bzero(dp, sizeof(*dp));
62 lwkt_initport(&dp->d_port, NULL); /* intercept port */
63 dp->d_port.mp_putport = disk_putport;
65 dev = makedev(cdevsw->d_maj, 0); /* base device */
68 dp->d_fwdport = cdevsw_add_override(cdevsw, &dp->d_port);
71 printf("Creating DISK %s%d\n", cdevsw->d_name, unit);
74 * The whole disk placemarker holds the disk structure.
76 dev = make_dev(cdevsw, dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART),
77 UID_ROOT, GID_OPERATOR, 0640, "%s%d", cdevsw->d_name, unit);
80 dp->d_dsflags = flags;
81 LIST_INSERT_HEAD(&disklist, dp, d_list);
86 disk_destroy(struct disk *disk)
88 dev_t dev = disk->d_dev;
90 LIST_REMOVE(disk, d_list);
91 bzero(disk, sizeof(*disk));
94 /* YYY remove cdevsw entries? */
99 disk_dumpcheck(dev_t dev, u_int *count, u_int *blkno, u_int *secsize)
102 struct disklabel *dl;
110 dl = dsgetlabel(dev, dp->d_slice);
113 *count = Maxmem * (PAGE_SIZE / dl->d_secsize);
114 if (dumplo <= LABELSECTOR ||
115 (dumplo + *count > dl->d_partitions[dkpart(dev)].p_size))
117 boff = dl->d_partitions[dkpart(dev)].p_offset +
118 dp->d_slice->dss_slices[dkslice(dev)].ds_offset;
119 *blkno = boff + dumplo;
120 *secsize = dl->d_secsize;
126 disk_invalidate (struct disk *disk)
129 dsgone(&disk->d_slice);
133 disk_enumerate(struct disk *disk)
136 return (LIST_FIRST(&disklist));
138 return (LIST_NEXT(disk, d_list));
142 sysctl_disks(SYSCTL_HANDLER_ARGS)
150 while ((disk = disk_enumerate(disk))) {
152 error = SYSCTL_OUT(req, " ", 1);
158 error = SYSCTL_OUT(req, disk->d_dev->si_name, strlen(disk->d_dev->si_name));
162 error = SYSCTL_OUT(req, "", 1);
166 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, 0, NULL,
167 sysctl_disks, "A", "names of available disks");
170 * The port intercept functions
174 disk_putport(lwkt_port_t port, lwkt_msg_t lmsg)
176 struct disk *disk = (struct disk *)port;
177 cdevallmsg_t msg = (cdevallmsg_t)lmsg;
180 switch(msg->am_lmsg.ms_cmd) {
183 msg->am_open.msg.dev,
185 msg->am_open.devtype,
190 msg->am_close.msg.dev,
192 msg->am_close.devtype,
197 msg->am_ioctl.msg.dev,
203 case CDEV_CMD_STRATEGY:
204 diskstrategy(msg->am_strategy.bp);
208 msg->am_psize.result = diskpsize(msg->am_psize.msg.dev);
212 error = lwkt_forwardmsg(disk->d_fwdport, &msg->am_lmsg);
219 diskopen(dev_t dev, int oflags, int devtype, struct thread *td)
226 pdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
232 while (dp->d_flags & DISKFLAG_LOCK) {
233 dp->d_flags |= DISKFLAG_WANTED;
234 error = tsleep(dp, PCATCH, "diskopen", hz);
238 dp->d_flags |= DISKFLAG_LOCK;
240 if (!dsisopen(dp->d_slice)) {
241 if (!pdev->si_iosize_max)
242 pdev->si_iosize_max = dev->si_iosize_max;
243 error = dev_port_dopen(dp->d_fwdport, pdev, oflags, devtype, td);
246 /* Inherit properties from the whole/raw dev_t */
247 inherit_raw(pdev, dev);
252 error = dsopen(dev, devtype, dp->d_dsflags, &dp->d_slice, &dp->d_label);
254 if (!dsisopen(dp->d_slice))
255 dev_port_dclose(dp->d_fwdport, pdev, oflags, devtype, td);
257 dp->d_flags &= ~DISKFLAG_LOCK;
258 if (dp->d_flags & DISKFLAG_WANTED) {
259 dp->d_flags &= ~DISKFLAG_WANTED;
267 diskclose(dev_t dev, int fflag, int devtype, struct thread *td)
274 pdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
278 dsclose(dev, devtype, dp->d_slice);
279 if (!dsisopen(dp->d_slice))
280 error = dev_port_dclose(dp->d_fwdport, pdev, fflag, devtype, td);
285 diskstrategy(struct buf *bp)
290 pdev = dkmodpart(dkmodslice(bp->b_dev, WHOLE_DISK_SLICE), RAW_PART);
292 if (dp != bp->b_dev->si_disk)
293 inherit_raw(pdev, bp->b_dev);
297 bp->b_flags |= B_ERROR;
302 if (dscheck(bp, dp->d_slice) <= 0) {
306 dev_port_dstrategy(dp->d_fwdport, dp->d_dev, bp);
310 * note: when forwarding the ioctl we use the original device rather then
311 * the whole disk slice.
314 diskioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
320 pdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
324 error = dsioctl(dev, cmd, data, fflag, &dp->d_slice);
325 if (error == ENOIOCTL)
326 error = dev_port_dioctl(dp->d_fwdport, dev, cmd, data, fflag, td);
336 pdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
340 if (dp != dev->si_disk) {
341 dev->si_drv1 = pdev->si_drv1;
342 dev->si_drv2 = pdev->si_drv2;
343 /* XXX: don't set bp->b_dev->si_disk (?) */
345 return (dssize(dev, &dp->d_slice));
348 SYSCTL_DECL(_debug_sizeof);
350 SYSCTL_INT(_debug_sizeof, OID_AUTO, disklabel, CTLFLAG_RD,
351 0, sizeof(struct disklabel), "sizeof(struct disklabel)");
353 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
354 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
356 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
357 0, sizeof(struct disk), "sizeof(struct disk)");