dntpd(8): client_main() never returns, so do not pretend it does.
[dragonfly.git] / sys / kern / subr_disk.c
blob44ce3eb877454df327d95fdedc5650ba496d0b9c
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/msgport2.h>
109 #include <sys/thread2.h>
111 static MALLOC_DEFINE(M_DISK, "disk", "disk data");
112 static int disk_debug_enable = 0;
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, int reprobe);
117 static void disk_probe(struct disk *dp, int reprobe);
118 static void _setdiskinfo(struct disk *disk, struct disk_info *info);
119 static void bioqwritereorder(struct bio_queue_head *bioq);
120 static void disk_cleanserial(char *serno);
121 static int disk_debug(int, char *, ...) __printflike(2, 3);
122 static cdev_t _disk_create_named(const char *name, int unit, struct disk *dp,
123 struct dev_ops *raw_ops, int clone);
125 static d_open_t diskopen;
126 static d_close_t diskclose;
127 static d_ioctl_t diskioctl;
128 static d_strategy_t diskstrategy;
129 static d_psize_t diskpsize;
130 static d_dump_t diskdump;
132 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
133 static struct lwkt_token disklist_token;
134 static struct lwkt_token ds_token;
136 static struct dev_ops disk1_ops = {
137 { "disk", 0, D_DISK | D_MPSAFE | D_TRACKCLOSE | D_KVABIO },
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 dev_ops disk2_ops = {
149 { "disk", 0, D_DISK | D_MPSAFE | D_TRACKCLOSE | D_KVABIO |
150 D_NOEMERGPGR },
151 .d_open = diskopen,
152 .d_close = diskclose,
153 .d_read = physread,
154 .d_write = physwrite,
155 .d_ioctl = diskioctl,
156 .d_strategy = diskstrategy,
157 .d_dump = diskdump,
158 .d_psize = diskpsize,
161 static struct objcache *disk_msg_cache;
163 struct objcache_malloc_args disk_msg_malloc_args = {
164 sizeof(struct disk_msg), M_DISK };
166 static struct lwkt_port disk_dispose_port;
167 static struct lwkt_port disk_msg_port;
169 static int
170 disk_debug(int level, char *fmt, ...)
172 __va_list ap;
174 __va_start(ap, fmt);
175 if (level <= disk_debug_enable)
176 kvprintf(fmt, ap);
177 __va_end(ap);
179 return 0;
182 static int
183 disk_probe_slice(struct disk *dp, cdev_t dev, int slice, int reprobe)
185 struct disk_info *info = &dp->d_info;
186 struct diskslice *sp = &dp->d_slice->dss_slices[slice];
187 disklabel_ops_t ops;
188 struct dev_ops *dops;
189 struct partinfo part;
190 const char *msg;
191 char uuid_buf[128];
192 cdev_t ndev;
193 int sno;
194 u_int i;
196 disk_debug(2, "disk_probe_slice (begin): %s (%s)\n",
197 dev->si_name, dp->d_cdev->si_name);
199 sno = slice ? slice - 1 : 0;
200 dops = (dp->d_rawdev->si_ops->head.flags & D_NOEMERGPGR) ?
201 &disk2_ops : &disk1_ops;
203 ops = &disklabel32_ops;
204 msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info);
205 if (msg && !strcmp(msg, "no disk label")) {
206 ops = &disklabel64_ops;
207 msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info);
210 if (msg == NULL) {
211 if (slice != WHOLE_DISK_SLICE)
212 ops->op_adjust_label_reserved(dp->d_slice, slice, sp);
213 else
214 sp->ds_reserved = 0;
216 sp->ds_ops = ops;
217 for (i = 0; i < ops->op_getnumparts(sp->ds_label); i++) {
218 ops->op_loadpartinfo(sp->ds_label, i, &part);
219 if (part.fstype) {
220 if (reprobe &&
221 (ndev = devfs_find_device_by_name("%s%c",
222 dev->si_name, 'a' + i))
225 * Device already exists and
226 * is still valid.
228 ndev->si_flags |= SI_REPROBE_TEST;
231 * Destroy old UUID alias
233 destroy_dev_alias(ndev, "part-by-uuid/*");
235 /* Create UUID alias */
236 if (!kuuid_is_nil(&part.storage_uuid)) {
237 snprintf_uuid(uuid_buf,
238 sizeof(uuid_buf),
239 &part.storage_uuid);
240 make_dev_alias(ndev,
241 "part-by-uuid/%s",
242 uuid_buf);
243 udev_dict_set_cstr(ndev, "uuid", uuid_buf);
245 } else {
246 ndev = make_dev_covering(dops,
247 dp->d_rawdev->si_ops,
248 dkmakeminor(dkunit(dp->d_cdev),
249 slice, i),
250 UID_ROOT, GID_OPERATOR, 0640,
251 "%s%c", dev->si_name, 'a'+ i);
252 ndev->si_parent = dev;
253 ndev->si_iosize_max = dev->si_iosize_max;
254 ndev->si_disk = dp;
255 udev_dict_set_cstr(ndev, "subsystem", "disk");
256 /* Inherit parent's disk type */
257 if (dp->d_disktype) {
258 udev_dict_set_cstr(ndev, "disk-type",
259 __DECONST(char *, dp->d_disktype));
262 /* Create serno alias */
263 if (dp->d_info.d_serialno) {
264 make_dev_alias(ndev,
265 "serno/%s.s%d%c",
266 dp->d_info.d_serialno,
267 sno, 'a' + i);
270 /* Create UUID alias */
271 if (!kuuid_is_nil(&part.storage_uuid)) {
272 snprintf_uuid(uuid_buf,
273 sizeof(uuid_buf),
274 &part.storage_uuid);
275 make_dev_alias(ndev,
276 "part-by-uuid/%s",
277 uuid_buf);
278 udev_dict_set_cstr(ndev, "uuid", uuid_buf);
280 ndev->si_flags |= SI_REPROBE_TEST;
284 } else if (info->d_dsflags & DSO_COMPATLABEL) {
285 msg = NULL;
286 if (sp->ds_size >= 0x100000000ULL)
287 ops = &disklabel64_ops;
288 else
289 ops = &disklabel32_ops;
290 sp->ds_label = ops->op_clone_label(info, sp);
291 } else {
292 if (sp->ds_type == DOSPTYP_386BSD || /* XXX */
293 sp->ds_type == DOSPTYP_NETBSD ||
294 sp->ds_type == DOSPTYP_OPENBSD ||
295 sp->ds_type == DOSPTYP_DFLYBSD) {
296 log(LOG_WARNING, "%s: cannot find label (%s)\n",
297 dev->si_name, msg);
300 if (sp->ds_label.opaque != NULL && sp->ds_ops != NULL) {
301 /* Clear out old label - it's not around anymore */
302 disk_debug(2,
303 "disk_probe_slice: clear out old diskabel on %s\n",
304 dev->si_name);
306 sp->ds_ops->op_freedisklabel(&sp->ds_label);
307 sp->ds_ops = NULL;
311 if (msg == NULL) {
312 sp->ds_wlabel = FALSE;
315 return (msg ? EINVAL : 0);
319 * This routine is only called for newly minted drives or to reprobe
320 * a drive with no open slices. disk_probe_slice() is called directly
321 * when reprobing partition changes within slices.
323 static void
324 disk_probe(struct disk *dp, int reprobe)
326 struct disk_info *info = &dp->d_info;
327 cdev_t dev = dp->d_cdev;
328 cdev_t ndev;
329 int error, i, sno;
330 struct diskslices *osp;
331 struct diskslice *sp;
332 struct dev_ops *dops;
333 char uuid_buf[128];
335 KKASSERT (info->d_media_blksize != 0);
337 osp = dp->d_slice;
338 dp->d_slice = dsmakeslicestruct(BASE_SLICE, info);
339 disk_debug(1, "disk_probe (begin): %s\n", dp->d_cdev->si_name);
341 error = mbrinit(dev, info, &(dp->d_slice));
342 if (error) {
343 dsgone(&osp);
344 return;
347 dops = (dp->d_rawdev->si_ops->head.flags & D_NOEMERGPGR) ?
348 &disk2_ops : &disk1_ops;
350 for (i = 0; i < dp->d_slice->dss_nslices; i++) {
352 * Ignore the whole-disk slice, it has already been created.
354 if (i == WHOLE_DISK_SLICE)
355 continue;
357 #if 1
359 * Ignore the compatibility slice s0 if it's a device mapper
360 * volume.
362 if ((i == COMPATIBILITY_SLICE) &&
363 (info->d_dsflags & DSO_DEVICEMAPPER))
364 continue;
365 #endif
367 sp = &dp->d_slice->dss_slices[i];
370 * Handle s0. s0 is a compatibility slice if there are no
371 * other slices and it has not otherwise been set up, else
372 * we ignore it.
374 if (i == COMPATIBILITY_SLICE) {
375 sno = 0;
376 if (sp->ds_type == 0 &&
377 dp->d_slice->dss_nslices == BASE_SLICE) {
378 sp->ds_size = info->d_media_blocks;
379 sp->ds_reserved = 0;
381 } else {
382 sno = i - 1;
383 sp->ds_reserved = 0;
387 * Ignore 0-length slices
389 if (sp->ds_size == 0)
390 continue;
392 if (reprobe &&
393 (ndev = devfs_find_device_by_name("%ss%d",
394 dev->si_name, sno))) {
396 * Device already exists and is still valid
398 ndev->si_flags |= SI_REPROBE_TEST;
401 * Destroy old UUID alias
403 destroy_dev_alias(ndev, "slice-by-uuid/*");
405 /* Create UUID alias */
406 if (!kuuid_is_nil(&sp->ds_stor_uuid)) {
407 snprintf_uuid(uuid_buf, sizeof(uuid_buf),
408 &sp->ds_stor_uuid);
409 make_dev_alias(ndev, "slice-by-uuid/%s",
410 uuid_buf);
412 } else {
414 * Else create new device
416 ndev = make_dev_covering(dops, dp->d_rawdev->si_ops,
417 dkmakewholeslice(dkunit(dev), i),
418 UID_ROOT, GID_OPERATOR, 0640,
419 (info->d_dsflags & DSO_DEVICEMAPPER)?
420 "%s.s%d" : "%ss%d", dev->si_name, sno);
421 ndev->si_parent = dev;
422 ndev->si_iosize_max = dev->si_iosize_max;
423 udev_dict_set_cstr(ndev, "subsystem", "disk");
424 /* Inherit parent's disk type */
425 if (dp->d_disktype) {
426 udev_dict_set_cstr(ndev, "disk-type",
427 __DECONST(char *, dp->d_disktype));
430 /* Create serno alias */
431 if (dp->d_info.d_serialno) {
432 make_dev_alias(ndev, "serno/%s.s%d",
433 dp->d_info.d_serialno, sno);
436 /* Create UUID alias */
437 if (!kuuid_is_nil(&sp->ds_stor_uuid)) {
438 snprintf_uuid(uuid_buf, sizeof(uuid_buf),
439 &sp->ds_stor_uuid);
440 make_dev_alias(ndev, "slice-by-uuid/%s",
441 uuid_buf);
444 ndev->si_disk = dp;
445 ndev->si_flags |= SI_REPROBE_TEST;
447 sp->ds_dev = ndev;
450 * Probe appropriate slices for a disklabel
452 * XXX slice type 1 used by our gpt probe code.
453 * XXX slice type 0 used by mbr compat slice.
455 if (sp->ds_type == DOSPTYP_386BSD ||
456 sp->ds_type == DOSPTYP_NETBSD ||
457 sp->ds_type == DOSPTYP_OPENBSD ||
458 sp->ds_type == DOSPTYP_DFLYBSD ||
459 sp->ds_type == 0 ||
460 sp->ds_type == 1) {
461 if (dp->d_slice->dss_first_bsd_slice == 0)
462 dp->d_slice->dss_first_bsd_slice = i;
463 disk_probe_slice(dp, ndev, i, reprobe);
466 dsgone(&osp);
467 disk_debug(1, "disk_probe (end): %s\n", dp->d_cdev->si_name);
471 static void
472 disk_msg_core(void *arg)
474 struct disk *dp;
475 struct diskslice *sp;
476 disk_msg_t msg;
477 int run;
479 lwkt_gettoken(&disklist_token);
480 lwkt_initport_thread(&disk_msg_port, curthread);
481 wakeup(curthread); /* synchronous startup */
482 lwkt_reltoken(&disklist_token);
484 lwkt_gettoken(&ds_token);
485 run = 1;
487 while (run) {
488 msg = (disk_msg_t)lwkt_waitport(&disk_msg_port, 0);
490 switch (msg->hdr.u.ms_result) {
491 case DISK_DISK_PROBE:
492 dp = (struct disk *)msg->load;
493 disk_debug(1,
494 "DISK_DISK_PROBE: %s\n",
495 dp->d_cdev->si_name);
496 disk_iocom_update(dp);
497 disk_probe(dp, 0);
498 break;
499 case DISK_DISK_DESTROY:
500 dp = (struct disk *)msg->load;
501 disk_debug(1,
502 "DISK_DISK_DESTROY: %s\n",
503 dp->d_cdev->si_name);
504 disk_iocom_uninit(dp);
507 * Interlock against struct disk enumerations.
508 * Wait for enumerations to complete then remove
509 * the dp from the list before tearing it down.
510 * This avoids numerous races.
512 lwkt_gettoken(&disklist_token);
513 while (dp->d_refs)
514 tsleep(&dp->d_refs, 0, "diskdel", hz / 10);
515 LIST_REMOVE(dp, d_list);
517 dsched_disk_destroy(dp);
518 devfs_destroy_related(dp->d_cdev);
519 destroy_dev(dp->d_cdev);
520 destroy_only_dev(dp->d_rawdev);
522 lwkt_reltoken(&disklist_token);
524 if (dp->d_info.d_serialno) {
525 kfree(dp->d_info.d_serialno, M_TEMP);
526 dp->d_info.d_serialno = NULL;
528 break;
529 case DISK_UNPROBE:
530 dp = (struct disk *)msg->load;
531 disk_debug(1,
532 "DISK_DISK_UNPROBE: %s\n",
533 dp->d_cdev->si_name);
534 devfs_destroy_related(dp->d_cdev);
535 break;
536 case DISK_SLICE_REPROBE:
537 dp = (struct disk *)msg->load;
538 sp = (struct diskslice *)msg->load2;
539 devfs_clr_related_flag(sp->ds_dev,
540 SI_REPROBE_TEST);
541 disk_debug(1,
542 "DISK_SLICE_REPROBE: %s\n",
543 sp->ds_dev->si_name);
544 disk_probe_slice(dp, sp->ds_dev,
545 dkslice(sp->ds_dev), 1);
546 devfs_destroy_related_without_flag(
547 sp->ds_dev, SI_REPROBE_TEST);
548 break;
549 case DISK_DISK_REPROBE:
550 dp = (struct disk *)msg->load;
551 devfs_clr_related_flag(dp->d_cdev, SI_REPROBE_TEST);
552 disk_debug(1,
553 "DISK_DISK_REPROBE: %s\n",
554 dp->d_cdev->si_name);
555 disk_probe(dp, 1);
556 devfs_destroy_related_without_flag(
557 dp->d_cdev, SI_REPROBE_TEST);
558 break;
559 case DISK_SYNC:
560 disk_debug(1, "DISK_SYNC\n");
561 break;
562 default:
563 devfs_debug(DEVFS_DEBUG_WARNING,
564 "disk_msg_core: unknown message "
565 "received at core\n");
566 break;
568 lwkt_replymsg(&msg->hdr, 0);
570 lwkt_reltoken(&ds_token);
571 lwkt_exit();
576 * Acts as a message drain. Any message that is replied to here gets
577 * destroyed and the memory freed.
579 static void
580 disk_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
582 objcache_put(disk_msg_cache, msg);
586 void
587 disk_msg_send(uint32_t cmd, void *load, void *load2)
589 disk_msg_t disk_msg;
590 lwkt_port_t port = &disk_msg_port;
592 disk_msg = objcache_get(disk_msg_cache, M_WAITOK);
594 lwkt_initmsg(&disk_msg->hdr, &disk_dispose_port, 0);
596 disk_msg->hdr.u.ms_result = cmd;
597 disk_msg->load = load;
598 disk_msg->load2 = load2;
599 KKASSERT(port);
600 lwkt_sendmsg(port, &disk_msg->hdr);
603 void
604 disk_msg_send_sync(uint32_t cmd, void *load, void *load2)
606 struct lwkt_port rep_port;
607 disk_msg_t disk_msg;
608 lwkt_port_t port;
610 disk_msg = objcache_get(disk_msg_cache, M_WAITOK);
611 port = &disk_msg_port;
613 /* XXX could probably use curthread's built-in msgport */
614 lwkt_initport_thread(&rep_port, curthread);
615 lwkt_initmsg(&disk_msg->hdr, &rep_port, 0);
617 disk_msg->hdr.u.ms_result = cmd;
618 disk_msg->load = load;
619 disk_msg->load2 = load2;
621 lwkt_domsg(port, &disk_msg->hdr, 0);
622 objcache_put(disk_msg_cache, disk_msg);
626 * Create a raw device for the dev_ops template (which is returned). Also
627 * create a slice and unit managed disk and overload the user visible
628 * device space with it.
630 * NOTE: The returned raw device is NOT a slice and unit managed device.
631 * It is an actual raw device representing the raw disk as specified by
632 * the passed dev_ops. The disk layer not only returns such a raw device,
633 * it also uses it internally when passing (modified) commands through.
635 cdev_t
636 disk_create(int unit, struct disk *dp, struct dev_ops *raw_ops)
638 return _disk_create_named(NULL, unit, dp, raw_ops, 0);
641 cdev_t
642 disk_create_clone(int unit, struct disk *dp,
643 struct dev_ops *raw_ops)
645 return _disk_create_named(NULL, unit, dp, raw_ops, 1);
648 cdev_t
649 disk_create_named(const char *name, int unit, struct disk *dp,
650 struct dev_ops *raw_ops)
652 return _disk_create_named(name, unit, dp, raw_ops, 0);
655 cdev_t
656 disk_create_named_clone(const char *name, int unit, struct disk *dp,
657 struct dev_ops *raw_ops)
659 return _disk_create_named(name, unit, dp, raw_ops, 1);
662 static cdev_t
663 _disk_create_named(const char *name, int unit, struct disk *dp,
664 struct dev_ops *raw_ops, int clone)
666 cdev_t rawdev;
667 struct dev_ops *dops;
669 disk_debug(1, "disk_create (begin): %s%d\n", name, unit);
671 if (name) {
672 rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit),
673 UID_ROOT, GID_OPERATOR, 0640, "%s", name);
674 } else {
675 rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit),
676 UID_ROOT, GID_OPERATOR, 0640,
677 "%s%d", raw_ops->head.name, unit);
680 bzero(dp, sizeof(*dp));
682 dops = (raw_ops->head.flags & D_NOEMERGPGR) ? &disk2_ops : &disk1_ops;
684 dp->d_rawdev = rawdev;
685 dp->d_raw_ops = raw_ops;
686 dp->d_dev_ops = dops;
688 if (name) {
689 if (clone) {
690 dp->d_cdev = make_only_dev_covering(
691 dops, dp->d_rawdev->si_ops,
692 dkmakewholedisk(unit),
693 UID_ROOT, GID_OPERATOR, 0640,
694 "%s", name);
695 } else {
696 dp->d_cdev = make_dev_covering(
697 dops, dp->d_rawdev->si_ops,
698 dkmakewholedisk(unit),
699 UID_ROOT, GID_OPERATOR, 0640,
700 "%s", name);
702 } else {
703 if (clone) {
704 dp->d_cdev = make_only_dev_covering(
705 dops, dp->d_rawdev->si_ops,
706 dkmakewholedisk(unit),
707 UID_ROOT, GID_OPERATOR, 0640,
708 "%s%d", raw_ops->head.name, unit);
709 } else {
710 dp->d_cdev = make_dev_covering(
711 dops, dp->d_rawdev->si_ops,
712 dkmakewholedisk(unit),
713 UID_ROOT, GID_OPERATOR, 0640,
714 "%s%d", raw_ops->head.name, unit);
718 udev_dict_set_cstr(dp->d_cdev, "subsystem", "disk");
719 dp->d_cdev->si_disk = dp;
721 if (name)
722 dsched_disk_create(dp, name, unit);
723 else
724 dsched_disk_create(dp, raw_ops->head.name, unit);
726 lwkt_gettoken(&disklist_token);
727 LIST_INSERT_HEAD(&disklist, dp, d_list);
728 lwkt_reltoken(&disklist_token);
730 disk_iocom_init(dp);
732 disk_debug(1, "disk_create (end): %s%d\n",
733 (name != NULL)?(name):(raw_ops->head.name), unit);
735 return (dp->d_rawdev);
739 disk_setdisktype(struct disk *disk, const char *type)
741 int error;
743 KKASSERT(disk != NULL);
745 disk->d_disktype = type;
746 error = udev_dict_set_cstr(disk->d_cdev, "disk-type",
747 __DECONST(char *, type));
748 return error;
752 disk_getopencount(struct disk *disk)
754 return disk->d_opencount;
757 static void
758 _setdiskinfo(struct disk *disk, struct disk_info *info)
760 char *oldserialno;
762 oldserialno = disk->d_info.d_serialno;
763 bcopy(info, &disk->d_info, sizeof(disk->d_info));
764 info = &disk->d_info;
766 disk_debug(1, "_setdiskinfo: %s\n", disk->d_cdev->si_name);
769 * The serial number is duplicated so the caller can throw
770 * their copy away.
772 if (info->d_serialno && info->d_serialno[0] &&
773 (info->d_serialno[0] != ' ' || strlen(info->d_serialno) > 1)) {
774 info->d_serialno = kstrdup(info->d_serialno, M_TEMP);
775 disk_cleanserial(info->d_serialno);
776 if (disk->d_cdev) {
777 make_dev_alias(disk->d_cdev, "serno/%s",
778 info->d_serialno);
780 } else {
781 info->d_serialno = NULL;
783 if (oldserialno)
784 kfree(oldserialno, M_TEMP);
786 dsched_disk_update(disk, info);
789 * The caller may set d_media_size or d_media_blocks and we
790 * calculate the other.
792 KKASSERT(info->d_media_size == 0 || info->d_media_blocks == 0);
793 if (info->d_media_size == 0 && info->d_media_blocks) {
794 info->d_media_size = (u_int64_t)info->d_media_blocks *
795 info->d_media_blksize;
796 } else if (info->d_media_size && info->d_media_blocks == 0 &&
797 info->d_media_blksize) {
798 info->d_media_blocks = info->d_media_size /
799 info->d_media_blksize;
803 * The si_* fields for rawdev are not set until after the
804 * disk_create() call, so someone using the cooked version
805 * of the raw device (i.e. da0s0) will not get the right
806 * si_iosize_max unless we fix it up here.
808 if (disk->d_cdev && disk->d_rawdev &&
809 disk->d_cdev->si_iosize_max == 0) {
810 disk->d_cdev->si_iosize_max = disk->d_rawdev->si_iosize_max;
811 disk->d_cdev->si_bsize_phys = disk->d_rawdev->si_bsize_phys;
812 disk->d_cdev->si_bsize_best = disk->d_rawdev->si_bsize_best;
815 /* Add the serial number to the udev_dictionary */
816 if (info->d_serialno)
817 udev_dict_set_cstr(disk->d_cdev, "serno", info->d_serialno);
821 * Disk drivers must call this routine when media parameters are available
822 * or have changed.
824 void
825 disk_setdiskinfo(struct disk *disk, struct disk_info *info)
827 _setdiskinfo(disk, info);
828 disk_msg_send(DISK_DISK_PROBE, disk, NULL);
829 disk_debug(1, "disk_setdiskinfo: sent probe for %s\n",
830 disk->d_cdev->si_name);
833 void
834 disk_setdiskinfo_sync(struct disk *disk, struct disk_info *info)
836 _setdiskinfo(disk, info);
837 disk_msg_send_sync(DISK_DISK_PROBE, disk, NULL);
838 disk_debug(1, "disk_setdiskinfo_sync: sent probe for %s\n",
839 disk->d_cdev->si_name);
843 * This routine is called when an adapter detaches. The higher level
844 * managed disk device is destroyed while the lower level raw device is
845 * released.
847 void
848 disk_destroy(struct disk *disk)
850 disk_msg_send_sync(DISK_DISK_DESTROY, disk, NULL);
851 return;
855 disk_dumpcheck(cdev_t dev, u_int64_t *size,
856 u_int64_t *blkno, u_int32_t *secsize)
858 struct partinfo pinfo;
859 int error;
861 if (size)
862 *size = 0; /* avoid gcc warnings */
863 if (secsize)
864 *secsize = 512; /* avoid gcc warnings */
865 bzero(&pinfo, sizeof(pinfo));
867 error = dev_dioctl(dev, DIOCGPART, (void *)&pinfo, 0,
868 proc0.p_ucred, NULL, NULL);
869 if (error)
870 return (error);
872 if (pinfo.media_blksize == 0)
873 return (ENXIO);
875 if (blkno) /* XXX: make sure this reserved stuff is right */
876 *blkno = pinfo.reserved_blocks +
877 pinfo.media_offset / pinfo.media_blksize;
878 if (secsize)
879 *secsize = pinfo.media_blksize;
880 if (size)
881 *size = (pinfo.media_blocks - pinfo.reserved_blocks);
883 return (0);
887 disk_dumpconf(cdev_t dev, u_int onoff)
889 struct dumperinfo di;
890 u_int64_t size, blkno;
891 u_int32_t secsize;
892 int error;
894 if (!onoff)
895 return set_dumper(NULL);
897 error = disk_dumpcheck(dev, &size, &blkno, &secsize);
899 if (error)
900 return ENXIO;
902 bzero(&di, sizeof(struct dumperinfo));
903 di.dumper = diskdump;
904 di.priv = dev;
905 di.blocksize = secsize;
906 di.maxiosize = dev->si_iosize_max;
907 di.mediaoffset = blkno * DEV_BSIZE;
908 di.mediasize = size * DEV_BSIZE;
910 return set_dumper(&di);
913 void
914 disk_unprobe(struct disk *disk)
916 if (disk == NULL)
917 return;
919 disk_msg_send_sync(DISK_UNPROBE, disk, NULL);
922 void
923 disk_invalidate (struct disk *disk)
925 dsgone(&disk->d_slice);
929 * Enumerate disks, pass a marker and an initial NULL dp to initialize,
930 * then loop with the previously returned dp.
932 * The returned dp will be referenced, preventing its destruction. When
933 * you pass the returned dp back into the loop the ref is dropped.
935 * WARNING: If terminating your loop early you must call
936 * disk_enumerate_stop().
938 struct disk *
939 disk_enumerate(struct disk *marker, struct disk *dp)
941 lwkt_gettoken(&disklist_token);
942 if (dp) {
943 --dp->d_refs;
944 dp = LIST_NEXT(marker, d_list);
945 LIST_REMOVE(marker, d_list);
946 } else {
947 bzero(marker, sizeof(*marker));
948 marker->d_flags = DISKFLAG_MARKER;
949 dp = LIST_FIRST(&disklist);
951 while (dp) {
952 if ((dp->d_flags & DISKFLAG_MARKER) == 0)
953 break;
954 dp = LIST_NEXT(dp, d_list);
956 if (dp) {
957 ++dp->d_refs;
958 LIST_INSERT_AFTER(dp, marker, d_list);
960 lwkt_reltoken(&disklist_token);
961 return (dp);
965 * Terminate an enumeration early. Do not call this function if the
966 * enumeration ended normally. dp can be NULL, indicating that you
967 * wish to retain the ref count on dp.
969 * This function removes the marker.
971 void
972 disk_enumerate_stop(struct disk *marker, struct disk *dp)
974 lwkt_gettoken(&disklist_token);
975 LIST_REMOVE(marker, d_list);
976 if (dp)
977 --dp->d_refs;
978 lwkt_reltoken(&disklist_token);
981 static
983 sysctl_disks(SYSCTL_HANDLER_ARGS)
985 struct disk marker;
986 struct disk *dp;
987 int error, first;
989 first = 1;
990 error = 0;
991 dp = NULL;
993 while ((dp = disk_enumerate(&marker, dp))) {
994 if (!first) {
995 error = SYSCTL_OUT(req, " ", 1);
996 if (error) {
997 disk_enumerate_stop(&marker, dp);
998 break;
1000 } else {
1001 first = 0;
1003 error = SYSCTL_OUT(req, dp->d_rawdev->si_name,
1004 strlen(dp->d_rawdev->si_name));
1005 if (error) {
1006 disk_enumerate_stop(&marker, dp);
1007 break;
1010 if (error == 0)
1011 error = SYSCTL_OUT(req, "", 1);
1012 return error;
1015 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
1016 sysctl_disks, "A", "names of available disks");
1019 * Open a disk device or partition.
1021 static
1023 diskopen(struct dev_open_args *ap)
1025 cdev_t dev = ap->a_head.a_dev;
1026 struct disk *dp;
1027 int error;
1030 * dp can't be NULL here XXX.
1032 * d_slice will be NULL if setdiskinfo() has not been called yet.
1033 * setdiskinfo() is typically called whether the disk is present
1034 * or not (e.g. CD), but the base disk device is created first
1035 * and there may be a race.
1037 dp = dev->si_disk;
1038 if (dp == NULL || dp->d_slice == NULL)
1039 return (ENXIO);
1040 error = 0;
1043 * Deal with open races
1045 lwkt_gettoken(&ds_token);
1046 while (dp->d_flags & DISKFLAG_LOCK) {
1047 dp->d_flags |= DISKFLAG_WANTED;
1048 error = tsleep(dp, PCATCH, "diskopen", hz);
1049 if (error) {
1050 lwkt_reltoken(&ds_token);
1051 return (error);
1054 dp->d_flags |= DISKFLAG_LOCK;
1057 * Open the underlying raw device.
1059 if (!dsisopen(dp->d_slice)) {
1060 #if 0
1061 if (!pdev->si_iosize_max)
1062 pdev->si_iosize_max = dev->si_iosize_max;
1063 #endif
1064 error = dev_dopen(dp->d_rawdev, ap->a_oflags,
1065 ap->a_devtype, ap->a_cred, NULL);
1068 if (error)
1069 goto out;
1070 error = dsopen(dev, ap->a_devtype, dp->d_info.d_dsflags,
1071 &dp->d_slice, &dp->d_info);
1072 if (!dsisopen(dp->d_slice)) {
1073 dev_dclose(dp->d_rawdev, ap->a_oflags, ap->a_devtype, NULL);
1075 out:
1076 dp->d_flags &= ~DISKFLAG_LOCK;
1077 if (dp->d_flags & DISKFLAG_WANTED) {
1078 dp->d_flags &= ~DISKFLAG_WANTED;
1079 wakeup(dp);
1081 lwkt_reltoken(&ds_token);
1083 KKASSERT(dp->d_opencount >= 0);
1084 /* If the open was successful, bump open count */
1085 if (error == 0)
1086 atomic_add_int(&dp->d_opencount, 1);
1088 return(error);
1092 * Close a disk device or partition
1094 static
1096 diskclose(struct dev_close_args *ap)
1098 cdev_t dev = ap->a_head.a_dev;
1099 struct disk *dp;
1100 int error;
1101 int lcount;
1103 error = 0;
1104 dp = dev->si_disk;
1107 * The cdev_t represents the disk/slice/part. The shared
1108 * dp structure governs all cdevs associated with the disk.
1110 * As a safety only close the underlying raw device on the last
1111 * close the disk device if our tracking of the slices/partitions
1112 * also indicates nothing is open.
1114 KKASSERT(dp->d_opencount >= 1);
1115 lcount = atomic_fetchadd_int(&dp->d_opencount, -1);
1117 lwkt_gettoken(&ds_token);
1118 dsclose(dev, ap->a_devtype, dp->d_slice);
1119 if (lcount <= 1 && !dsisopen(dp->d_slice)) {
1120 error = dev_dclose(dp->d_rawdev, ap->a_fflag, ap->a_devtype, NULL);
1122 lwkt_reltoken(&ds_token);
1124 return (error);
1128 * First execute the ioctl on the disk device, and if it isn't supported
1129 * try running it on the backing device.
1131 static
1133 diskioctl(struct dev_ioctl_args *ap)
1135 cdev_t dev = ap->a_head.a_dev;
1136 struct disk *dp;
1137 int error;
1138 u_int u;
1140 dp = dev->si_disk;
1141 if (dp == NULL)
1142 return (ENXIO);
1144 devfs_debug(DEVFS_DEBUG_DEBUG,
1145 "diskioctl: cmd is: %lx (name: %s)\n",
1146 ap->a_cmd, dev->si_name);
1147 devfs_debug(DEVFS_DEBUG_DEBUG,
1148 "diskioctl: &dp->d_slice is: %p, %p\n",
1149 &dp->d_slice, dp->d_slice);
1151 if (ap->a_cmd == DIOCGKERNELDUMP) {
1152 u = *(u_int *)ap->a_data;
1153 return disk_dumpconf(dev, u);
1156 if (ap->a_cmd == DIOCRECLUSTER && dev == dp->d_cdev) {
1157 error = disk_iocom_ioctl(dp, ap->a_cmd, ap->a_data);
1158 return error;
1161 if (&dp->d_slice == NULL || dp->d_slice == NULL ||
1162 ((dp->d_info.d_dsflags & DSO_DEVICEMAPPER) &&
1163 dkslice(dev) == WHOLE_DISK_SLICE)) {
1164 error = ENOIOCTL;
1165 } else {
1166 lwkt_gettoken(&ds_token);
1167 error = dsioctl(dev, ap->a_cmd, ap->a_data, ap->a_fflag,
1168 &dp->d_slice, &dp->d_info);
1169 lwkt_reltoken(&ds_token);
1172 if (error == ENOIOCTL) {
1173 error = dev_dioctl(dp->d_rawdev, ap->a_cmd, ap->a_data,
1174 ap->a_fflag, ap->a_cred, NULL, NULL);
1176 return (error);
1180 * Execute strategy routine
1182 * WARNING! We are using the KVABIO API and must not access memory
1183 * through bp->b_data without first calling bkvasync(bp).
1185 static
1187 diskstrategy(struct dev_strategy_args *ap)
1189 cdev_t dev = ap->a_head.a_dev;
1190 struct bio *bio = ap->a_bio;
1191 struct bio *nbio;
1192 struct disk *dp;
1194 dp = dev->si_disk;
1196 if (dp == NULL) {
1197 bio->bio_buf->b_error = ENXIO;
1198 bio->bio_buf->b_flags |= B_ERROR;
1199 biodone(bio);
1200 return(0);
1202 KKASSERT(dev->si_disk == dp);
1205 * The dscheck() function will also transform the slice relative
1206 * block number i.e. bio->bio_offset into a block number that can be
1207 * passed directly to the underlying raw device. If dscheck()
1208 * returns NULL it will have handled the bio for us (e.g. EOF
1209 * or error due to being beyond the device size).
1211 if ((nbio = dscheck(dev, bio, dp->d_slice)) != NULL) {
1212 dev_dstrategy(dp->d_rawdev, nbio);
1213 } else {
1214 biodone(bio);
1216 return(0);
1220 * Return the partition size in ?blocks?
1222 static
1224 diskpsize(struct dev_psize_args *ap)
1226 cdev_t dev = ap->a_head.a_dev;
1227 struct disk *dp;
1229 dp = dev->si_disk;
1230 if (dp == NULL)
1231 return(ENODEV);
1233 ap->a_result = dssize(dev, &dp->d_slice);
1235 if ((ap->a_result == -1) &&
1236 (dp->d_info.d_dsflags & DSO_RAWPSIZE)) {
1237 ap->a_head.a_dev = dp->d_rawdev;
1238 return dev_doperate(&ap->a_head);
1240 return(0);
1243 static int
1244 diskdump(struct dev_dump_args *ap)
1246 cdev_t dev = ap->a_head.a_dev;
1247 struct disk *dp = dev->si_disk;
1248 u_int64_t size, offset;
1249 int error;
1251 error = disk_dumpcheck(dev, &size, &ap->a_blkno, &ap->a_secsize);
1252 /* XXX: this should probably go in disk_dumpcheck somehow */
1253 if (ap->a_length != 0) {
1254 size *= DEV_BSIZE;
1255 offset = ap->a_blkno * DEV_BSIZE;
1256 if ((ap->a_offset < offset) ||
1257 (ap->a_offset + ap->a_length - offset > size)) {
1258 kprintf("Attempt to write outside dump "
1259 "device boundaries.\n");
1260 error = ENOSPC;
1264 if (error == 0) {
1265 ap->a_head.a_dev = dp->d_rawdev;
1266 error = dev_doperate(&ap->a_head);
1269 return(error);
1273 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
1274 0, sizeof(struct diskslices), "sizeof(struct diskslices)");
1276 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
1277 0, sizeof(struct disk), "sizeof(struct disk)");
1280 * Reorder interval for burst write allowance and minor write
1281 * allowance.
1283 * We always want to trickle some writes in to make use of the
1284 * disk's zone cache. Bursting occurs on a longer interval and only
1285 * runningbufspace is well over the hirunningspace limit.
1287 int bioq_reorder_burst_interval = 60; /* should be multiple of minor */
1288 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_burst_interval,
1289 CTLFLAG_RW, &bioq_reorder_burst_interval, 0, "");
1290 int bioq_reorder_minor_interval = 5;
1291 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_minor_interval,
1292 CTLFLAG_RW, &bioq_reorder_minor_interval, 0, "");
1294 int bioq_reorder_burst_bytes = 3000000;
1295 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_burst_bytes,
1296 CTLFLAG_RW, &bioq_reorder_burst_bytes, 0, "");
1297 int bioq_reorder_minor_bytes = 262144;
1298 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_minor_bytes,
1299 CTLFLAG_RW, &bioq_reorder_minor_bytes, 0, "");
1303 * Order I/Os. Generally speaking this code is designed to make better
1304 * use of drive zone caches. A drive zone cache can typically track linear
1305 * reads or writes for around 16 zones simultaniously.
1307 * Read prioritization issues: It is possible for hundreds of megabytes worth
1308 * of writes to be queued asynchronously. This creates a huge bottleneck
1309 * for reads which reduce read bandwidth to a trickle.
1311 * To solve this problem we generally reorder reads before writes.
1313 * However, a large number of random reads can also starve writes and
1314 * make poor use of the drive zone cache so we allow writes to trickle
1315 * in every N reads.
1317 void
1318 bioqdisksort(struct bio_queue_head *bioq, struct bio *bio)
1320 #if 0
1322 * The BIO wants to be ordered. Adding to the tail also
1323 * causes transition to be set to NULL, forcing the ordering
1324 * of all prior I/O's.
1326 if (bio->bio_buf->b_flags & B_ORDERED) {
1327 bioq_insert_tail(bioq, bio);
1328 return;
1330 #endif
1332 switch(bio->bio_buf->b_cmd) {
1333 case BUF_CMD_READ:
1334 if (bioq->transition) {
1336 * Insert before the first write. Bleedover writes
1337 * based on reorder intervals to prevent starvation.
1339 TAILQ_INSERT_BEFORE(bioq->transition, bio, bio_act);
1340 ++bioq->reorder;
1341 if (bioq->reorder % bioq_reorder_minor_interval == 0) {
1342 bioqwritereorder(bioq);
1343 if (bioq->reorder >=
1344 bioq_reorder_burst_interval) {
1345 bioq->reorder = 0;
1348 } else {
1350 * No writes queued (or ordering was forced),
1351 * insert at tail.
1353 TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act);
1355 break;
1356 case BUF_CMD_WRITE:
1358 * Writes are always appended. If no writes were previously
1359 * queued or an ordered tail insertion occured the transition
1360 * field will be NULL.
1362 TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act);
1363 if (bioq->transition == NULL)
1364 bioq->transition = bio;
1365 break;
1366 default:
1368 * All other request types are forced to be ordered.
1370 bioq_insert_tail(bioq, bio);
1371 break;
1376 * Move the read-write transition point to prevent reads from
1377 * completely starving our writes. This brings a number of writes into
1378 * the fold every N reads.
1380 * We bring a few linear writes into the fold on a minor interval
1381 * and we bring a non-linear burst of writes into the fold on a major
1382 * interval. Bursting only occurs if runningbufspace is really high
1383 * (typically from syncs, fsyncs, or HAMMER flushes).
1385 static
1386 void
1387 bioqwritereorder(struct bio_queue_head *bioq)
1389 struct bio *bio;
1390 off_t next_offset;
1391 size_t left;
1392 size_t n;
1393 int check_off;
1395 if (bioq->reorder < bioq_reorder_burst_interval ||
1396 !buf_runningbufspace_severe()) {
1397 left = (size_t)bioq_reorder_minor_bytes;
1398 check_off = 1;
1399 } else {
1400 left = (size_t)bioq_reorder_burst_bytes;
1401 check_off = 0;
1404 next_offset = bioq->transition->bio_offset;
1405 while ((bio = bioq->transition) != NULL &&
1406 (check_off == 0 || next_offset == bio->bio_offset)
1408 n = bio->bio_buf->b_bcount;
1409 next_offset = bio->bio_offset + n;
1410 bioq->transition = TAILQ_NEXT(bio, bio_act);
1411 if (left < n)
1412 break;
1413 left -= n;
1418 * Bounds checking against the media size, used for the raw partition.
1419 * secsize, mediasize and b_blkno must all be the same units.
1420 * Possibly this has to be DEV_BSIZE (512).
1423 bounds_check_with_mediasize(struct bio *bio, int secsize, uint64_t mediasize)
1425 struct buf *bp = bio->bio_buf;
1426 int64_t sz;
1428 sz = howmany(bp->b_bcount, secsize);
1430 if (bio->bio_offset/DEV_BSIZE + sz > mediasize) {
1431 sz = mediasize - bio->bio_offset/DEV_BSIZE;
1432 if (sz == 0) {
1433 /* If exactly at end of disk, return EOF. */
1434 bp->b_resid = bp->b_bcount;
1435 return 0;
1437 if (sz < 0) {
1438 /* If past end of disk, return EINVAL. */
1439 bp->b_error = EINVAL;
1440 return 0;
1442 /* Otherwise, truncate request. */
1443 bp->b_bcount = sz * secsize;
1446 return 1;
1450 * Disk error is the preface to plaintive error messages
1451 * about failing disk transfers. It prints messages of the form
1453 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
1455 * if the offset of the error in the transfer and a disk label
1456 * are both available. blkdone should be -1 if the position of the error
1457 * is unknown; the disklabel pointer may be null from drivers that have not
1458 * been converted to use them. The message is printed with kprintf
1459 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
1460 * The message should be completed (with at least a newline) with kprintf
1461 * or log(-1, ...), respectively. There is no trailing space.
1463 void
1464 diskerr(struct bio *bio, cdev_t dev, const char *what, int pri, int donecnt)
1466 struct buf *bp = bio->bio_buf;
1467 const char *term;
1469 switch(bp->b_cmd) {
1470 case BUF_CMD_READ:
1471 term = "read";
1472 break;
1473 case BUF_CMD_WRITE:
1474 term = "write";
1475 break;
1476 default:
1477 term = "access";
1478 break;
1480 kprintf("%s: %s %sing ", dev->si_name, what, term);
1481 kprintf("offset %012llx for %d",
1482 (long long)bio->bio_offset,
1483 bp->b_bcount);
1485 if (donecnt)
1486 kprintf(" (%d bytes completed)", donecnt);
1490 * Locate a disk device
1492 cdev_t
1493 disk_locate(const char *devname)
1495 return devfs_find_device_by_name("%s", devname);
1498 void
1499 disk_config(void *arg)
1501 disk_msg_send_sync(DISK_SYNC, NULL, NULL);
1504 static void
1505 disk_init(void)
1507 struct thread* td_core;
1509 disk_msg_cache = objcache_create("disk-msg-cache", 0, 0,
1510 NULL, NULL, NULL,
1511 objcache_malloc_alloc,
1512 objcache_malloc_free,
1513 &disk_msg_malloc_args);
1515 lwkt_token_init(&disklist_token, "disks");
1516 lwkt_token_init(&ds_token, "ds");
1519 * Initialize the reply-only port which acts as a message drain
1521 lwkt_initport_replyonly(&disk_dispose_port, disk_msg_autofree_reply);
1523 lwkt_gettoken(&disklist_token);
1524 lwkt_create(disk_msg_core, /*args*/NULL, &td_core, NULL,
1525 0, -1, "disk_msg_core");
1526 tsleep(td_core, 0, "diskcore", 0);
1527 lwkt_reltoken(&disklist_token);
1530 static void
1531 disk_uninit(void)
1533 objcache_destroy(disk_msg_cache);
1537 * Clean out illegal characters in serial numbers.
1539 static void
1540 disk_cleanserial(char *serno)
1542 char c;
1544 while ((c = *serno) != 0) {
1545 if (c >= 'a' && c <= 'z')
1547 else if (c >= 'A' && c <= 'Z')
1549 else if (c >= '0' && c <= '9')
1551 else if (c == '-' || c == '@' || c == '+' || c == '.')
1553 else
1554 c = '_';
1555 *serno++= c;
1559 TUNABLE_INT("kern.disk_debug", &disk_debug_enable);
1560 SYSCTL_INT(_kern, OID_AUTO, disk_debug, CTLFLAG_RW, &disk_debug_enable,
1561 0, "Enable subr_disk debugging");
1563 SYSINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, disk_init, NULL);
1564 SYSUNINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, disk_uninit, NULL);