1.\" $OpenBSD: disk.9,v 1.35 2022/09/07 05:36:59 jsg Exp $ 2.\" $NetBSD: disk.9,v 1.2 1996/04/08 20:41:25 jtc Exp $ 3.\" 4.\" Copyright (c) 1995, 1996 Jason R. Thorpe. 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed for the NetBSD Project 18.\" by Jason R. Thorpe. 19.\" 4. The name of the author may not be used to endorse or promote products 20.\" derived from this software without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.Dd $Mdocdate: September 7 2022 $ 35.Dt DISK_INIT 9 36.Os 37.Sh NAME 38.Nm disk_init , 39.Nm disk_attach , 40.Nm disk_detach , 41.Nm disk_busy , 42.Nm disk_unbusy 43.Nd generic disk framework 44.Sh SYNOPSIS 45.In sys/types.h 46.In sys/disklabel.h 47.In sys/disk.h 48.Ft void 49.Fn disk_init "void" 50.Ft void 51.Fn disk_attach "struct disk *" 52.Ft void 53.Fn disk_detach "struct disk *" 54.Ft void 55.Fn disk_busy "struct disk *" 56.Ft void 57.Fn disk_unbusy "struct disk *" "long bcount" "int read" 58.Sh DESCRIPTION 59The 60.Ox 61generic disk framework is designed to provide flexible, 62scalable, and consistent handling of disk state and metrics information. 63The fundamental component of this framework is the 64.Nm 65structure, which is defined as follows: 66.Bd -literal 67struct disk { 68 TAILQ_ENTRY(disk) dk_link; /* link in global disklist */ 69 struct rwlock dk_lock; /* disk lock */ 70 struct mutex dk_mtx; /* busy/unbusy mtx */ 71 char *dk_name; /* disk name */ 72 struct device *dk_device; /* disk device structure. */ 73 dev_t dk_devno; /* disk device number. */ 74 int dk_flags; /* disk flags */ 75#define DKF_CONSTRUCTED 0x0001 76#define DKF_OPENED 0x0002 77#define DKF_NOLABELREAD 0x0004 78 79 /* 80 * Metrics data; note that some metrics may have no meaning 81 * on certain types of disks. 82 */ 83 int dk_busy; /* busy counter */ 84 u_int64_t dk_rxfer; /* total number of read transfers */ 85 u_int64_t dk_wxfer; /* total number of write transfers */ 86 u_int64_t dk_seek; /* total independent seek operations */ 87 u_int64_t dk_rbytes; /* total bytes read */ 88 u_int64_t dk_wbytes; /* total bytes written */ 89 struct timeval dk_attachtime; /* time disk was attached */ 90 struct timeval dk_timestamp; /*time of first busy or any unbusy*/ 91 struct timeval dk_time; /* total time spent busy */ 92 93 int dk_bopenmask; /* block devices open */ 94 int dk_copenmask; /* character devices open */ 95 int dk_openmask; /* composite (bopen|copen) */ 96 int dk_state; /* label state ### */ 97 int dk_blkshift; /*shift to convert DEV_BSIZE to blks*/ 98 int dk_byteshift; /* shift to convert bytes to blks */ 99 100 /* 101 * Disk label information. Storage for the in-core disk label 102 * must be dynamically allocated, otherwise the size of this 103 * structure becomes machine-dependent. 104 */ 105 struct disklabel *dk_label; 106}; 107.Ed 108.Pp 109The system maintains a global linked-list of all disks attached to the 110system. 111This list, called 112.Nm disklist , 113may grow or shrink over time as disks are dynamically added and removed 114from the system. 115An example of a driver which currently makes use of the detachment 116capability of the framework is the 117.Xr vnd 4 118pseudo-device driver. 119.Pp 120The following is a brief description of each function in the framework: 121.Bl -tag -width "disk_unbusy()" 122.It Fn disk_init 123Initialize the disklist and other data structures used by the framework. 124Called by 125.Fn main 126before autoconfiguration. 127.It Fn disk_attach 128Attach a disk; allocate storage for the disklabel, set the 129.Dq attached time 130timestamp, insert the disk into the disklist, and increment the 131system disk count. 132.It Fn disk_detach 133Detach a disk; free storage for the disklabel, remove the disk 134from the disklist, and decrement the system disk count. 135If the count drops below zero, panic. 136.It Fn disk_busy 137Increment the disk's 138.Dq busy counter . 139If this counter goes from 0 to 1, set the timestamp corresponding to 140this transfer. 141.It Fn disk_unbusy 142Decrement a disk's busy counter. 143If the count drops below zero, print a warning message. 144Get the current time, subtract it from the disk's timestamp, and add 145the difference to the disk's running total. 146Set the disk's timestamp to the current time. 147If the provided byte count is greater than 0, 148add it to the disk's running total and increment the number of transfers 149performed by the disk. 150The third argument 151.Ar read 152specifies the direction of I/O; 153if non-zero it means reading from the disk, 154otherwise it means writing to the disk. 155.El 156.Pp 157The functions typically called by device drivers are 158.Fn disk_attach , 159.Fn disk_detach , 160.Fn disk_busy 161and 162.Fn disk_unbusy . 163.Sh USING THE FRAMEWORK 164This section includes a description on basic use of the framework 165and example usage of its functions. 166Actual implementation of 167a device driver which utilizes the framework may vary. 168.Pp 169A special routine, 170.Fn disk_init , 171is provided to perform basic initialization of data structures used by 172the framework. 173It is called exactly once by the system, in 174.Fn main , 175before device autoconfiguration. 176.Pp 177Each device in the system uses a 178.Dq softc 179structure which contains autoconfiguration and state information for that 180device. 181In the case of disks, the softc should also contain one instance 182of the disk structure, e.g.: 183.Bd -literal 184struct foo_softc { 185 struct device *sc_dev; /* generic device information */ 186 struct disk *sc_dk; /* generic disk information */ 187 [ . . . more . . . ] 188}; 189.Ed 190.Pp 191In order for the system to gather metrics data about a disk, the disk must 192be registered with the system. 193The 194.Fn disk_attach 195routine performs all of the functions currently required to register a disk 196with the system including allocation of disklabel storage space, 197recording of the time since boot that the disk was attached, and insertion 198into the disklist. 199Note that since this function allocates storage space 200for the disklabel, it must be called before the disklabel is read from the 201media or used in any other way. 202Before 203.Fn disk_attach 204is called, a portion of the disk structure must be initialized with 205data specific to that disk. 206For example, in the 207.Dq foo 208disk driver, the following would be performed in the autoconfiguration 209.Dq attach 210routine: 211.Bd -literal 212void 213fooattach(struct device *parent, struct device *self, void *aux) 214{ 215 struct foo_softc *sc = (struct foo_softc *)self; 216 [ . . . ] 217 218 /* Initialize and attach the disk structure. */ 219 sc->sc_dk.dk_driver = &foodkdriver; 220 sc->sc_dk.dk_name = sc->sc_dev.dv_xname; 221 disk_attach(&sc->sc_dk); 222 223 /* Read geometry and fill in pertinent parts of disklabel. */ 224 [ . . . ] 225} 226.Ed 227.Pp 228The 229.Nm foodkdriver 230above is the disk's 231.Dq driver 232switch. 233This switch currently includes a pointer to the disk's 234.Dq strategy 235routine. 236This switch needs to have global scope and should be initialized as follows: 237.Bd -literal 238void foostrategy(struct buf *); 239struct dkdriver foodkdriver = { foostrategy }; 240.Ed 241.Pp 242Once the disk is attached, metrics may be gathered on that disk. 243In order to gather metrics data, the driver must tell the framework 244when the disk starts and stops operations. 245This functionality is provided by the 246.Fn disk_busy 247and 248.Fn disk_unbusy 249routines. 250The 251.Fn disk_busy 252routine should be called immediately before a command to the disk is 253sent, e.g.: 254.Bd -literal 255void 256foostart(struct foo_softc *sc) 257{ 258 [ . . . ] 259 260 /* Get buffer from drive's transfer queue. */ 261 [ . . . ] 262 263 /* Build command to send to drive. */ 264 [ . . . ] 265 266 /* Tell the disk framework we're going busy. */ 267 disk_busy(&sc->sc_dk); 268 269 /* Send command to the drive. */ 270 [ . . . ] 271} 272.Ed 273.Pp 274When 275.Fn disk_busy 276is called, a timestamp is taken if the disk's busy counter moves from 2770 to 1, indicating the disk has gone from an idle to non-idle state. 278Note that 279.Fn disk_busy 280must be called at 281.Fn splbio . 282At the end of a transaction, the 283.Fn disk_unbusy 284routine should be called. 285This routine performs some consistency checks, 286such as ensuring that the calls to 287.Fn disk_busy 288and 289.Fn disk_unbusy 290are balanced. 291This routine also performs the actual metrics calculation. 292A timestamp is taken, and the difference from the timestamp taken in 293.Fn disk_busy 294is added to the disk's total running time. 295The disk's timestamp is then 296updated in case there is more than one pending transfer on the disk. 297A byte count is also added to the disk's running total, and if greater than 298zero, the number of transfers the disk has performed is incremented. 299.Bd -literal 300void 301foodone(struct foo_xfer *xfer) 302{ 303 struct foo_softc = (struct foo_softc *)xfer->xf_softc; 304 struct buf *bp = xfer->xf_buf; 305 long nbytes; 306 [ . . . ] 307 308 /* 309 * Get number of bytes transferred. If there is no buf 310 * associated with the xfer, we are being called at the 311 * end of a non-I/O command. 312 */ 313 if (bp == NULL) 314 nbytes = 0; 315 else 316 nbytes = bp->b_bcount - bp->b_resid; 317 318 [ . . . ] 319 320 /* Notify the disk framework that we've completed the transfer. */ 321 disk_unbusy(&sc->sc_dk, nbytes); 322 323 [ . . . ] 324} 325.Ed 326.Pp 327Like 328.Fn disk_busy , 329.Fn disk_unbusy 330must be called at 331.Fn splbio . 332.Sh CODE REFERENCES 333The disk framework itself is implemented within the file 334.Pa sys/kern/subr_disk.c . 335Data structures and function prototypes for the framework are located in 336.Pa sys/sys/disk.h . 337.Pp 338The 339.Ox 340machine-independent SCSI disk and CD-ROM drivers utilize the disk framework. 341They are located in 342.Pa sys/scsi/sd.c 343and 344.Pa sys/scsi/cd.c . 345.Pp 346The 347.Ox 348.Xr vnd 4 349driver utilizes the detachment capability of the framework. 350This is located in 351.Pa sys/dev/vnd.c . 352.Sh SEE ALSO 353.Xr vnd 4 , 354.Xr spl 9 355.Sh HISTORY 356The 357.Ox 358generic disk framework first appeared in 359.Nx 1.2 . 360.Sh AUTHORS 361The 362.Ox 363generic disk framework was architected and implemented within 364.Nx 365by 366.An Jason R. Thorpe Aq Mt thorpej@NetBSD.ORG . 367