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
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
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
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
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
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>
83 #include <sys/sysctl.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>
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
},
139 .d_close
= diskclose
,
141 .d_write
= physwrite
,
142 .d_ioctl
= diskioctl
,
143 .d_strategy
= diskstrategy
,
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
;
157 disk_debug(int level
, char *fmt
, ...)
162 if (level
<= disk_debug_enable
)
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
];
175 struct partinfo part
;
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
);
195 if (slice
!= WHOLE_DISK_SLICE
)
196 ops
->op_adjust_label_reserved(dp
->d_slice
, slice
, sp
);
201 for (i
= 0; i
< ops
->op_getnumparts(sp
->ds_label
); i
++) {
202 ops
->op_loadpartinfo(sp
->ds_label
, i
, &part
);
205 (ndev
= devfs_find_device_by_name("%s%c",
206 dev
->si_name
, 'a' + i
))
209 * Device already exists and
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
,
227 udev_dict_set_cstr(ndev
, "uuid", uuid_buf
);
230 ndev
= make_dev_covering(&disk_ops
, dp
->d_rawdev
->si_ops
,
231 dkmakeminor(dkunit(dp
->d_cdev
),
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
;
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
) {
249 dp
->d_info
.d_serialno
,
253 /* Create UUID alias */
254 if (!kuuid_is_nil(&part
.storage_uuid
)) {
255 snprintf_uuid(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
) {
269 if (sp
->ds_size
>= 0x100000000ULL
)
270 ops
= &disklabel64_ops
;
272 ops
= &disklabel32_ops
;
273 sp
->ds_label
= ops
->op_clone_label(info
, sp
);
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",
282 if (sp
->ds_label
.opaque
!= NULL
&& sp
->ds_ops
!= NULL
) {
283 /* Clear out old label - it's not around anymore */
285 "disk_probe_slice: clear out old diskabel on %s\n",
288 sp
->ds_ops
->op_freedisklabel(&sp
->ds_label
);
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.
306 disk_probe(struct disk
*dp
, int reprobe
)
308 struct disk_info
*info
= &dp
->d_info
;
309 cdev_t dev
= dp
->d_cdev
;
312 struct diskslices
*osp
;
313 struct diskslice
*sp
;
316 KKASSERT (info
->d_media_blksize
!= 0);
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
));
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
)
337 * Ignore the compatibility slice s0 if it's a device mapper
340 if ((i
== COMPATIBILITY_SLICE
) &&
341 (info
->d_dsflags
& DSO_DEVICEMAPPER
))
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
352 if (i
== COMPATIBILITY_SLICE
) {
354 if (sp
->ds_type
== 0 &&
355 dp
->d_slice
->dss_nslices
== BASE_SLICE
) {
356 sp
->ds_size
= info
->d_media_blocks
;
365 * Ignore 0-length slices
367 if (sp
->ds_size
== 0)
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
),
387 make_dev_alias(ndev
, "slice-by-uuid/%s",
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
),
418 make_dev_alias(ndev
, "slice-by-uuid/%s",
423 ndev
->si_flags
|= SI_REPROBE_TEST
;
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
||
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
);
444 disk_debug(1, "disk_probe (end): %s\n", dp
->d_cdev
->si_name
);
449 disk_msg_core(void *arg
)
452 struct diskslice
*sp
;
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? */
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
;
471 "DISK_DISK_PROBE: %s\n",
472 dp
->d_cdev
->si_name
);
473 disk_iocom_update(dp
);
476 case DISK_DISK_DESTROY
:
477 dp
= (struct disk
*)msg
->load
;
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.
487 * This avoids numerous races.
489 lwkt_gettoken(&disklist_token
);
491 tsleep(&dp
->d_refs
, 0, "diskdel", hz
/ 10);
492 LIST_REMOVE(dp
, d_list
);
494 dsched_disk_destroy(dp
);
495 devfs_destroy_related(dp
->d_cdev
);
496 destroy_dev(dp
->d_cdev
);
497 destroy_only_dev(dp
->d_rawdev
);
499 lwkt_reltoken(&disklist_token
);
501 if (dp
->d_info
.d_serialno
) {
502 kfree(dp
->d_info
.d_serialno
, M_TEMP
);
503 dp
->d_info
.d_serialno
= NULL
;
507 dp
= (struct disk
*)msg
->load
;
509 "DISK_DISK_UNPROBE: %s\n",
510 dp
->d_cdev
->si_name
);
511 devfs_destroy_related(dp
->d_cdev
);
513 case DISK_SLICE_REPROBE
:
514 dp
= (struct disk
*)msg
->load
;
515 sp
= (struct diskslice
*)msg
->load2
;
516 devfs_clr_related_flag(sp
->ds_dev
,
519 "DISK_SLICE_REPROBE: %s\n",
520 sp
->ds_dev
->si_name
);
521 disk_probe_slice(dp
, sp
->ds_dev
,
522 dkslice(sp
->ds_dev
), 1);
523 devfs_destroy_related_without_flag(
524 sp
->ds_dev
, SI_REPROBE_TEST
);
526 case DISK_DISK_REPROBE
:
527 dp
= (struct disk
*)msg
->load
;
528 devfs_clr_related_flag(dp
->d_cdev
, SI_REPROBE_TEST
);
530 "DISK_DISK_REPROBE: %s\n",
531 dp
->d_cdev
->si_name
);
533 devfs_destroy_related_without_flag(
534 dp
->d_cdev
, SI_REPROBE_TEST
);
537 disk_debug(1, "DISK_SYNC\n");
540 devfs_debug(DEVFS_DEBUG_WARNING
,
541 "disk_msg_core: unknown message "
542 "received at core\n");
545 lwkt_replymsg(&msg
->hdr
, 0);
552 * Acts as a message drain. Any message that is replied to here gets
553 * destroyed and the memory freed.
556 disk_msg_autofree_reply(lwkt_port_t port
, lwkt_msg_t msg
)
558 objcache_put(disk_msg_cache
, msg
);
563 disk_msg_send(uint32_t cmd
, void *load
, void *load2
)
566 lwkt_port_t port
= &disk_msg_port
;
568 disk_msg
= objcache_get(disk_msg_cache
, M_WAITOK
);
570 lwkt_initmsg(&disk_msg
->hdr
, &disk_dispose_port
, 0);
572 disk_msg
->hdr
.u
.ms_result
= cmd
;
573 disk_msg
->load
= load
;
574 disk_msg
->load2
= load2
;
576 lwkt_sendmsg(port
, &disk_msg
->hdr
);
580 disk_msg_send_sync(uint32_t cmd
, void *load
, void *load2
)
582 struct lwkt_port rep_port
;
586 disk_msg
= objcache_get(disk_msg_cache
, M_WAITOK
);
587 port
= &disk_msg_port
;
589 /* XXX could probably use curthread's built-in msgport */
590 lwkt_initport_thread(&rep_port
, curthread
);
591 lwkt_initmsg(&disk_msg
->hdr
, &rep_port
, 0);
593 disk_msg
->hdr
.u
.ms_result
= cmd
;
594 disk_msg
->load
= load
;
595 disk_msg
->load2
= load2
;
597 lwkt_domsg(port
, &disk_msg
->hdr
, 0);
598 objcache_put(disk_msg_cache
, disk_msg
);
602 * Create a raw device for the dev_ops template (which is returned). Also
603 * create a slice and unit managed disk and overload the user visible
604 * device space with it.
606 * NOTE: The returned raw device is NOT a slice and unit managed device.
607 * It is an actual raw device representing the raw disk as specified by
608 * the passed dev_ops. The disk layer not only returns such a raw device,
609 * it also uses it internally when passing (modified) commands through.
612 disk_create(int unit
, struct disk
*dp
, struct dev_ops
*raw_ops
)
614 return _disk_create_named(NULL
, unit
, dp
, raw_ops
, 0);
618 disk_create_clone(int unit
, struct disk
*dp
,
619 struct dev_ops
*raw_ops
)
621 return _disk_create_named(NULL
, unit
, dp
, raw_ops
, 1);
625 disk_create_named(const char *name
, int unit
, struct disk
*dp
,
626 struct dev_ops
*raw_ops
)
628 return _disk_create_named(name
, unit
, dp
, raw_ops
, 0);
632 disk_create_named_clone(const char *name
, int unit
, struct disk
*dp
,
633 struct dev_ops
*raw_ops
)
635 return _disk_create_named(name
, unit
, dp
, raw_ops
, 1);
639 _disk_create_named(const char *name
, int unit
, struct disk
*dp
,
640 struct dev_ops
*raw_ops
, int clone
)
644 disk_debug(1, "disk_create (begin): %s%d\n", name
, unit
);
647 rawdev
= make_only_dev(raw_ops
, dkmakewholedisk(unit
),
648 UID_ROOT
, GID_OPERATOR
, 0640, "%s", name
);
650 rawdev
= make_only_dev(raw_ops
, dkmakewholedisk(unit
),
651 UID_ROOT
, GID_OPERATOR
, 0640,
652 "%s%d", raw_ops
->head
.name
, unit
);
655 bzero(dp
, sizeof(*dp
));
657 dp
->d_rawdev
= rawdev
;
658 dp
->d_raw_ops
= raw_ops
;
659 dp
->d_dev_ops
= &disk_ops
;
663 dp
->d_cdev
= make_only_dev_covering(
664 &disk_ops
, dp
->d_rawdev
->si_ops
,
665 dkmakewholedisk(unit
),
666 UID_ROOT
, GID_OPERATOR
, 0640,
669 dp
->d_cdev
= make_dev_covering(
670 &disk_ops
, dp
->d_rawdev
->si_ops
,
671 dkmakewholedisk(unit
),
672 UID_ROOT
, GID_OPERATOR
, 0640,
677 dp
->d_cdev
= make_only_dev_covering(
678 &disk_ops
, dp
->d_rawdev
->si_ops
,
679 dkmakewholedisk(unit
),
680 UID_ROOT
, GID_OPERATOR
, 0640,
681 "%s%d", raw_ops
->head
.name
, unit
);
683 dp
->d_cdev
= make_dev_covering(
684 &disk_ops
, dp
->d_rawdev
->si_ops
,
685 dkmakewholedisk(unit
),
686 UID_ROOT
, GID_OPERATOR
, 0640,
687 "%s%d", raw_ops
->head
.name
, unit
);
691 udev_dict_set_cstr(dp
->d_cdev
, "subsystem", "disk");
692 dp
->d_cdev
->si_disk
= dp
;
695 dsched_disk_create(dp
, name
, unit
);
697 dsched_disk_create(dp
, raw_ops
->head
.name
, unit
);
699 lwkt_gettoken(&disklist_token
);
700 LIST_INSERT_HEAD(&disklist
, dp
, d_list
);
701 lwkt_reltoken(&disklist_token
);
705 disk_debug(1, "disk_create (end): %s%d\n",
706 (name
!= NULL
)?(name
):(raw_ops
->head
.name
), unit
);
708 return (dp
->d_rawdev
);
712 disk_setdisktype(struct disk
*disk
, const char *type
)
716 KKASSERT(disk
!= NULL
);
718 disk
->d_disktype
= type
;
719 error
= udev_dict_set_cstr(disk
->d_cdev
, "disk-type",
720 __DECONST(char *, type
));
725 disk_getopencount(struct disk
*disk
)
727 return disk
->d_opencount
;
731 _setdiskinfo(struct disk
*disk
, struct disk_info
*info
)
735 oldserialno
= disk
->d_info
.d_serialno
;
736 bcopy(info
, &disk
->d_info
, sizeof(disk
->d_info
));
737 info
= &disk
->d_info
;
739 disk_debug(1, "_setdiskinfo: %s\n", disk
->d_cdev
->si_name
);
742 * The serial number is duplicated so the caller can throw
745 if (info
->d_serialno
&& info
->d_serialno
[0] &&
746 (info
->d_serialno
[0] != ' ' || strlen(info
->d_serialno
) > 1)) {
747 info
->d_serialno
= kstrdup(info
->d_serialno
, M_TEMP
);
748 disk_cleanserial(info
->d_serialno
);
750 make_dev_alias(disk
->d_cdev
, "serno/%s",
754 info
->d_serialno
= NULL
;
757 kfree(oldserialno
, M_TEMP
);
759 dsched_disk_update(disk
, info
);
762 * The caller may set d_media_size or d_media_blocks and we
763 * calculate the other.
765 KKASSERT(info
->d_media_size
== 0 || info
->d_media_blocks
== 0);
766 if (info
->d_media_size
== 0 && info
->d_media_blocks
) {
767 info
->d_media_size
= (u_int64_t
)info
->d_media_blocks
*
768 info
->d_media_blksize
;
769 } else if (info
->d_media_size
&& info
->d_media_blocks
== 0 &&
770 info
->d_media_blksize
) {
771 info
->d_media_blocks
= info
->d_media_size
/
772 info
->d_media_blksize
;
776 * The si_* fields for rawdev are not set until after the
777 * disk_create() call, so someone using the cooked version
778 * of the raw device (i.e. da0s0) will not get the right
779 * si_iosize_max unless we fix it up here.
781 if (disk
->d_cdev
&& disk
->d_rawdev
&&
782 disk
->d_cdev
->si_iosize_max
== 0) {
783 disk
->d_cdev
->si_iosize_max
= disk
->d_rawdev
->si_iosize_max
;
784 disk
->d_cdev
->si_bsize_phys
= disk
->d_rawdev
->si_bsize_phys
;
785 disk
->d_cdev
->si_bsize_best
= disk
->d_rawdev
->si_bsize_best
;
788 /* Add the serial number to the udev_dictionary */
789 if (info
->d_serialno
)
790 udev_dict_set_cstr(disk
->d_cdev
, "serno", info
->d_serialno
);
794 * Disk drivers must call this routine when media parameters are available
798 disk_setdiskinfo(struct disk
*disk
, struct disk_info
*info
)
800 _setdiskinfo(disk
, info
);
801 disk_msg_send(DISK_DISK_PROBE
, disk
, NULL
);
802 disk_debug(1, "disk_setdiskinfo: sent probe for %s\n",
803 disk
->d_cdev
->si_name
);
807 disk_setdiskinfo_sync(struct disk
*disk
, struct disk_info
*info
)
809 _setdiskinfo(disk
, info
);
810 disk_msg_send_sync(DISK_DISK_PROBE
, disk
, NULL
);
811 disk_debug(1, "disk_setdiskinfo_sync: sent probe for %s\n",
812 disk
->d_cdev
->si_name
);
816 * This routine is called when an adapter detaches. The higher level
817 * managed disk device is destroyed while the lower level raw device is
821 disk_destroy(struct disk
*disk
)
823 disk_msg_send_sync(DISK_DISK_DESTROY
, disk
, NULL
);
828 disk_dumpcheck(cdev_t dev
, u_int64_t
*size
,
829 u_int64_t
*blkno
, u_int32_t
*secsize
)
831 struct partinfo pinfo
;
834 bzero(&pinfo
, sizeof(pinfo
));
835 error
= dev_dioctl(dev
, DIOCGPART
, (void *)&pinfo
, 0,
836 proc0
.p_ucred
, NULL
, NULL
);
840 if (pinfo
.media_blksize
== 0)
843 if (blkno
) /* XXX: make sure this reserved stuff is right */
844 *blkno
= pinfo
.reserved_blocks
+
845 pinfo
.media_offset
/ pinfo
.media_blksize
;
847 *secsize
= pinfo
.media_blksize
;
849 *size
= (pinfo
.media_blocks
- pinfo
.reserved_blocks
);
855 disk_dumpconf(cdev_t dev
, u_int onoff
)
857 struct dumperinfo di
;
858 u_int64_t size
, blkno
;
863 return set_dumper(NULL
);
865 error
= disk_dumpcheck(dev
, &size
, &blkno
, &secsize
);
870 bzero(&di
, sizeof(struct dumperinfo
));
871 di
.dumper
= diskdump
;
873 di
.blocksize
= secsize
;
874 di
.maxiosize
= dev
->si_iosize_max
;
875 di
.mediaoffset
= blkno
* DEV_BSIZE
;
876 di
.mediasize
= size
* DEV_BSIZE
;
878 return set_dumper(&di
);
882 disk_unprobe(struct disk
*disk
)
887 disk_msg_send_sync(DISK_UNPROBE
, disk
, NULL
);
891 disk_invalidate (struct disk
*disk
)
893 dsgone(&disk
->d_slice
);
897 * Enumerate disks, pass a marker and an initial NULL dp to initialize,
898 * then loop with the previously returned dp.
900 * The returned dp will be referenced, preventing its destruction. When
901 * you pass the returned dp back into the loop the ref is dropped.
903 * WARNING: If terminating your loop early you must call
904 * disk_enumerate_stop().
907 disk_enumerate(struct disk
*marker
, struct disk
*dp
)
909 lwkt_gettoken(&disklist_token
);
912 dp
= LIST_NEXT(marker
, d_list
);
913 LIST_REMOVE(marker
, d_list
);
915 bzero(marker
, sizeof(*marker
));
916 marker
->d_flags
= DISKFLAG_MARKER
;
917 dp
= LIST_FIRST(&disklist
);
920 if ((dp
->d_flags
& DISKFLAG_MARKER
) == 0)
922 dp
= LIST_NEXT(dp
, d_list
);
926 LIST_INSERT_AFTER(dp
, marker
, d_list
);
928 lwkt_reltoken(&disklist_token
);
933 * Terminate an enumeration early. Do not call this function if the
934 * enumeration ended normally. dp can be NULL, indicating that you
935 * wish to retain the ref count on dp.
937 * This function removes the marker.
940 disk_enumerate_stop(struct disk
*marker
, struct disk
*dp
)
942 lwkt_gettoken(&disklist_token
);
943 LIST_REMOVE(marker
, d_list
);
946 lwkt_reltoken(&disklist_token
);
951 sysctl_disks(SYSCTL_HANDLER_ARGS
)
961 while ((dp
= disk_enumerate(&marker
, dp
))) {
963 error
= SYSCTL_OUT(req
, " ", 1);
965 disk_enumerate_stop(&marker
, dp
);
971 error
= SYSCTL_OUT(req
, dp
->d_rawdev
->si_name
,
972 strlen(dp
->d_rawdev
->si_name
));
974 disk_enumerate_stop(&marker
, dp
);
979 error
= SYSCTL_OUT(req
, "", 1);
983 SYSCTL_PROC(_kern
, OID_AUTO
, disks
, CTLTYPE_STRING
| CTLFLAG_RD
, NULL
, 0,
984 sysctl_disks
, "A", "names of available disks");
987 * Open a disk device or partition.
991 diskopen(struct dev_open_args
*ap
)
993 cdev_t dev
= ap
->a_head
.a_dev
;
998 * dp can't be NULL here XXX.
1000 * d_slice will be NULL if setdiskinfo() has not been called yet.
1001 * setdiskinfo() is typically called whether the disk is present
1002 * or not (e.g. CD), but the base disk device is created first
1003 * and there may be a race.
1006 if (dp
== NULL
|| dp
->d_slice
== NULL
)
1011 * Deal with open races
1014 while (dp
->d_flags
& DISKFLAG_LOCK
) {
1015 dp
->d_flags
|= DISKFLAG_WANTED
;
1016 error
= tsleep(dp
, PCATCH
, "diskopen", hz
);
1022 dp
->d_flags
|= DISKFLAG_LOCK
;
1025 * Open the underlying raw device.
1027 if (!dsisopen(dp
->d_slice
)) {
1029 if (!pdev
->si_iosize_max
)
1030 pdev
->si_iosize_max
= dev
->si_iosize_max
;
1032 error
= dev_dopen(dp
->d_rawdev
, ap
->a_oflags
,
1033 ap
->a_devtype
, ap
->a_cred
, NULL
);
1038 error
= dsopen(dev
, ap
->a_devtype
, dp
->d_info
.d_dsflags
,
1039 &dp
->d_slice
, &dp
->d_info
);
1040 if (!dsisopen(dp
->d_slice
)) {
1041 dev_dclose(dp
->d_rawdev
, ap
->a_oflags
, ap
->a_devtype
, NULL
);
1044 dp
->d_flags
&= ~DISKFLAG_LOCK
;
1045 if (dp
->d_flags
& DISKFLAG_WANTED
) {
1046 dp
->d_flags
&= ~DISKFLAG_WANTED
;
1051 KKASSERT(dp
->d_opencount
>= 0);
1052 /* If the open was successful, bump open count */
1054 atomic_add_int(&dp
->d_opencount
, 1);
1060 * Close a disk device or partition
1064 diskclose(struct dev_close_args
*ap
)
1066 cdev_t dev
= ap
->a_head
.a_dev
;
1075 * The cdev_t represents the disk/slice/part. The shared
1076 * dp structure governs all cdevs associated with the disk.
1078 * As a safety only close the underlying raw device on the last
1079 * close the disk device if our tracking of the slices/partitions
1080 * also indicates nothing is open.
1082 KKASSERT(dp
->d_opencount
>= 1);
1083 lcount
= atomic_fetchadd_int(&dp
->d_opencount
, -1);
1086 dsclose(dev
, ap
->a_devtype
, dp
->d_slice
);
1087 if (lcount
<= 1 && !dsisopen(dp
->d_slice
)) {
1088 error
= dev_dclose(dp
->d_rawdev
, ap
->a_fflag
, ap
->a_devtype
, NULL
);
1095 * First execute the ioctl on the disk device, and if it isn't supported
1096 * try running it on the backing device.
1100 diskioctl(struct dev_ioctl_args
*ap
)
1102 cdev_t dev
= ap
->a_head
.a_dev
;
1111 devfs_debug(DEVFS_DEBUG_DEBUG
,
1112 "diskioctl: cmd is: %lx (name: %s)\n",
1113 ap
->a_cmd
, dev
->si_name
);
1114 devfs_debug(DEVFS_DEBUG_DEBUG
,
1115 "diskioctl: &dp->d_slice is: %p, %p\n",
1116 &dp
->d_slice
, dp
->d_slice
);
1118 if (ap
->a_cmd
== DIOCGKERNELDUMP
) {
1119 u
= *(u_int
*)ap
->a_data
;
1120 return disk_dumpconf(dev
, u
);
1123 if (ap
->a_cmd
== DIOCRECLUSTER
&& dev
== dp
->d_cdev
) {
1124 error
= disk_iocom_ioctl(dp
, ap
->a_cmd
, ap
->a_data
);
1128 if (&dp
->d_slice
== NULL
|| dp
->d_slice
== NULL
||
1129 ((dp
->d_info
.d_dsflags
& DSO_DEVICEMAPPER
) &&
1130 dkslice(dev
) == WHOLE_DISK_SLICE
)) {
1134 error
= dsioctl(dev
, ap
->a_cmd
, ap
->a_data
, ap
->a_fflag
,
1135 &dp
->d_slice
, &dp
->d_info
);
1139 if (error
== ENOIOCTL
) {
1140 error
= dev_dioctl(dp
->d_rawdev
, ap
->a_cmd
, ap
->a_data
,
1141 ap
->a_fflag
, ap
->a_cred
, NULL
, NULL
);
1147 * Execute strategy routine
1151 diskstrategy(struct dev_strategy_args
*ap
)
1153 cdev_t dev
= ap
->a_head
.a_dev
;
1154 struct bio
*bio
= ap
->a_bio
;
1161 bio
->bio_buf
->b_error
= ENXIO
;
1162 bio
->bio_buf
->b_flags
|= B_ERROR
;
1166 KKASSERT(dev
->si_disk
== dp
);
1169 * The dscheck() function will also transform the slice relative
1170 * block number i.e. bio->bio_offset into a block number that can be
1171 * passed directly to the underlying raw device. If dscheck()
1172 * returns NULL it will have handled the bio for us (e.g. EOF
1173 * or error due to being beyond the device size).
1175 if ((nbio
= dscheck(dev
, bio
, dp
->d_slice
)) != NULL
) {
1176 dev_dstrategy(dp
->d_rawdev
, nbio
);
1184 * Return the partition size in ?blocks?
1188 diskpsize(struct dev_psize_args
*ap
)
1190 cdev_t dev
= ap
->a_head
.a_dev
;
1197 ap
->a_result
= dssize(dev
, &dp
->d_slice
);
1199 if ((ap
->a_result
== -1) &&
1200 (dp
->d_info
.d_dsflags
& DSO_RAWPSIZE
)) {
1201 ap
->a_head
.a_dev
= dp
->d_rawdev
;
1202 return dev_doperate(&ap
->a_head
);
1208 diskdump(struct dev_dump_args
*ap
)
1210 cdev_t dev
= ap
->a_head
.a_dev
;
1211 struct disk
*dp
= dev
->si_disk
;
1212 u_int64_t size
, offset
;
1215 error
= disk_dumpcheck(dev
, &size
, &ap
->a_blkno
, &ap
->a_secsize
);
1216 /* XXX: this should probably go in disk_dumpcheck somehow */
1217 if (ap
->a_length
!= 0) {
1219 offset
= ap
->a_blkno
* DEV_BSIZE
;
1220 if ((ap
->a_offset
< offset
) ||
1221 (ap
->a_offset
+ ap
->a_length
- offset
> size
)) {
1222 kprintf("Attempt to write outside dump "
1223 "device boundaries.\n");
1229 ap
->a_head
.a_dev
= dp
->d_rawdev
;
1230 error
= dev_doperate(&ap
->a_head
);
1237 SYSCTL_INT(_debug_sizeof
, OID_AUTO
, diskslices
, CTLFLAG_RD
,
1238 0, sizeof(struct diskslices
), "sizeof(struct diskslices)");
1240 SYSCTL_INT(_debug_sizeof
, OID_AUTO
, disk
, CTLFLAG_RD
,
1241 0, sizeof(struct disk
), "sizeof(struct disk)");
1244 * Reorder interval for burst write allowance and minor write
1247 * We always want to trickle some writes in to make use of the
1248 * disk's zone cache. Bursting occurs on a longer interval and only
1249 * runningbufspace is well over the hirunningspace limit.
1251 int bioq_reorder_burst_interval
= 60; /* should be multiple of minor */
1252 SYSCTL_INT(_kern
, OID_AUTO
, bioq_reorder_burst_interval
,
1253 CTLFLAG_RW
, &bioq_reorder_burst_interval
, 0, "");
1254 int bioq_reorder_minor_interval
= 5;
1255 SYSCTL_INT(_kern
, OID_AUTO
, bioq_reorder_minor_interval
,
1256 CTLFLAG_RW
, &bioq_reorder_minor_interval
, 0, "");
1258 int bioq_reorder_burst_bytes
= 3000000;
1259 SYSCTL_INT(_kern
, OID_AUTO
, bioq_reorder_burst_bytes
,
1260 CTLFLAG_RW
, &bioq_reorder_burst_bytes
, 0, "");
1261 int bioq_reorder_minor_bytes
= 262144;
1262 SYSCTL_INT(_kern
, OID_AUTO
, bioq_reorder_minor_bytes
,
1263 CTLFLAG_RW
, &bioq_reorder_minor_bytes
, 0, "");
1267 * Order I/Os. Generally speaking this code is designed to make better
1268 * use of drive zone caches. A drive zone cache can typically track linear
1269 * reads or writes for around 16 zones simultaniously.
1271 * Read prioritization issues: It is possible for hundreds of megabytes worth
1272 * of writes to be queued asynchronously. This creates a huge bottleneck
1273 * for reads which reduce read bandwidth to a trickle.
1275 * To solve this problem we generally reorder reads before writes.
1277 * However, a large number of random reads can also starve writes and
1278 * make poor use of the drive zone cache so we allow writes to trickle
1282 bioqdisksort(struct bio_queue_head
*bioq
, struct bio
*bio
)
1285 * The BIO wants to be ordered. Adding to the tail also
1286 * causes transition to be set to NULL, forcing the ordering
1287 * of all prior I/O's.
1289 if (bio
->bio_buf
->b_flags
& B_ORDERED
) {
1290 bioq_insert_tail(bioq
, bio
);
1294 switch(bio
->bio_buf
->b_cmd
) {
1296 if (bioq
->transition
) {
1298 * Insert before the first write. Bleedover writes
1299 * based on reorder intervals to prevent starvation.
1301 TAILQ_INSERT_BEFORE(bioq
->transition
, bio
, bio_act
);
1303 if (bioq
->reorder
% bioq_reorder_minor_interval
== 0) {
1304 bioqwritereorder(bioq
);
1305 if (bioq
->reorder
>=
1306 bioq_reorder_burst_interval
) {
1312 * No writes queued (or ordering was forced),
1315 TAILQ_INSERT_TAIL(&bioq
->queue
, bio
, bio_act
);
1320 * Writes are always appended. If no writes were previously
1321 * queued or an ordered tail insertion occured the transition
1322 * field will be NULL.
1324 TAILQ_INSERT_TAIL(&bioq
->queue
, bio
, bio_act
);
1325 if (bioq
->transition
== NULL
)
1326 bioq
->transition
= bio
;
1330 * All other request types are forced to be ordered.
1332 bioq_insert_tail(bioq
, bio
);
1338 * Move the read-write transition point to prevent reads from
1339 * completely starving our writes. This brings a number of writes into
1340 * the fold every N reads.
1342 * We bring a few linear writes into the fold on a minor interval
1343 * and we bring a non-linear burst of writes into the fold on a major
1344 * interval. Bursting only occurs if runningbufspace is really high
1345 * (typically from syncs, fsyncs, or HAMMER flushes).
1349 bioqwritereorder(struct bio_queue_head
*bioq
)
1357 if (bioq
->reorder
< bioq_reorder_burst_interval
||
1358 !buf_runningbufspace_severe()) {
1359 left
= (size_t)bioq_reorder_minor_bytes
;
1362 left
= (size_t)bioq_reorder_burst_bytes
;
1366 next_offset
= bioq
->transition
->bio_offset
;
1367 while ((bio
= bioq
->transition
) != NULL
&&
1368 (check_off
== 0 || next_offset
== bio
->bio_offset
)
1370 n
= bio
->bio_buf
->b_bcount
;
1371 next_offset
= bio
->bio_offset
+ n
;
1372 bioq
->transition
= TAILQ_NEXT(bio
, bio_act
);
1380 * Bounds checking against the media size, used for the raw partition.
1381 * secsize, mediasize and b_blkno must all be the same units.
1382 * Possibly this has to be DEV_BSIZE (512).
1385 bounds_check_with_mediasize(struct bio
*bio
, int secsize
, uint64_t mediasize
)
1387 struct buf
*bp
= bio
->bio_buf
;
1390 sz
= howmany(bp
->b_bcount
, secsize
);
1392 if (bio
->bio_offset
/DEV_BSIZE
+ sz
> mediasize
) {
1393 sz
= mediasize
- bio
->bio_offset
/DEV_BSIZE
;
1395 /* If exactly at end of disk, return EOF. */
1396 bp
->b_resid
= bp
->b_bcount
;
1400 /* If past end of disk, return EINVAL. */
1401 bp
->b_error
= EINVAL
;
1404 /* Otherwise, truncate request. */
1405 bp
->b_bcount
= sz
* secsize
;
1412 * Disk error is the preface to plaintive error messages
1413 * about failing disk transfers. It prints messages of the form
1415 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
1417 * if the offset of the error in the transfer and a disk label
1418 * are both available. blkdone should be -1 if the position of the error
1419 * is unknown; the disklabel pointer may be null from drivers that have not
1420 * been converted to use them. The message is printed with kprintf
1421 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
1422 * The message should be completed (with at least a newline) with kprintf
1423 * or log(-1, ...), respectively. There is no trailing space.
1426 diskerr(struct bio
*bio
, cdev_t dev
, const char *what
, int pri
, int donecnt
)
1428 struct buf
*bp
= bio
->bio_buf
;
1442 kprintf("%s: %s %sing ", dev
->si_name
, what
, term
);
1443 kprintf("offset %012llx for %d",
1444 (long long)bio
->bio_offset
,
1448 kprintf(" (%d bytes completed)", donecnt
);
1452 * Locate a disk device
1455 disk_locate(const char *devname
)
1457 return devfs_find_device_by_name("%s", devname
);
1461 disk_config(void *arg
)
1463 disk_msg_send_sync(DISK_SYNC
, NULL
, NULL
);
1469 struct thread
* td_core
;
1471 disk_msg_cache
= objcache_create("disk-msg-cache", 0, 0,
1473 objcache_malloc_alloc
,
1474 objcache_malloc_free
,
1475 &disk_msg_malloc_args
);
1477 lwkt_token_init(&disklist_token
, "disks");
1480 * Initialize the reply-only port which acts as a message drain
1482 lwkt_initport_replyonly(&disk_dispose_port
, disk_msg_autofree_reply
);
1484 lwkt_gettoken(&disklist_token
);
1485 lwkt_create(disk_msg_core
, /*args*/NULL
, &td_core
, NULL
,
1486 0, -1, "disk_msg_core");
1487 tsleep(td_core
, 0, "diskcore", 0);
1488 lwkt_reltoken(&disklist_token
);
1494 objcache_destroy(disk_msg_cache
);
1498 * Clean out illegal characters in serial numbers.
1501 disk_cleanserial(char *serno
)
1505 while ((c
= *serno
) != 0) {
1506 if (c
>= 'a' && c
<= 'z')
1508 else if (c
>= 'A' && c
<= 'Z')
1510 else if (c
>= '0' && c
<= '9')
1512 else if (c
== '-' || c
== '@' || c
== '+' || c
== '.')
1520 TUNABLE_INT("kern.disk_debug", &disk_debug_enable
);
1521 SYSCTL_INT(_kern
, OID_AUTO
, disk_debug
, CTLFLAG_RW
, &disk_debug_enable
,
1522 0, "Enable subr_disk debugging");
1524 SYSINIT(disk_register
, SI_SUB_PRE_DRIVERS
, SI_ORDER_FIRST
, disk_init
, NULL
);
1525 SYSUNINIT(disk_register
, SI_SUB_PRE_DRIVERS
, SI_ORDER_ANY
, disk_uninit
, NULL
);