drm: Add atomic_cmpxchg()
[dragonfly.git] / sys / kern / subr_disk.c
blob67d1be452a75416e37362224e2f11ada8d9e1ac7
1 /*
2 * Copyright (c) 2003,2004,2009 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>
6 * and Alex Hornung <ahornung@gmail.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 * ----------------------------------------------------------------------------
36 * "THE BEER-WARE LICENSE" (Revision 42):
37 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
38 * can do whatever you want with this stuff. If we meet some day, and you think
39 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
40 * ----------------------------------------------------------------------------
42 * Copyright (c) 1982, 1986, 1988, 1993
43 * The Regents of the University of California. All rights reserved.
44 * (c) UNIX System Laboratories, Inc.
45 * All or some portions of this file are derived from material licensed
46 * to the University of California by American Telephone and Telegraph
47 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
48 * the permission of UNIX System Laboratories, Inc.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
74 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
75 * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
76 * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/proc.h>
83 #include <sys/sysctl.h>
84 #include <sys/buf.h>
85 #include <sys/conf.h>
86 #include <sys/disklabel.h>
87 #include <sys/disklabel32.h>
88 #include <sys/disklabel64.h>
89 #include <sys/diskslice.h>
90 #include <sys/diskmbr.h>
91 #include <sys/disk.h>
92 #include <sys/kerneldump.h>
93 #include <sys/malloc.h>
94 #include <machine/md_var.h>
95 #include <sys/ctype.h>
96 #include <sys/syslog.h>
97 #include <sys/device.h>
98 #include <sys/msgport.h>
99 #include <sys/devfs.h>
100 #include <sys/thread.h>
101 #include <sys/dsched.h>
102 #include <sys/queue.h>
103 #include <sys/lock.h>
104 #include <sys/udev.h>
105 #include <sys/uuid.h>
107 #include <sys/buf2.h>
108 #include <sys/mplock2.h>
109 #include <sys/msgport2.h>
110 #include <sys/thread2.h>
112 static MALLOC_DEFINE(M_DISK, "disk", "disk data");
113 static int disk_debug_enable = 0;
115 static void disk_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
116 static void disk_msg_core(void *);
117 static int disk_probe_slice(struct disk *dp, cdev_t dev, int slice, int reprobe);
118 static void disk_probe(struct disk *dp, int reprobe);
119 static void _setdiskinfo(struct disk *disk, struct disk_info *info);
120 static void bioqwritereorder(struct bio_queue_head *bioq);
121 static void disk_cleanserial(char *serno);
122 static int disk_debug(int, char *, ...) __printflike(2, 3);
123 static cdev_t _disk_create_named(const char *name, int unit, struct disk *dp,
124 struct dev_ops *raw_ops, int clone);
126 static d_open_t diskopen;
127 static d_close_t diskclose;
128 static d_ioctl_t diskioctl;
129 static d_strategy_t diskstrategy;
130 static d_psize_t diskpsize;
131 static d_dump_t diskdump;
133 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
134 static struct lwkt_token disklist_token;
136 static struct dev_ops disk_ops = {
137 { "disk", 0, D_DISK | D_MPSAFE | D_TRACKCLOSE },
138 .d_open = diskopen,
139 .d_close = diskclose,
140 .d_read = physread,
141 .d_write = physwrite,
142 .d_ioctl = diskioctl,
143 .d_strategy = diskstrategy,
144 .d_dump = diskdump,
145 .d_psize = diskpsize,
148 static struct objcache *disk_msg_cache;
150 struct objcache_malloc_args disk_msg_malloc_args = {
151 sizeof(struct disk_msg), M_DISK };
153 static struct lwkt_port disk_dispose_port;
154 static struct lwkt_port disk_msg_port;
156 static int
157 disk_debug(int level, char *fmt, ...)
159 __va_list ap;
161 __va_start(ap, fmt);
162 if (level <= disk_debug_enable)
163 kvprintf(fmt, ap);
164 __va_end(ap);
166 return 0;
169 static int
170 disk_probe_slice(struct disk *dp, cdev_t dev, int slice, int reprobe)
172 struct disk_info *info = &dp->d_info;
173 struct diskslice *sp = &dp->d_slice->dss_slices[slice];
174 disklabel_ops_t ops;
175 struct partinfo part;
176 const char *msg;
177 char uuid_buf[128];
178 cdev_t ndev;
179 int sno;
180 u_int i;
182 disk_debug(2, "disk_probe_slice (begin): %s (%s)\n",
183 dev->si_name, dp->d_cdev->si_name);
185 sno = slice ? slice - 1 : 0;
187 ops = &disklabel32_ops;
188 msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info);
189 if (msg && !strcmp(msg, "no disk label")) {
190 ops = &disklabel64_ops;
191 msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info);
194 if (msg == NULL) {
195 if (slice != WHOLE_DISK_SLICE)
196 ops->op_adjust_label_reserved(dp->d_slice, slice, sp);
197 else
198 sp->ds_reserved = 0;
200 sp->ds_ops = ops;
201 for (i = 0; i < ops->op_getnumparts(sp->ds_label); i++) {
202 ops->op_loadpartinfo(sp->ds_label, i, &part);
203 if (part.fstype) {
204 if (reprobe &&
205 (ndev = devfs_find_device_by_name("%s%c",
206 dev->si_name, 'a' + i))
209 * Device already exists and
210 * is still valid.
212 ndev->si_flags |= SI_REPROBE_TEST;
215 * Destroy old UUID alias
217 destroy_dev_alias(ndev, "part-by-uuid/*");
219 /* Create UUID alias */
220 if (!kuuid_is_nil(&part.storage_uuid)) {
221 snprintf_uuid(uuid_buf,
222 sizeof(uuid_buf),
223 &part.storage_uuid);
224 make_dev_alias(ndev,
225 "part-by-uuid/%s",
226 uuid_buf);
227 udev_dict_set_cstr(ndev, "uuid", uuid_buf);
229 } else {
230 ndev = make_dev_covering(&disk_ops, dp->d_rawdev->si_ops,
231 dkmakeminor(dkunit(dp->d_cdev),
232 slice, i),
233 UID_ROOT, GID_OPERATOR, 0640,
234 "%s%c", dev->si_name, 'a'+ i);
235 ndev->si_parent = dev;
236 ndev->si_iosize_max = dev->si_iosize_max;
237 ndev->si_disk = dp;
238 udev_dict_set_cstr(ndev, "subsystem", "disk");
239 /* Inherit parent's disk type */
240 if (dp->d_disktype) {
241 udev_dict_set_cstr(ndev, "disk-type",
242 __DECONST(char *, dp->d_disktype));
245 /* Create serno alias */
246 if (dp->d_info.d_serialno) {
247 make_dev_alias(ndev,
248 "serno/%s.s%d%c",
249 dp->d_info.d_serialno,
250 sno, 'a' + i);
253 /* Create UUID alias */
254 if (!kuuid_is_nil(&part.storage_uuid)) {
255 snprintf_uuid(uuid_buf,
256 sizeof(uuid_buf),
257 &part.storage_uuid);
258 make_dev_alias(ndev,
259 "part-by-uuid/%s",
260 uuid_buf);
261 udev_dict_set_cstr(ndev, "uuid", uuid_buf);
263 ndev->si_flags |= SI_REPROBE_TEST;
267 } else if (info->d_dsflags & DSO_COMPATLABEL) {
268 msg = NULL;
269 if (sp->ds_size >= 0x100000000ULL)
270 ops = &disklabel64_ops;
271 else
272 ops = &disklabel32_ops;
273 sp->ds_label = ops->op_clone_label(info, sp);
274 } else {
275 if (sp->ds_type == DOSPTYP_386BSD || /* XXX */
276 sp->ds_type == DOSPTYP_NETBSD ||
277 sp->ds_type == DOSPTYP_OPENBSD) {
278 log(LOG_WARNING, "%s: cannot find label (%s)\n",
279 dev->si_name, msg);
282 if (sp->ds_label.opaque != NULL && sp->ds_ops != NULL) {
283 /* Clear out old label - it's not around anymore */
284 disk_debug(2,
285 "disk_probe_slice: clear out old diskabel on %s\n",
286 dev->si_name);
288 sp->ds_ops->op_freedisklabel(&sp->ds_label);
289 sp->ds_ops = NULL;
293 if (msg == NULL) {
294 sp->ds_wlabel = FALSE;
297 return (msg ? EINVAL : 0);
301 * This routine is only called for newly minted drives or to reprobe
302 * a drive with no open slices. disk_probe_slice() is called directly
303 * when reprobing partition changes within slices.
305 static void
306 disk_probe(struct disk *dp, int reprobe)
308 struct disk_info *info = &dp->d_info;
309 cdev_t dev = dp->d_cdev;
310 cdev_t ndev;
311 int error, i, sno;
312 struct diskslices *osp;
313 struct diskslice *sp;
314 char uuid_buf[128];
316 KKASSERT (info->d_media_blksize != 0);
318 osp = dp->d_slice;
319 dp->d_slice = dsmakeslicestruct(BASE_SLICE, info);
320 disk_debug(1, "disk_probe (begin): %s\n", dp->d_cdev->si_name);
322 error = mbrinit(dev, info, &(dp->d_slice));
323 if (error) {
324 dsgone(&osp);
325 return;
328 for (i = 0; i < dp->d_slice->dss_nslices; i++) {
330 * Ignore the whole-disk slice, it has already been created.
332 if (i == WHOLE_DISK_SLICE)
333 continue;
335 #if 1
337 * Ignore the compatibility slice s0 if it's a device mapper
338 * volume.
340 if ((i == COMPATIBILITY_SLICE) &&
341 (info->d_dsflags & DSO_DEVICEMAPPER))
342 continue;
343 #endif
345 sp = &dp->d_slice->dss_slices[i];
348 * Handle s0. s0 is a compatibility slice if there are no
349 * other slices and it has not otherwise been set up, else
350 * we ignore it.
352 if (i == COMPATIBILITY_SLICE) {
353 sno = 0;
354 if (sp->ds_type == 0 &&
355 dp->d_slice->dss_nslices == BASE_SLICE) {
356 sp->ds_size = info->d_media_blocks;
357 sp->ds_reserved = 0;
359 } else {
360 sno = i - 1;
361 sp->ds_reserved = 0;
365 * Ignore 0-length slices
367 if (sp->ds_size == 0)
368 continue;
370 if (reprobe &&
371 (ndev = devfs_find_device_by_name("%ss%d",
372 dev->si_name, sno))) {
374 * Device already exists and is still valid
376 ndev->si_flags |= SI_REPROBE_TEST;
379 * Destroy old UUID alias
381 destroy_dev_alias(ndev, "slice-by-uuid/*");
383 /* Create UUID alias */
384 if (!kuuid_is_nil(&sp->ds_stor_uuid)) {
385 snprintf_uuid(uuid_buf, sizeof(uuid_buf),
386 &sp->ds_stor_uuid);
387 make_dev_alias(ndev, "slice-by-uuid/%s",
388 uuid_buf);
390 } else {
392 * Else create new device
394 ndev = make_dev_covering(&disk_ops, dp->d_rawdev->si_ops,
395 dkmakewholeslice(dkunit(dev), i),
396 UID_ROOT, GID_OPERATOR, 0640,
397 (info->d_dsflags & DSO_DEVICEMAPPER)?
398 "%s.s%d" : "%ss%d", dev->si_name, sno);
399 ndev->si_parent = dev;
400 ndev->si_iosize_max = dev->si_iosize_max;
401 udev_dict_set_cstr(ndev, "subsystem", "disk");
402 /* Inherit parent's disk type */
403 if (dp->d_disktype) {
404 udev_dict_set_cstr(ndev, "disk-type",
405 __DECONST(char *, dp->d_disktype));
408 /* Create serno alias */
409 if (dp->d_info.d_serialno) {
410 make_dev_alias(ndev, "serno/%s.s%d",
411 dp->d_info.d_serialno, sno);
414 /* Create UUID alias */
415 if (!kuuid_is_nil(&sp->ds_stor_uuid)) {
416 snprintf_uuid(uuid_buf, sizeof(uuid_buf),
417 &sp->ds_stor_uuid);
418 make_dev_alias(ndev, "slice-by-uuid/%s",
419 uuid_buf);
422 ndev->si_disk = dp;
423 ndev->si_flags |= SI_REPROBE_TEST;
425 sp->ds_dev = ndev;
428 * Probe appropriate slices for a disklabel
430 * XXX slice type 1 used by our gpt probe code.
431 * XXX slice type 0 used by mbr compat slice.
433 if (sp->ds_type == DOSPTYP_386BSD ||
434 sp->ds_type == DOSPTYP_NETBSD ||
435 sp->ds_type == DOSPTYP_OPENBSD ||
436 sp->ds_type == 0 ||
437 sp->ds_type == 1) {
438 if (dp->d_slice->dss_first_bsd_slice == 0)
439 dp->d_slice->dss_first_bsd_slice = i;
440 disk_probe_slice(dp, ndev, i, reprobe);
443 dsgone(&osp);
444 disk_debug(1, "disk_probe (end): %s\n", dp->d_cdev->si_name);
448 static void
449 disk_msg_core(void *arg)
451 struct disk *dp;
452 struct diskslice *sp;
453 disk_msg_t msg;
454 int run;
456 lwkt_gettoken(&disklist_token);
457 lwkt_initport_thread(&disk_msg_port, curthread);
458 wakeup(curthread); /* synchronous startup */
459 lwkt_reltoken(&disklist_token);
461 get_mplock(); /* not mpsafe yet? */
462 run = 1;
464 while (run) {
465 msg = (disk_msg_t)lwkt_waitport(&disk_msg_port, 0);
467 switch (msg->hdr.u.ms_result) {
468 case DISK_DISK_PROBE:
469 dp = (struct disk *)msg->load;
470 disk_debug(1,
471 "DISK_DISK_PROBE: %s\n",
472 dp->d_cdev->si_name);
473 disk_iocom_update(dp);
474 disk_probe(dp, 0);
475 break;
476 case DISK_DISK_DESTROY:
477 dp = (struct disk *)msg->load;
478 disk_debug(1,
479 "DISK_DISK_DESTROY: %s\n",
480 dp->d_cdev->si_name);
481 disk_iocom_uninit(dp);
484 * Interlock against struct disk enumerations.
485 * Wait for enumerations to complete then remove
486 * the dp from the list before tearing it down.
488 * This avoids races against e.g.
489 * dsched_thread_io_alloc().
491 lwkt_gettoken(&disklist_token);
492 while (dp->d_refs)
493 tsleep(&dp->d_refs, 0, "diskdel", hz / 10);
494 LIST_REMOVE(dp, d_list);
496 dsched_disk_destroy_callback(dp);
497 devfs_destroy_related(dp->d_cdev);
498 destroy_dev(dp->d_cdev);
499 destroy_only_dev(dp->d_rawdev);
501 lwkt_reltoken(&disklist_token);
503 if (dp->d_info.d_serialno) {
504 kfree(dp->d_info.d_serialno, M_TEMP);
505 dp->d_info.d_serialno = NULL;
507 break;
508 case DISK_UNPROBE:
509 dp = (struct disk *)msg->load;
510 disk_debug(1,
511 "DISK_DISK_UNPROBE: %s\n",
512 dp->d_cdev->si_name);
513 devfs_destroy_related(dp->d_cdev);
514 break;
515 case DISK_SLICE_REPROBE:
516 dp = (struct disk *)msg->load;
517 sp = (struct diskslice *)msg->load2;
518 devfs_clr_related_flag(sp->ds_dev,
519 SI_REPROBE_TEST);
520 disk_debug(1,
521 "DISK_SLICE_REPROBE: %s\n",
522 sp->ds_dev->si_name);
523 disk_probe_slice(dp, sp->ds_dev,
524 dkslice(sp->ds_dev), 1);
525 devfs_destroy_related_without_flag(
526 sp->ds_dev, SI_REPROBE_TEST);
527 break;
528 case DISK_DISK_REPROBE:
529 dp = (struct disk *)msg->load;
530 devfs_clr_related_flag(dp->d_cdev, SI_REPROBE_TEST);
531 disk_debug(1,
532 "DISK_DISK_REPROBE: %s\n",
533 dp->d_cdev->si_name);
534 disk_probe(dp, 1);
535 devfs_destroy_related_without_flag(
536 dp->d_cdev, SI_REPROBE_TEST);
537 break;
538 case DISK_SYNC:
539 disk_debug(1, "DISK_SYNC\n");
540 break;
541 default:
542 devfs_debug(DEVFS_DEBUG_WARNING,
543 "disk_msg_core: unknown message "
544 "received at core\n");
545 break;
547 lwkt_replymsg(&msg->hdr, 0);
549 lwkt_exit();
554 * Acts as a message drain. Any message that is replied to here gets
555 * destroyed and the memory freed.
557 static void
558 disk_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
560 objcache_put(disk_msg_cache, msg);
564 void
565 disk_msg_send(uint32_t cmd, void *load, void *load2)
567 disk_msg_t disk_msg;
568 lwkt_port_t port = &disk_msg_port;
570 disk_msg = objcache_get(disk_msg_cache, M_WAITOK);
572 lwkt_initmsg(&disk_msg->hdr, &disk_dispose_port, 0);
574 disk_msg->hdr.u.ms_result = cmd;
575 disk_msg->load = load;
576 disk_msg->load2 = load2;
577 KKASSERT(port);
578 lwkt_sendmsg(port, &disk_msg->hdr);
581 void
582 disk_msg_send_sync(uint32_t cmd, void *load, void *load2)
584 struct lwkt_port rep_port;
585 disk_msg_t disk_msg;
586 lwkt_port_t port;
588 disk_msg = objcache_get(disk_msg_cache, M_WAITOK);
589 port = &disk_msg_port;
591 /* XXX could probably use curthread's built-in msgport */
592 lwkt_initport_thread(&rep_port, curthread);
593 lwkt_initmsg(&disk_msg->hdr, &rep_port, 0);
595 disk_msg->hdr.u.ms_result = cmd;
596 disk_msg->load = load;
597 disk_msg->load2 = load2;
599 lwkt_domsg(port, &disk_msg->hdr, 0);
600 objcache_put(disk_msg_cache, disk_msg);
604 * Create a raw device for the dev_ops template (which is returned). Also
605 * create a slice and unit managed disk and overload the user visible
606 * device space with it.
608 * NOTE: The returned raw device is NOT a slice and unit managed device.
609 * It is an actual raw device representing the raw disk as specified by
610 * the passed dev_ops. The disk layer not only returns such a raw device,
611 * it also uses it internally when passing (modified) commands through.
613 cdev_t
614 disk_create(int unit, struct disk *dp, struct dev_ops *raw_ops)
616 return _disk_create_named(NULL, unit, dp, raw_ops, 0);
619 cdev_t
620 disk_create_clone(int unit, struct disk *dp,
621 struct dev_ops *raw_ops)
623 return _disk_create_named(NULL, unit, dp, raw_ops, 1);
626 cdev_t
627 disk_create_named(const char *name, int unit, struct disk *dp,
628 struct dev_ops *raw_ops)
630 return _disk_create_named(name, unit, dp, raw_ops, 0);
633 cdev_t
634 disk_create_named_clone(const char *name, int unit, struct disk *dp,
635 struct dev_ops *raw_ops)
637 return _disk_create_named(name, unit, dp, raw_ops, 1);
640 static cdev_t
641 _disk_create_named(const char *name, int unit, struct disk *dp,
642 struct dev_ops *raw_ops, int clone)
644 cdev_t rawdev;
646 disk_debug(1, "disk_create (begin): %s%d\n", name, unit);
648 if (name) {
649 rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit),
650 UID_ROOT, GID_OPERATOR, 0640, "%s", name);
651 } else {
652 rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit),
653 UID_ROOT, GID_OPERATOR, 0640,
654 "%s%d", raw_ops->head.name, unit);
657 bzero(dp, sizeof(*dp));
659 dp->d_rawdev = rawdev;
660 dp->d_raw_ops = raw_ops;
661 dp->d_dev_ops = &disk_ops;
663 if (name) {
664 if (clone) {
665 dp->d_cdev = make_only_dev_covering(
666 &disk_ops, dp->d_rawdev->si_ops,
667 dkmakewholedisk(unit),
668 UID_ROOT, GID_OPERATOR, 0640,
669 "%s", name);
670 } else {
671 dp->d_cdev = make_dev_covering(
672 &disk_ops, dp->d_rawdev->si_ops,
673 dkmakewholedisk(unit),
674 UID_ROOT, GID_OPERATOR, 0640,
675 "%s", name);
677 } else {
678 if (clone) {
679 dp->d_cdev = make_only_dev_covering(
680 &disk_ops, dp->d_rawdev->si_ops,
681 dkmakewholedisk(unit),
682 UID_ROOT, GID_OPERATOR, 0640,
683 "%s%d", raw_ops->head.name, unit);
684 } else {
685 dp->d_cdev = make_dev_covering(
686 &disk_ops, dp->d_rawdev->si_ops,
687 dkmakewholedisk(unit),
688 UID_ROOT, GID_OPERATOR, 0640,
689 "%s%d", raw_ops->head.name, unit);
693 udev_dict_set_cstr(dp->d_cdev, "subsystem", "disk");
694 dp->d_cdev->si_disk = dp;
696 if (name)
697 dsched_disk_create_callback(dp, name, unit);
698 else
699 dsched_disk_create_callback(dp, raw_ops->head.name, unit);
701 lwkt_gettoken(&disklist_token);
702 LIST_INSERT_HEAD(&disklist, dp, d_list);
703 lwkt_reltoken(&disklist_token);
705 disk_iocom_init(dp);
707 disk_debug(1, "disk_create (end): %s%d\n",
708 (name != NULL)?(name):(raw_ops->head.name), unit);
710 return (dp->d_rawdev);
714 disk_setdisktype(struct disk *disk, const char *type)
716 int error;
718 KKASSERT(disk != NULL);
720 disk->d_disktype = type;
721 error = udev_dict_set_cstr(disk->d_cdev, "disk-type",
722 __DECONST(char *, type));
723 return error;
727 disk_getopencount(struct disk *disk)
729 return disk->d_opencount;
732 static void
733 _setdiskinfo(struct disk *disk, struct disk_info *info)
735 char *oldserialno;
737 oldserialno = disk->d_info.d_serialno;
738 bcopy(info, &disk->d_info, sizeof(disk->d_info));
739 info = &disk->d_info;
741 disk_debug(1, "_setdiskinfo: %s\n", disk->d_cdev->si_name);
744 * The serial number is duplicated so the caller can throw
745 * their copy away.
747 if (info->d_serialno && info->d_serialno[0] &&
748 (info->d_serialno[0] != ' ' || strlen(info->d_serialno) > 1)) {
749 info->d_serialno = kstrdup(info->d_serialno, M_TEMP);
750 disk_cleanserial(info->d_serialno);
751 if (disk->d_cdev) {
752 make_dev_alias(disk->d_cdev, "serno/%s",
753 info->d_serialno);
755 } else {
756 info->d_serialno = NULL;
758 if (oldserialno)
759 kfree(oldserialno, M_TEMP);
761 dsched_disk_update_callback(disk, info);
764 * The caller may set d_media_size or d_media_blocks and we
765 * calculate the other.
767 KKASSERT(info->d_media_size == 0 || info->d_media_blocks == 0);
768 if (info->d_media_size == 0 && info->d_media_blocks) {
769 info->d_media_size = (u_int64_t)info->d_media_blocks *
770 info->d_media_blksize;
771 } else if (info->d_media_size && info->d_media_blocks == 0 &&
772 info->d_media_blksize) {
773 info->d_media_blocks = info->d_media_size /
774 info->d_media_blksize;
778 * The si_* fields for rawdev are not set until after the
779 * disk_create() call, so someone using the cooked version
780 * of the raw device (i.e. da0s0) will not get the right
781 * si_iosize_max unless we fix it up here.
783 if (disk->d_cdev && disk->d_rawdev &&
784 disk->d_cdev->si_iosize_max == 0) {
785 disk->d_cdev->si_iosize_max = disk->d_rawdev->si_iosize_max;
786 disk->d_cdev->si_bsize_phys = disk->d_rawdev->si_bsize_phys;
787 disk->d_cdev->si_bsize_best = disk->d_rawdev->si_bsize_best;
790 /* Add the serial number to the udev_dictionary */
791 if (info->d_serialno)
792 udev_dict_set_cstr(disk->d_cdev, "serno", info->d_serialno);
796 * Disk drivers must call this routine when media parameters are available
797 * or have changed.
799 void
800 disk_setdiskinfo(struct disk *disk, struct disk_info *info)
802 _setdiskinfo(disk, info);
803 disk_msg_send(DISK_DISK_PROBE, disk, NULL);
804 disk_debug(1, "disk_setdiskinfo: sent probe for %s\n",
805 disk->d_cdev->si_name);
808 void
809 disk_setdiskinfo_sync(struct disk *disk, struct disk_info *info)
811 _setdiskinfo(disk, info);
812 disk_msg_send_sync(DISK_DISK_PROBE, disk, NULL);
813 disk_debug(1, "disk_setdiskinfo_sync: sent probe for %s\n",
814 disk->d_cdev->si_name);
818 * This routine is called when an adapter detaches. The higher level
819 * managed disk device is destroyed while the lower level raw device is
820 * released.
822 void
823 disk_destroy(struct disk *disk)
825 disk_msg_send_sync(DISK_DISK_DESTROY, disk, NULL);
826 return;
830 disk_dumpcheck(cdev_t dev, u_int64_t *size,
831 u_int64_t *blkno, u_int32_t *secsize)
833 struct partinfo pinfo;
834 int error;
836 bzero(&pinfo, sizeof(pinfo));
837 error = dev_dioctl(dev, DIOCGPART, (void *)&pinfo, 0,
838 proc0.p_ucred, NULL, NULL);
839 if (error)
840 return (error);
842 if (pinfo.media_blksize == 0)
843 return (ENXIO);
845 if (blkno) /* XXX: make sure this reserved stuff is right */
846 *blkno = pinfo.reserved_blocks +
847 pinfo.media_offset / pinfo.media_blksize;
848 if (secsize)
849 *secsize = pinfo.media_blksize;
850 if (size)
851 *size = (pinfo.media_blocks - pinfo.reserved_blocks);
853 return (0);
857 disk_dumpconf(cdev_t dev, u_int onoff)
859 struct dumperinfo di;
860 u_int64_t size, blkno;
861 u_int32_t secsize;
862 int error;
864 if (!onoff)
865 return set_dumper(NULL);
867 error = disk_dumpcheck(dev, &size, &blkno, &secsize);
869 if (error)
870 return ENXIO;
872 bzero(&di, sizeof(struct dumperinfo));
873 di.dumper = diskdump;
874 di.priv = dev;
875 di.blocksize = secsize;
876 di.maxiosize = dev->si_iosize_max;
877 di.mediaoffset = blkno * DEV_BSIZE;
878 di.mediasize = size * DEV_BSIZE;
880 return set_dumper(&di);
883 void
884 disk_unprobe(struct disk *disk)
886 if (disk == NULL)
887 return;
889 disk_msg_send_sync(DISK_UNPROBE, disk, NULL);
892 void
893 disk_invalidate (struct disk *disk)
895 dsgone(&disk->d_slice);
899 * Enumerate disks, pass a marker and an initial NULL dp to initialize,
900 * then loop with the previously returned dp.
902 * The returned dp will be referenced, preventing its destruction. When
903 * you pass the returned dp back into the loop the ref is dropped.
905 * WARNING: If terminating your loop early you must call
906 * disk_enumerate_stop().
908 struct disk *
909 disk_enumerate(struct disk *marker, struct disk *dp)
911 lwkt_gettoken(&disklist_token);
912 if (dp) {
913 --dp->d_refs;
914 dp = LIST_NEXT(marker, d_list);
915 LIST_REMOVE(marker, d_list);
916 } else {
917 bzero(marker, sizeof(*marker));
918 marker->d_flags = DISKFLAG_MARKER;
919 dp = LIST_FIRST(&disklist);
921 while (dp) {
922 if ((dp->d_flags & DISKFLAG_MARKER) == 0)
923 break;
924 dp = LIST_NEXT(dp, d_list);
926 if (dp) {
927 ++dp->d_refs;
928 LIST_INSERT_AFTER(dp, marker, d_list);
930 lwkt_reltoken(&disklist_token);
931 return (dp);
935 * Terminate an enumeration early. Do not call this function if the
936 * enumeration ended normally. dp can be NULL, indicating that you
937 * wish to retain the ref count on dp.
939 * This function removes the marker.
941 void
942 disk_enumerate_stop(struct disk *marker, struct disk *dp)
944 lwkt_gettoken(&disklist_token);
945 LIST_REMOVE(marker, d_list);
946 if (dp)
947 --dp->d_refs;
948 lwkt_reltoken(&disklist_token);
951 static
953 sysctl_disks(SYSCTL_HANDLER_ARGS)
955 struct disk marker;
956 struct disk *dp;
957 int error, first;
959 first = 1;
960 error = 0;
961 dp = NULL;
963 while ((dp = disk_enumerate(&marker, dp))) {
964 if (!first) {
965 error = SYSCTL_OUT(req, " ", 1);
966 if (error) {
967 disk_enumerate_stop(&marker, dp);
968 break;
970 } else {
971 first = 0;
973 error = SYSCTL_OUT(req, dp->d_rawdev->si_name,
974 strlen(dp->d_rawdev->si_name));
975 if (error) {
976 disk_enumerate_stop(&marker, dp);
977 break;
980 if (error == 0)
981 error = SYSCTL_OUT(req, "", 1);
982 return error;
985 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
986 sysctl_disks, "A", "names of available disks");
989 * Open a disk device or partition.
991 static
993 diskopen(struct dev_open_args *ap)
995 cdev_t dev = ap->a_head.a_dev;
996 struct disk *dp;
997 int error;
1000 * dp can't be NULL here XXX.
1002 * d_slice will be NULL if setdiskinfo() has not been called yet.
1003 * setdiskinfo() is typically called whether the disk is present
1004 * or not (e.g. CD), but the base disk device is created first
1005 * and there may be a race.
1007 dp = dev->si_disk;
1008 if (dp == NULL || dp->d_slice == NULL)
1009 return (ENXIO);
1010 error = 0;
1013 * Deal with open races
1015 get_mplock();
1016 while (dp->d_flags & DISKFLAG_LOCK) {
1017 dp->d_flags |= DISKFLAG_WANTED;
1018 error = tsleep(dp, PCATCH, "diskopen", hz);
1019 if (error) {
1020 rel_mplock();
1021 return (error);
1024 dp->d_flags |= DISKFLAG_LOCK;
1027 * Open the underlying raw device.
1029 if (!dsisopen(dp->d_slice)) {
1030 #if 0
1031 if (!pdev->si_iosize_max)
1032 pdev->si_iosize_max = dev->si_iosize_max;
1033 #endif
1034 error = dev_dopen(dp->d_rawdev, ap->a_oflags,
1035 ap->a_devtype, ap->a_cred, NULL);
1038 if (error)
1039 goto out;
1040 error = dsopen(dev, ap->a_devtype, dp->d_info.d_dsflags,
1041 &dp->d_slice, &dp->d_info);
1042 if (!dsisopen(dp->d_slice)) {
1043 dev_dclose(dp->d_rawdev, ap->a_oflags, ap->a_devtype, NULL);
1045 out:
1046 dp->d_flags &= ~DISKFLAG_LOCK;
1047 if (dp->d_flags & DISKFLAG_WANTED) {
1048 dp->d_flags &= ~DISKFLAG_WANTED;
1049 wakeup(dp);
1051 rel_mplock();
1053 KKASSERT(dp->d_opencount >= 0);
1054 /* If the open was successful, bump open count */
1055 if (error == 0)
1056 atomic_add_int(&dp->d_opencount, 1);
1058 return(error);
1062 * Close a disk device or partition
1064 static
1066 diskclose(struct dev_close_args *ap)
1068 cdev_t dev = ap->a_head.a_dev;
1069 struct disk *dp;
1070 int error;
1071 int lcount;
1073 error = 0;
1074 dp = dev->si_disk;
1077 * The cdev_t represents the disk/slice/part. The shared
1078 * dp structure governs all cdevs associated with the disk.
1080 * As a safety only close the underlying raw device on the last
1081 * close the disk device if our tracking of the slices/partitions
1082 * also indicates nothing is open.
1084 KKASSERT(dp->d_opencount >= 1);
1085 lcount = atomic_fetchadd_int(&dp->d_opencount, -1);
1087 get_mplock();
1088 dsclose(dev, ap->a_devtype, dp->d_slice);
1089 if (lcount <= 1 && !dsisopen(dp->d_slice)) {
1090 error = dev_dclose(dp->d_rawdev, ap->a_fflag, ap->a_devtype, NULL);
1092 rel_mplock();
1093 return (error);
1097 * First execute the ioctl on the disk device, and if it isn't supported
1098 * try running it on the backing device.
1100 static
1102 diskioctl(struct dev_ioctl_args *ap)
1104 cdev_t dev = ap->a_head.a_dev;
1105 struct disk *dp;
1106 int error;
1107 u_int u;
1109 dp = dev->si_disk;
1110 if (dp == NULL)
1111 return (ENXIO);
1113 devfs_debug(DEVFS_DEBUG_DEBUG,
1114 "diskioctl: cmd is: %lx (name: %s)\n",
1115 ap->a_cmd, dev->si_name);
1116 devfs_debug(DEVFS_DEBUG_DEBUG,
1117 "diskioctl: &dp->d_slice is: %p, %p\n",
1118 &dp->d_slice, dp->d_slice);
1120 if (ap->a_cmd == DIOCGKERNELDUMP) {
1121 u = *(u_int *)ap->a_data;
1122 return disk_dumpconf(dev, u);
1125 if (ap->a_cmd == DIOCRECLUSTER && dev == dp->d_cdev) {
1126 error = disk_iocom_ioctl(dp, ap->a_cmd, ap->a_data);
1127 return error;
1130 if (&dp->d_slice == NULL || dp->d_slice == NULL ||
1131 ((dp->d_info.d_dsflags & DSO_DEVICEMAPPER) &&
1132 dkslice(dev) == WHOLE_DISK_SLICE)) {
1133 error = ENOIOCTL;
1134 } else {
1135 get_mplock();
1136 error = dsioctl(dev, ap->a_cmd, ap->a_data, ap->a_fflag,
1137 &dp->d_slice, &dp->d_info);
1138 rel_mplock();
1141 if (error == ENOIOCTL) {
1142 error = dev_dioctl(dp->d_rawdev, ap->a_cmd, ap->a_data,
1143 ap->a_fflag, ap->a_cred, NULL, NULL);
1145 return (error);
1149 * Execute strategy routine
1151 static
1153 diskstrategy(struct dev_strategy_args *ap)
1155 cdev_t dev = ap->a_head.a_dev;
1156 struct bio *bio = ap->a_bio;
1157 struct bio *nbio;
1158 struct disk *dp;
1160 dp = dev->si_disk;
1162 if (dp == NULL) {
1163 bio->bio_buf->b_error = ENXIO;
1164 bio->bio_buf->b_flags |= B_ERROR;
1165 biodone(bio);
1166 return(0);
1168 KKASSERT(dev->si_disk == dp);
1171 * The dscheck() function will also transform the slice relative
1172 * block number i.e. bio->bio_offset into a block number that can be
1173 * passed directly to the underlying raw device. If dscheck()
1174 * returns NULL it will have handled the bio for us (e.g. EOF
1175 * or error due to being beyond the device size).
1177 if ((nbio = dscheck(dev, bio, dp->d_slice)) != NULL) {
1178 dsched_queue(dp, nbio);
1179 } else {
1180 biodone(bio);
1182 return(0);
1186 * Return the partition size in ?blocks?
1188 static
1190 diskpsize(struct dev_psize_args *ap)
1192 cdev_t dev = ap->a_head.a_dev;
1193 struct disk *dp;
1195 dp = dev->si_disk;
1196 if (dp == NULL)
1197 return(ENODEV);
1199 ap->a_result = dssize(dev, &dp->d_slice);
1201 if ((ap->a_result == -1) &&
1202 (dp->d_info.d_dsflags & DSO_RAWPSIZE)) {
1203 ap->a_head.a_dev = dp->d_rawdev;
1204 return dev_doperate(&ap->a_head);
1206 return(0);
1209 static int
1210 diskdump(struct dev_dump_args *ap)
1212 cdev_t dev = ap->a_head.a_dev;
1213 struct disk *dp = dev->si_disk;
1214 u_int64_t size, offset;
1215 int error;
1217 error = disk_dumpcheck(dev, &size, &ap->a_blkno, &ap->a_secsize);
1218 /* XXX: this should probably go in disk_dumpcheck somehow */
1219 if (ap->a_length != 0) {
1220 size *= DEV_BSIZE;
1221 offset = ap->a_blkno * DEV_BSIZE;
1222 if ((ap->a_offset < offset) ||
1223 (ap->a_offset + ap->a_length - offset > size)) {
1224 kprintf("Attempt to write outside dump "
1225 "device boundaries.\n");
1226 error = ENOSPC;
1230 if (error == 0) {
1231 ap->a_head.a_dev = dp->d_rawdev;
1232 error = dev_doperate(&ap->a_head);
1235 return(error);
1239 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
1240 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
1242 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
1243 0, sizeof(struct disk), "sizeof(struct disk)");
1246 * Reorder interval for burst write allowance and minor write
1247 * allowance.
1249 * We always want to trickle some writes in to make use of the
1250 * disk's zone cache. Bursting occurs on a longer interval and only
1251 * runningbufspace is well over the hirunningspace limit.
1253 int bioq_reorder_burst_interval = 60; /* should be multiple of minor */
1254 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_burst_interval,
1255 CTLFLAG_RW, &bioq_reorder_burst_interval, 0, "");
1256 int bioq_reorder_minor_interval = 5;
1257 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_minor_interval,
1258 CTLFLAG_RW, &bioq_reorder_minor_interval, 0, "");
1260 int bioq_reorder_burst_bytes = 3000000;
1261 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_burst_bytes,
1262 CTLFLAG_RW, &bioq_reorder_burst_bytes, 0, "");
1263 int bioq_reorder_minor_bytes = 262144;
1264 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_minor_bytes,
1265 CTLFLAG_RW, &bioq_reorder_minor_bytes, 0, "");
1269 * Order I/Os. Generally speaking this code is designed to make better
1270 * use of drive zone caches. A drive zone cache can typically track linear
1271 * reads or writes for around 16 zones simultaniously.
1273 * Read prioritization issues: It is possible for hundreds of megabytes worth
1274 * of writes to be queued asynchronously. This creates a huge bottleneck
1275 * for reads which reduce read bandwidth to a trickle.
1277 * To solve this problem we generally reorder reads before writes.
1279 * However, a large number of random reads can also starve writes and
1280 * make poor use of the drive zone cache so we allow writes to trickle
1281 * in every N reads.
1283 void
1284 bioqdisksort(struct bio_queue_head *bioq, struct bio *bio)
1287 * The BIO wants to be ordered. Adding to the tail also
1288 * causes transition to be set to NULL, forcing the ordering
1289 * of all prior I/O's.
1291 if (bio->bio_buf->b_flags & B_ORDERED) {
1292 bioq_insert_tail(bioq, bio);
1293 return;
1296 switch(bio->bio_buf->b_cmd) {
1297 case BUF_CMD_READ:
1298 if (bioq->transition) {
1300 * Insert before the first write. Bleedover writes
1301 * based on reorder intervals to prevent starvation.
1303 TAILQ_INSERT_BEFORE(bioq->transition, bio, bio_act);
1304 ++bioq->reorder;
1305 if (bioq->reorder % bioq_reorder_minor_interval == 0) {
1306 bioqwritereorder(bioq);
1307 if (bioq->reorder >=
1308 bioq_reorder_burst_interval) {
1309 bioq->reorder = 0;
1312 } else {
1314 * No writes queued (or ordering was forced),
1315 * insert at tail.
1317 TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act);
1319 break;
1320 case BUF_CMD_WRITE:
1322 * Writes are always appended. If no writes were previously
1323 * queued or an ordered tail insertion occured the transition
1324 * field will be NULL.
1326 TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act);
1327 if (bioq->transition == NULL)
1328 bioq->transition = bio;
1329 break;
1330 default:
1332 * All other request types are forced to be ordered.
1334 bioq_insert_tail(bioq, bio);
1335 break;
1340 * Move the read-write transition point to prevent reads from
1341 * completely starving our writes. This brings a number of writes into
1342 * the fold every N reads.
1344 * We bring a few linear writes into the fold on a minor interval
1345 * and we bring a non-linear burst of writes into the fold on a major
1346 * interval. Bursting only occurs if runningbufspace is really high
1347 * (typically from syncs, fsyncs, or HAMMER flushes).
1349 static
1350 void
1351 bioqwritereorder(struct bio_queue_head *bioq)
1353 struct bio *bio;
1354 off_t next_offset;
1355 size_t left;
1356 size_t n;
1357 int check_off;
1359 if (bioq->reorder < bioq_reorder_burst_interval ||
1360 !buf_runningbufspace_severe()) {
1361 left = (size_t)bioq_reorder_minor_bytes;
1362 check_off = 1;
1363 } else {
1364 left = (size_t)bioq_reorder_burst_bytes;
1365 check_off = 0;
1368 next_offset = bioq->transition->bio_offset;
1369 while ((bio = bioq->transition) != NULL &&
1370 (check_off == 0 || next_offset == bio->bio_offset)
1372 n = bio->bio_buf->b_bcount;
1373 next_offset = bio->bio_offset + n;
1374 bioq->transition = TAILQ_NEXT(bio, bio_act);
1375 if (left < n)
1376 break;
1377 left -= n;
1382 * Bounds checking against the media size, used for the raw partition.
1383 * secsize, mediasize and b_blkno must all be the same units.
1384 * Possibly this has to be DEV_BSIZE (512).
1387 bounds_check_with_mediasize(struct bio *bio, int secsize, uint64_t mediasize)
1389 struct buf *bp = bio->bio_buf;
1390 int64_t sz;
1392 sz = howmany(bp->b_bcount, secsize);
1394 if (bio->bio_offset/DEV_BSIZE + sz > mediasize) {
1395 sz = mediasize - bio->bio_offset/DEV_BSIZE;
1396 if (sz == 0) {
1397 /* If exactly at end of disk, return EOF. */
1398 bp->b_resid = bp->b_bcount;
1399 return 0;
1401 if (sz < 0) {
1402 /* If past end of disk, return EINVAL. */
1403 bp->b_error = EINVAL;
1404 return 0;
1406 /* Otherwise, truncate request. */
1407 bp->b_bcount = sz * secsize;
1410 return 1;
1414 * Disk error is the preface to plaintive error messages
1415 * about failing disk transfers. It prints messages of the form
1417 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
1419 * if the offset of the error in the transfer and a disk label
1420 * are both available. blkdone should be -1 if the position of the error
1421 * is unknown; the disklabel pointer may be null from drivers that have not
1422 * been converted to use them. The message is printed with kprintf
1423 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
1424 * The message should be completed (with at least a newline) with kprintf
1425 * or log(-1, ...), respectively. There is no trailing space.
1427 void
1428 diskerr(struct bio *bio, cdev_t dev, const char *what, int pri, int donecnt)
1430 struct buf *bp = bio->bio_buf;
1431 const char *term;
1433 switch(bp->b_cmd) {
1434 case BUF_CMD_READ:
1435 term = "read";
1436 break;
1437 case BUF_CMD_WRITE:
1438 term = "write";
1439 break;
1440 default:
1441 term = "access";
1442 break;
1444 kprintf("%s: %s %sing ", dev->si_name, what, term);
1445 kprintf("offset %012llx for %d",
1446 (long long)bio->bio_offset,
1447 bp->b_bcount);
1449 if (donecnt)
1450 kprintf(" (%d bytes completed)", donecnt);
1454 * Locate a disk device
1456 cdev_t
1457 disk_locate(const char *devname)
1459 return devfs_find_device_by_name("%s", devname);
1462 void
1463 disk_config(void *arg)
1465 disk_msg_send_sync(DISK_SYNC, NULL, NULL);
1468 static void
1469 disk_init(void)
1471 struct thread* td_core;
1473 disk_msg_cache = objcache_create("disk-msg-cache", 0, 0,
1474 NULL, NULL, NULL,
1475 objcache_malloc_alloc,
1476 objcache_malloc_free,
1477 &disk_msg_malloc_args);
1479 lwkt_token_init(&disklist_token, "disks");
1482 * Initialize the reply-only port which acts as a message drain
1484 lwkt_initport_replyonly(&disk_dispose_port, disk_msg_autofree_reply);
1486 lwkt_gettoken(&disklist_token);
1487 lwkt_create(disk_msg_core, /*args*/NULL, &td_core, NULL,
1488 0, -1, "disk_msg_core");
1489 tsleep(td_core, 0, "diskcore", 0);
1490 lwkt_reltoken(&disklist_token);
1493 static void
1494 disk_uninit(void)
1496 objcache_destroy(disk_msg_cache);
1500 * Clean out illegal characters in serial numbers.
1502 static void
1503 disk_cleanserial(char *serno)
1505 char c;
1507 while ((c = *serno) != 0) {
1508 if (c >= 'a' && c <= 'z')
1510 else if (c >= 'A' && c <= 'Z')
1512 else if (c >= '0' && c <= '9')
1514 else if (c == '-' || c == '@' || c == '+' || c == '.')
1516 else
1517 c = '_';
1518 *serno++= c;
1522 TUNABLE_INT("kern.disk_debug", &disk_debug_enable);
1523 SYSCTL_INT(_kern, OID_AUTO, disk_debug, CTLFLAG_RW, &disk_debug_enable,
1524 0, "Enable subr_disk debugging");
1526 SYSINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, disk_init, NULL);
1527 SYSUNINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, disk_uninit, NULL);