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.40 2008/06/05 18:06:32 swildner 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/disklabel32.h>
92 #include <sys/disklabel64.h>
93 #include <sys/diskslice.h>
94 #include <sys/diskmbr.h>
96 #include <sys/malloc.h>
97 #include <sys/sysctl.h>
98 #include <machine/md_var.h>
99 #include <sys/ctype.h>
100 #include <sys/syslog.h>
101 #include <sys/device.h>
102 #include <sys/msgport.h>
103 #include <sys/msgport2.h>
104 #include <sys/buf2.h>
105 #include <vfs/devfs/devfs.h>
107 #include <sys/thread2.h>
109 #include <sys/queue.h>
110 #include <sys/lock.h>
112 static MALLOC_DEFINE(M_DISK, "disk", "disk data");
114 static void disk_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
115 static void disk_msg_core(void *);
116 static int disk_probe_slice(struct disk *dp, cdev_t dev, int slice);
117 static void disk_probe(struct disk *dp);
119 static d_open_t diskopen;
120 static d_close_t diskclose;
121 static d_ioctl_t diskioctl;
122 static d_strategy_t diskstrategy;
123 static d_psize_t diskpsize;
124 static d_clone_t diskclone;
125 static d_dump_t diskdump;
127 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
129 static struct dev_ops disk_ops = {
130 { "disk", 0, D_DISK },
132 .d_close = diskclose,
134 .d_write = physwrite,
135 .d_ioctl = diskioctl,
136 .d_strategy = diskstrategy,
138 .d_psize = diskpsize,
142 static struct objcache *disk_msg_cache;
144 struct objcache_malloc_args disk_msg_malloc_args = {
145 sizeof(struct disk_msg), M_DISK };
147 static struct lwkt_port disk_dispose_port;
148 static struct lwkt_port disk_msg_port;
152 disk_probe_slice(struct disk *dp, cdev_t dev, int slice)
154 struct disk_info *info = &dp->d_info;
155 struct diskslice *sp = &dp->d_slice->dss_slices[slice];
157 struct partinfo part;
164 ops = &disklabel32_ops;
165 msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info);
166 if (msg && !strcmp(msg, "no disk label")) {
167 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe_slice: trying with disklabel64\n");
168 ops = &disklabel64_ops;
169 msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info);
171 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe_slice: label: %s\n", (msg)?msg:"is NULL");
172 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe_slice: found %d partitions in the label\n", ops->op_getnumparts(sp->ds_label));
174 if (slice != WHOLE_DISK_SLICE)
175 ops->op_adjust_label_reserved(dp->d_slice, slice, sp);
180 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe_slice: lp.opaque: %x\n", sp->ds_label.opaque);
181 for (i = 0; i < ops->op_getnumparts(sp->ds_label); i++) {
182 ops->op_loadpartinfo(sp->ds_label, i, &part);
183 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe_slice: partinfo says fstype=%d for part %d\n", part.fstype, i);
185 ndev = make_only_devfs_dev(&disk_ops,
186 dkmakeminor(dkunit(dp->d_cdev), slice, i),
187 UID_ROOT, GID_OPERATOR, 0640,
188 "%s%c", dev->si_name, 'a'+ (char)i);
190 make_dev_alias(ndev, "disk-by-id/diskTEST-sliceTEST-part%d", i);
193 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe_slice:end: lp.opaque: %x\n", ndev->si_disk->d_slice->dss_slices[slice].ds_label.opaque);
196 } else if (info->d_dsflags & DSO_COMPATLABEL) {
198 if (sp->ds_size >= 0x100000000ULL)
199 ops = &disklabel64_ops;
201 ops = &disklabel32_ops;
202 sp->ds_label = ops->op_clone_label(info, sp);
204 if (sp->ds_type == DOSPTYP_386BSD /* XXX */)
205 log(LOG_WARNING, "%s: cannot find label (%s)\n",
210 sp->ds_wlabel = FALSE;
213 return (msg ? EINVAL : 0);
218 disk_probe(struct disk *dp)
220 struct disk_info *info = &dp->d_info;
221 cdev_t dev = dp->d_cdev;
225 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe called for %s\n", dp->d_cdev->si_name);
226 KKASSERT (info->d_media_blksize != 0);
227 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe: info set!\n");
229 dp->d_slice = dsmakeslicestruct(BASE_SLICE, info);
231 error = mbrinit(dev, info, &(dp->d_slice));
232 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe: &dp->d_slice is: %x, %x\n", &dp->d_slice, dp->d_slice);
234 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe: mbrinit() failed with error: %d\n", error);
237 devfs_debug(DEVFS_DEBUG_DEBUG, "mbrinit succeeded, found %d slices\n", dp->d_slice->dss_nslices);
238 if (dp->d_slice->dss_nslices == BASE_SLICE) {
239 dp->d_slice->dss_slices[COMPATIBILITY_SLICE].ds_size = info->d_media_blocks;
240 dp->d_slice->dss_slices[COMPATIBILITY_SLICE].ds_reserved = 0;
241 ndev = make_only_devfs_dev(&disk_ops,
242 dkmakewholeslice(dkunit(dev), COMPATIBILITY_SLICE),
243 UID_ROOT, GID_OPERATOR, 0640,
244 "%ss%d", dev->si_name, COMPATIBILITY_SLICE);
247 dp->d_slice->dss_slices[COMPATIBILITY_SLICE].ds_dev = ndev;
248 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe: type of slice is :%x\n", dp->d_slice->dss_slices[COMPATIBILITY_SLICE].ds_type );
249 //if (dp->d_slice->dss_slices[COMPATIBILITY_SLICE].ds_type == DOSPTYP_386BSD) {
250 dp->d_slice->dss_first_bsd_slice = COMPATIBILITY_SLICE;
251 disk_probe_slice(dp, ndev, COMPATIBILITY_SLICE);
254 for (i = BASE_SLICE; i < dp->d_slice->dss_nslices; i++) {
255 ndev = make_only_devfs_dev(&disk_ops,
256 dkmakewholeslice(dkunit(dev), i),
257 UID_ROOT, GID_OPERATOR, 0640,
258 "%ss%d", dev->si_name, i-1);
259 make_dev_alias(ndev, "disk-by-id/diskTEST-slice%d", i-1);
262 dp->d_slice->dss_slices[i].ds_dev = ndev;
263 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_probe-> type of slice is :%x\n", dp->d_slice->dss_slices[i].ds_type );
264 if (dp->d_slice->dss_slices[i].ds_type == DOSPTYP_386BSD) {
265 if (!dp->d_slice->dss_first_bsd_slice)
266 dp->d_slice->dss_first_bsd_slice = i;
267 disk_probe_slice(dp, ndev, i);
275 disk_msg_core(void *arg)
279 struct diskslice *sp;
283 lwkt_initport_thread(&disk_msg_port, curthread);
287 msg = (disk_msg_t)lwkt_waitport(&disk_msg_port, 0);
288 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_msg_core, new msg: %x\n", (unsigned int)msg->hdr.u.ms_result);
290 switch (msg->hdr.u.ms_result) {
292 case DISK_DISK_PROBE:
293 dp = (struct disk *)msg->load;
297 case DISK_DISK_DESTROY:
298 dp = (struct disk *)msg->load;
299 devfs_destroy_subnames(dp->d_cdev->si_name);
300 devfs_destroy_dev(dp->d_cdev);
301 //devfs_destroy_dev(dp->d_rawdev); //XXX: needed? when?
304 case DISK_SLICE_REPROBE:
305 dp = (struct disk *)msg->load;
306 sp = (struct diskslice *)msg->load2;
307 devfs_destroy_subnames(sp->ds_dev->si_name);
308 disk_probe_slice(dp, sp->ds_dev, dkslice(sp->ds_dev));
311 case DISK_DISK_REPROBE:
312 dp = (struct disk *)msg->load;
313 devfs_destroy_subnames(dp->d_cdev->si_name);
321 devfs_debug(DEVFS_DEBUG_WARNING, "disk_msg_core: unknown message received at core\n");
324 lwkt_replymsg((lwkt_msg_t)msg, 0);
331 * Acts as a message drain. Any message that is replied to here gets destroyed and
335 disk_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
337 objcache_put(disk_msg_cache, msg);
342 disk_msg_send(uint32_t cmd, void *load, void *load2)
345 lwkt_port_t port = &disk_msg_port;
347 disk_msg = objcache_get(disk_msg_cache, M_WAITOK);
349 lwkt_initmsg(&disk_msg->hdr, &disk_dispose_port, 0);
351 disk_msg->hdr.u.ms_result = cmd;
352 disk_msg->load = load;
353 disk_msg->load2 = load2;
355 lwkt_sendmsg(port, (lwkt_msg_t)disk_msg);
359 * Create a raw device for the dev_ops template (which is returned). Also
360 * create a slice and unit managed disk and overload the user visible
361 * device space with it.
363 * NOTE: The returned raw device is NOT a slice and unit managed device.
364 * It is an actual raw device representing the raw disk as specified by
365 * the passed dev_ops. The disk layer not only returns such a raw device,
366 * it also uses it internally when passing (modified) commands through.
369 disk_create(int unit, struct disk *dp, struct dev_ops *raw_ops)
373 rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit),
374 UID_ROOT, GID_OPERATOR, 0640,
375 "%s%d", raw_ops->head.name, unit);
378 bzero(dp, sizeof(*dp));
380 dp->d_rawdev = rawdev;
381 dp->d_raw_ops = raw_ops;
382 dp->d_dev_ops = &disk_ops;
383 dp->d_cdev = make_only_devfs_dev(&disk_ops,
384 dkmakewholedisk(unit),
385 UID_ROOT, GID_OPERATOR, 0640,
386 "%s%d", raw_ops->head.name, unit);
388 dp->d_cdev->si_disk = dp;
390 disk_ops.head.data = dp;
392 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_create called for %s\n", dp->d_cdev->si_name);
393 LIST_INSERT_HEAD(&disklist, dp, d_list);
394 return (dp->d_rawdev);
398 * Disk drivers must call this routine when media parameters are available
402 disk_setdiskinfo(struct disk *disk, struct disk_info *info)
404 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_setdiskinfo called for disk -1-: %x\n", disk);
405 bcopy(info, &disk->d_info, sizeof(disk->d_info));
406 info = &disk->d_info;
408 KKASSERT(info->d_media_size == 0 || info->d_media_blksize == 0);
409 if (info->d_media_size == 0 && info->d_media_blocks) {
410 info->d_media_size = (u_int64_t)info->d_media_blocks *
411 info->d_media_blksize;
412 } else if (info->d_media_size && info->d_media_blocks == 0 &&
413 info->d_media_blksize) {
414 info->d_media_blocks = info->d_media_size /
415 info->d_media_blksize;
419 * The si_* fields for rawdev are not set until after the
420 * disk_create() call, so someone using the cooked version
421 * of the raw device (i.e. da0s0) will not get the right
422 * si_iosize_max unless we fix it up here.
424 if (disk->d_cdev && disk->d_rawdev &&
425 disk->d_cdev->si_iosize_max == 0) {
426 disk->d_cdev->si_iosize_max = disk->d_rawdev->si_iosize_max;
427 disk->d_cdev->si_bsize_phys = disk->d_rawdev->si_bsize_phys;
428 disk->d_cdev->si_bsize_best = disk->d_rawdev->si_bsize_best;
431 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_setdiskinfo called for disk -2-: %x\n", disk);
432 disk_msg_send(DISK_DISK_PROBE, disk, NULL);
436 * This routine is called when an adapter detaches. The higher level
437 * managed disk device is destroyed while the lower level raw device is
441 disk_destroy(struct disk *disk)
443 disk_msg_send(DISK_DISK_DESTROY, disk, NULL);
448 disk_dumpcheck(cdev_t dev, u_int64_t *count, u_int64_t *blkno, u_int *secsize)
450 struct partinfo pinfo;
453 bzero(&pinfo, sizeof(pinfo));
454 error = dev_dioctl(dev, DIOCGPART, (void *)&pinfo, 0, proc0.p_ucred);
457 if (pinfo.media_blksize == 0)
459 *count = (u_int64_t)Maxmem * PAGE_SIZE / pinfo.media_blksize;
460 if (dumplo64 < pinfo.reserved_blocks ||
461 dumplo64 + *count > pinfo.media_blocks) {
464 *blkno = dumplo64 + pinfo.media_offset / pinfo.media_blksize;
465 *secsize = pinfo.media_blksize;
470 disk_invalidate (struct disk *disk)
472 devfs_debug(DEVFS_DEBUG_INFO, "disk_invalidate for %s\n", disk->d_cdev->si_name);
474 dsgone(&disk->d_slice);
478 disk_enumerate(struct disk *disk)
481 return (LIST_FIRST(&disklist));
483 return (LIST_NEXT(disk, d_list));
488 sysctl_disks(SYSCTL_HANDLER_ARGS)
496 while ((disk = disk_enumerate(disk))) {
498 error = SYSCTL_OUT(req, " ", 1);
504 error = SYSCTL_OUT(req, disk->d_rawdev->si_name,
505 strlen(disk->d_rawdev->si_name));
509 error = SYSCTL_OUT(req, "", 1);
513 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
514 sysctl_disks, "A", "names of available disks");
517 * Open a disk device or partition.
521 diskopen(struct dev_open_args *ap)
523 cdev_t dev = ap->a_head.a_dev;
527 devfs_debug(DEVFS_DEBUG_DEBUG, "diskopen: name is %s\n", dev->si_name);
530 * dp can't be NULL here XXX.
538 * Deal with open races
540 while (dp->d_flags & DISKFLAG_LOCK) {
541 dp->d_flags |= DISKFLAG_WANTED;
542 error = tsleep(dp, PCATCH, "diskopen", hz);
546 dp->d_flags |= DISKFLAG_LOCK;
548 devfs_debug(DEVFS_DEBUG_DEBUG, "diskopen: -2- name is %s\n", dev->si_name);
551 * Open the underlying raw device.
553 if (!dsisopen(dp->d_slice)) {
555 if (!pdev->si_iosize_max)
556 pdev->si_iosize_max = dev->si_iosize_max;
558 error = dev_dopen(dp->d_rawdev, ap->a_oflags,
559 ap->a_devtype, ap->a_cred);
563 * Inherit properties from the underlying device now that it is
571 error = dsopen(dev, ap->a_devtype, dp->d_info.d_dsflags,
572 &dp->d_slice, &dp->d_info);
573 if (!dsisopen(dp->d_slice)) {
574 dev_dclose(dp->d_rawdev, ap->a_oflags, ap->a_devtype);
577 dp->d_flags &= ~DISKFLAG_LOCK;
578 if (dp->d_flags & DISKFLAG_WANTED) {
579 dp->d_flags &= ~DISKFLAG_WANTED;
587 * Close a disk device or partition
591 diskclose(struct dev_close_args *ap)
593 cdev_t dev = ap->a_head.a_dev;
600 devfs_debug(DEVFS_DEBUG_DEBUG, "diskclose: name %s\n", dev->si_name);
602 dsclose(dev, ap->a_devtype, dp->d_slice);
603 if (!dsisopen(dp->d_slice)) {
604 devfs_debug(DEVFS_DEBUG_DEBUG, "diskclose is closing underlying device\n");
605 error = dev_dclose(dp->d_rawdev, ap->a_fflag, ap->a_devtype);
611 * First execute the ioctl on the disk device, and if it isn't supported
612 * try running it on the backing device.
616 diskioctl(struct dev_ioctl_args *ap)
618 cdev_t dev = ap->a_head.a_dev;
626 devfs_debug(DEVFS_DEBUG_DEBUG, "diskioctl: cmd is: %x (name: %s)\n", ap->a_cmd, dev->si_name);
627 devfs_debug(DEVFS_DEBUG_DEBUG, "diskioctl: &dp->d_slice is: %x, %x\n", &dp->d_slice, dp->d_slice);
629 devfs_debug(DEVFS_DEBUG_DEBUG, "diskioctl:1: says lp.opaque is: %x\n", dp->d_slice->dss_slices[0].ds_label.opaque);
631 error = dsioctl(dev, ap->a_cmd, ap->a_data, ap->a_fflag,
632 &dp->d_slice, &dp->d_info);
634 devfs_debug(DEVFS_DEBUG_DEBUG, "diskioctl:2: says lp.opaque is: %x\n", dp->d_slice->dss_slices[0].ds_label.opaque);
636 if (error == ENOIOCTL) {
637 devfs_debug(DEVFS_DEBUG_DEBUG, "diskioctl: going for dev_dioctl instead!\n");
638 error = dev_dioctl(dp->d_rawdev, ap->a_cmd, ap->a_data,
639 ap->a_fflag, ap->a_cred);
645 * Execute strategy routine
649 diskstrategy(struct dev_strategy_args *ap)
651 cdev_t dev = ap->a_head.a_dev;
652 struct bio *bio = ap->a_bio;
659 bio->bio_buf->b_error = ENXIO;
660 bio->bio_buf->b_flags |= B_ERROR;
664 KKASSERT(dev->si_disk == dp);
667 * The dscheck() function will also transform the slice relative
668 * block number i.e. bio->bio_offset into a block number that can be
669 * passed directly to the underlying raw device. If dscheck()
670 * returns NULL it will have handled the bio for us (e.g. EOF
671 * or error due to being beyond the device size).
673 if ((nbio = dscheck(dev, bio, dp->d_slice)) != NULL) {
674 dev_dstrategy(dp->d_rawdev, nbio);
676 devfs_debug(DEVFS_DEBUG_DEBUG, "diskstrategy: dscheck NULL!!! biodone time!\n");
683 * Return the partition size in ?blocks?
687 diskpsize(struct dev_psize_args *ap)
689 cdev_t dev = ap->a_head.a_dev;
695 ap->a_result = dssize(dev, &dp->d_slice);
700 * When new device entries are instantiated, make sure they inherit our
701 * si_disk structure and block and iosize limits from the raw device.
703 * This routine is always called synchronously in the context of the
706 * XXX The various io and block size constraints are not always initialized
707 * properly by devices.
711 diskclone(struct dev_clone_args *ap)
713 cdev_t dev = ap->a_head.a_dev;
715 //XXX: need changes for devfs
716 dp = dev->si_ops->head.data;
717 KKASSERT(dp != NULL);
719 dev->si_iosize_max = dp->d_rawdev->si_iosize_max;
720 dev->si_bsize_phys = dp->d_rawdev->si_bsize_phys;
721 dev->si_bsize_best = dp->d_rawdev->si_bsize_best;
726 diskdump(struct dev_dump_args *ap)
728 cdev_t dev = ap->a_head.a_dev;
729 struct disk *dp = dev->si_ops->head.data;
732 error = disk_dumpcheck(dev, &ap->a_count, &ap->a_blkno, &ap->a_secsize);
734 ap->a_head.a_dev = dp->d_rawdev;
735 error = dev_doperate(&ap->a_head);
742 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
743 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
745 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
746 0, sizeof(struct disk), "sizeof(struct disk)");
750 * Seek sort for disks.
752 * The bio_queue keep two queues, sorted in ascending block order. The first
753 * queue holds those requests which are positioned after the current block
754 * (in the first request); the second, which starts at queue->switch_point,
755 * holds requests which came in after their block number was passed. Thus
756 * we implement a one way scan, retracting after reaching the end of the drive
757 * to the first request on the second queue, at which time it becomes the
760 * A one-way scan is natural because of the way UNIX read-ahead blocks are
764 bioqdisksort(struct bio_queue_head *bioq, struct bio *bio)
770 be = TAILQ_LAST(&bioq->queue, bio_queue);
772 * If the queue is empty or we are an
773 * ordered transaction, then it's easy.
775 if ((bq = bioq_first(bioq)) == NULL ||
776 (bio->bio_buf->b_flags & B_ORDERED) != 0) {
777 bioq_insert_tail(bioq, bio);
779 } else if (bioq->insert_point != NULL) {
782 * A certain portion of the list is
783 * "locked" to preserve ordering, so
784 * we can only insert after the insert
787 bq = bioq->insert_point;
791 * If we lie before the last removed (currently active)
792 * request, and are not inserting ourselves into the
793 * "locked" portion of the list, then we must add ourselves
794 * to the second request list.
796 if (bio->bio_offset < bioq->last_offset) {
797 bq = bioq->switch_point;
799 * If we are starting a new secondary list,
803 bioq->switch_point = bio;
804 bioq_insert_tail(bioq, bio);
808 * If we lie ahead of the current switch point,
809 * insert us before the switch point and move
812 if (bio->bio_offset < bq->bio_offset) {
813 bioq->switch_point = bio;
814 TAILQ_INSERT_BEFORE(bq, bio, bio_act);
818 if (bioq->switch_point != NULL)
819 be = TAILQ_PREV(bioq->switch_point,
822 * If we lie between last_offset and bq,
825 if (bio->bio_offset < bq->bio_offset) {
826 TAILQ_INSERT_BEFORE(bq, bio, bio_act);
833 * Request is at/after our current position in the list.
834 * Optimize for sequential I/O by seeing if we go at the tail.
836 if (bio->bio_offset > be->bio_offset) {
837 TAILQ_INSERT_AFTER(&bioq->queue, be, bio, bio_act);
841 /* Otherwise, insertion sort */
842 while ((bn = TAILQ_NEXT(bq, bio_act)) != NULL) {
845 * We want to go after the current request if it is the end
846 * of the first request list, or if the next request is a
847 * larger cylinder than our request.
849 if (bn == bioq->switch_point
850 || bio->bio_offset < bn->bio_offset)
854 TAILQ_INSERT_AFTER(&bioq->queue, bq, bio, bio_act);
858 * Disk error is the preface to plaintive error messages
859 * about failing disk transfers. It prints messages of the form
861 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
863 * if the offset of the error in the transfer and a disk label
864 * are both available. blkdone should be -1 if the position of the error
865 * is unknown; the disklabel pointer may be null from drivers that have not
866 * been converted to use them. The message is printed with kprintf
867 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
868 * The message should be completed (with at least a newline) with kprintf
869 * or log(-1, ...), respectively. There is no trailing space.
872 diskerr(struct bio *bio, cdev_t dev, const char *what, int pri, int donecnt)
874 struct buf *bp = bio->bio_buf;
888 //sname = dsname(dev, unit, slice, part, partname);
889 kprintf("%s: %s %sing ", dev->si_name, what, term);
890 kprintf("offset %012llx for %d",
891 (long long)bio->bio_offset,
895 kprintf(" (%d bytes completed)", donecnt);
899 * Locate a disk device
902 disk_locate(const char *devname)
904 return devfs_find_device_by_name(devname);
909 disk_config(void *arg)
911 struct lwkt_port rep_port;
912 disk_msg_t disk_msg = objcache_get(disk_msg_cache, M_WAITOK);
913 disk_msg_t msg_incoming;
914 lwkt_port_t port = &disk_msg_port;
916 lwkt_initport_thread(&rep_port, curthread);
917 lwkt_initmsg(&disk_msg->hdr, &rep_port, 0);
918 kprintf("disk_config: sync'ing up\n");
919 disk_msg->hdr.u.ms_result = DISK_SYNC;
921 lwkt_sendmsg(port, (lwkt_msg_t)disk_msg);
922 msg_incoming = lwkt_waitport(&rep_port, 0);
929 struct thread* td_core;
930 devfs_debug(DEVFS_DEBUG_DEBUG, "disk_init() called\n");
932 disk_msg_cache = objcache_create("disk-msg-cache", 0, 0,
934 objcache_malloc_alloc,
935 objcache_malloc_free,
936 &disk_msg_malloc_args );
938 /* Initialize the reply-only port which acts as a message drain */
939 lwkt_initport_replyonly(&disk_dispose_port, disk_msg_autofree_reply);
941 lwkt_create(disk_msg_core, /*args*/NULL, &td_core, NULL,
942 0, 0, "disk_msg_core");
944 tsleep(td_core, 0, "diskcore", 0);
951 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
953 objcache_destroy(disk_msg_cache);
958 SYSINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, disk_init, NULL);
959 SYSUNINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, disk_uninit, NULL);