1*bc843134Sjsg.\" $OpenBSD: disk.9,v 1.35 2022/09/07 05:36:59 jsg Exp $ 2cc10a18aSespie.\" $NetBSD: disk.9,v 1.2 1996/04/08 20:41:25 jtc Exp $ 323005153Sderaadt.\" 423005153Sderaadt.\" Copyright (c) 1995, 1996 Jason R. Thorpe. 523005153Sderaadt.\" All rights reserved. 623005153Sderaadt.\" 723005153Sderaadt.\" Redistribution and use in source and binary forms, with or without 823005153Sderaadt.\" modification, are permitted provided that the following conditions 923005153Sderaadt.\" are met: 1023005153Sderaadt.\" 1. Redistributions of source code must retain the above copyright 1123005153Sderaadt.\" notice, this list of conditions and the following disclaimer. 1223005153Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright 1323005153Sderaadt.\" notice, this list of conditions and the following disclaimer in the 1423005153Sderaadt.\" documentation and/or other materials provided with the distribution. 1523005153Sderaadt.\" 3. All advertising materials mentioning features or use of this software 1623005153Sderaadt.\" must display the following acknowledgement: 1723005153Sderaadt.\" This product includes software developed for the NetBSD Project 1823005153Sderaadt.\" by Jason R. Thorpe. 1923005153Sderaadt.\" 4. The name of the author may not be used to endorse or promote products 2023005153Sderaadt.\" derived from this software without specific prior written permission. 2123005153Sderaadt.\" 2223005153Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2323005153Sderaadt.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2423005153Sderaadt.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2523005153Sderaadt.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2623005153Sderaadt.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2723005153Sderaadt.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 2823005153Sderaadt.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2923005153Sderaadt.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 3023005153Sderaadt.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3123005153Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3223005153Sderaadt.\" SUCH DAMAGE. 3323005153Sderaadt.\" 34*bc843134Sjsg.Dd $Mdocdate: September 7 2022 $ 3533378d91Sjmc.Dt DISK_INIT 9 36fc8533a3Saaron.Os 3723005153Sderaadt.Sh NAME 3833378d91Sjmc.Nm disk_init , 3933378d91Sjmc.Nm disk_attach , 4033378d91Sjmc.Nm disk_detach , 4133378d91Sjmc.Nm disk_busy , 4233378d91Sjmc.Nm disk_unbusy 4323005153Sderaadt.Nd generic disk framework 4423005153Sderaadt.Sh SYNOPSIS 45dddd2645Sschwarze.In sys/types.h 46dddd2645Sschwarze.In sys/disklabel.h 47dddd2645Sschwarze.In sys/disk.h 4823005153Sderaadt.Ft void 4923005153Sderaadt.Fn disk_init "void" 5023005153Sderaadt.Ft void 5123005153Sderaadt.Fn disk_attach "struct disk *" 5223005153Sderaadt.Ft void 53261f581cSmickey.Fn disk_detach "struct disk *" 5423005153Sderaadt.Ft void 5523005153Sderaadt.Fn disk_busy "struct disk *" 5623005153Sderaadt.Ft void 571df77410Smillert.Fn disk_unbusy "struct disk *" "long bcount" "int read" 5823005153Sderaadt.Sh DESCRIPTION 59657686c6SespieThe 60cc10a18aSespie.Ox 61657686c6Sespiegeneric disk framework is designed to provide flexible, 6223005153Sderaadtscalable, and consistent handling of disk state and metrics information. 6323005153SderaadtThe fundamental component of this framework is the 64cc10a18aSespie.Nm 6523005153Sderaadtstructure, which is defined as follows: 6623005153Sderaadt.Bd -literal 6723005153Sderaadtstruct disk { 6823005153Sderaadt TAILQ_ENTRY(disk) dk_link; /* link in global disklist */ 69c9ff0b05Sjmc struct rwlock dk_lock; /* disk lock */ 70e20f3f15Skrw struct mutex dk_mtx; /* busy/unbusy mtx */ 7123005153Sderaadt char *dk_name; /* disk name */ 72e20f3f15Skrw struct device *dk_device; /* disk device structure. */ 73e20f3f15Skrw dev_t dk_devno; /* disk device number. */ 74c9ff0b05Sjmc int dk_flags; /* disk flags */ 75c9ff0b05Sjmc#define DKF_CONSTRUCTED 0x0001 76771f3500Sjmc#define DKF_OPENED 0x0002 77771f3500Sjmc#define DKF_NOLABELREAD 0x0004 7823005153Sderaadt 7923005153Sderaadt /* 8023005153Sderaadt * Metrics data; note that some metrics may have no meaning 8123005153Sderaadt * on certain types of disks. 8223005153Sderaadt */ 8323005153Sderaadt int dk_busy; /* busy counter */ 84c9ff0b05Sjmc u_int64_t dk_rxfer; /* total number of read transfers */ 85c9ff0b05Sjmc u_int64_t dk_wxfer; /* total number of write transfers */ 8623005153Sderaadt u_int64_t dk_seek; /* total independent seek operations */ 87c9ff0b05Sjmc u_int64_t dk_rbytes; /* total bytes read */ 88c9ff0b05Sjmc u_int64_t dk_wbytes; /* total bytes written */ 8923005153Sderaadt struct timeval dk_attachtime; /* time disk was attached */ 904cd1a496Sbluhm struct timeval dk_timestamp; /*time of first busy or any unbusy*/ 9123005153Sderaadt struct timeval dk_time; /* total time spent busy */ 9223005153Sderaadt 93c9ff0b05Sjmc int dk_bopenmask; /* block devices open */ 94c9ff0b05Sjmc int dk_copenmask; /* character devices open */ 95c9ff0b05Sjmc int dk_openmask; /* composite (bopen|copen) */ 96c9ff0b05Sjmc int dk_state; /* label state ### */ 97c9ff0b05Sjmc int dk_blkshift; /*shift to convert DEV_BSIZE to blks*/ 98c9ff0b05Sjmc int dk_byteshift; /* shift to convert bytes to blks */ 99c9ff0b05Sjmc 10023005153Sderaadt /* 10123005153Sderaadt * Disk label information. Storage for the in-core disk label 10223005153Sderaadt * must be dynamically allocated, otherwise the size of this 10323005153Sderaadt * structure becomes machine-dependent. 10423005153Sderaadt */ 105e20f3f15Skrw struct disklabel *dk_label; 10623005153Sderaadt}; 10723005153Sderaadt.Ed 10823005153Sderaadt.Pp 10923005153SderaadtThe system maintains a global linked-list of all disks attached to the 110ab967380Saaronsystem. 111ab967380SaaronThis list, called 11223005153Sderaadt.Nm disklist , 11323005153Sderaadtmay grow or shrink over time as disks are dynamically added and removed 114ab967380Saaronfrom the system. 115ba2212f3SjmcAn example of a driver which currently makes use of the detachment 116ba2212f3Sjmccapability of the framework is the 11718280b74Sjmc.Xr vnd 4 118ba2212f3Sjmcpseudo-device driver. 11923005153Sderaadt.Pp 12023005153SderaadtThe following is a brief description of each function in the framework: 12153fae765Sart.Bl -tag -width "disk_unbusy()" 12223005153Sderaadt.It Fn disk_init 12323005153SderaadtInitialize the disklist and other data structures used by the framework. 12423005153SderaadtCalled by 12523005153Sderaadt.Fn main 12623005153Sderaadtbefore autoconfiguration. 12723005153Sderaadt.It Fn disk_attach 12823005153SderaadtAttach a disk; allocate storage for the disklabel, set the 12923005153Sderaadt.Dq attached time 130962020bbSavsmtimestamp, insert the disk into the disklist, and increment the 13123005153Sderaadtsystem disk count. 132261f581cSmickey.It Fn disk_detach 133962020bbSavsmDetach a disk; free storage for the disklabel, remove the disk 134ab967380Saaronfrom the disklist, and decrement the system disk count. 135ab967380SaaronIf the count drops below zero, panic. 13623005153Sderaadt.It Fn disk_busy 13723005153SderaadtIncrement the disk's 13823005153Sderaadt.Dq busy counter . 13923005153SderaadtIf this counter goes from 0 to 1, set the timestamp corresponding to 14023005153Sderaadtthis transfer. 14123005153Sderaadt.It Fn disk_unbusy 142ab967380SaaronDecrement a disk's busy counter. 143ab967380SaaronIf the count drops below zero, print a warning message. 14423005153SderaadtGet the current time, subtract it from the disk's timestamp, and add 145ab967380Saaronthe difference to the disk's running total. 146ab967380SaaronSet the disk's timestamp to the current time. 147ab967380SaaronIf the provided byte count is greater than 0, 14823005153Sderaadtadd it to the disk's running total and increment the number of transfers 14923005153Sderaadtperformed by the disk. 1501df77410SmillertThe third argument 1511df77410Smillert.Ar read 1521df77410Smillertspecifies the direction of I/O; 1531df77410Smillertif non-zero it means reading from the disk, 1541df77410Smillertotherwise it means writing to the disk. 15523005153Sderaadt.El 15623005153Sderaadt.Pp 15723005153SderaadtThe functions typically called by device drivers are 15823005153Sderaadt.Fn disk_attach , 159261f581cSmickey.Fn disk_detach , 16053fae765Sart.Fn disk_busy 16123005153Sderaadtand 16253fae765Sart.Fn disk_unbusy . 16323005153Sderaadt.Sh USING THE FRAMEWORK 16423005153SderaadtThis section includes a description on basic use of the framework 165ab967380Saaronand example usage of its functions. 166ab967380SaaronActual implementation of 16723005153Sderaadta device driver which utilizes the framework may vary. 16823005153Sderaadt.Pp 16923005153SderaadtA special routine, 17023005153Sderaadt.Fn disk_init , 17123005153Sderaadtis provided to perform basic initialization of data structures used by 172ab967380Saaronthe framework. 173ab967380SaaronIt is called exactly once by the system, in 17423005153Sderaadt.Fn main , 17523005153Sderaadtbefore device autoconfiguration. 17623005153Sderaadt.Pp 17723005153SderaadtEach device in the system uses a 17823005153Sderaadt.Dq softc 17923005153Sderaadtstructure which contains autoconfiguration and state information for that 180ab967380Saarondevice. 181ab967380SaaronIn the case of disks, the softc should also contain one instance 182962020bbSavsmof the disk structure, e.g.: 18323005153Sderaadt.Bd -literal 18423005153Sderaadtstruct foo_softc { 18523005153Sderaadt struct device *sc_dev; /* generic device information */ 186cc10a18aSespie struct disk *sc_dk; /* generic disk information */ 18723005153Sderaadt [ . . . more . . . ] 18823005153Sderaadt}; 18923005153Sderaadt.Ed 19023005153Sderaadt.Pp 19123005153SderaadtIn order for the system to gather metrics data about a disk, the disk must 192ab967380Saaronbe registered with the system. 193ab967380SaaronThe 19423005153Sderaadt.Fn disk_attach 19523005153Sderaadtroutine performs all of the functions currently required to register a disk 19623005153Sderaadtwith the system including allocation of disklabel storage space, 19723005153Sderaadtrecording of the time since boot that the disk was attached, and insertion 198ab967380Saaroninto the disklist. 199ab967380SaaronNote that since this function allocates storage space 20023005153Sderaadtfor the disklabel, it must be called before the disklabel is read from the 201ab967380Saaronmedia or used in any other way. 202ab967380SaaronBefore 20323005153Sderaadt.Fn disk_attach 204163baf3aSmillertis called, a portion of the disk structure must be initialized with 205ab967380Saarondata specific to that disk. 206ab967380SaaronFor example, in the 20723005153Sderaadt.Dq foo 20823005153Sderaadtdisk driver, the following would be performed in the autoconfiguration 20923005153Sderaadt.Dq attach 21023005153Sderaadtroutine: 21123005153Sderaadt.Bd -literal 21223005153Sderaadtvoid 213*bc843134Sjsgfooattach(struct device *parent, struct device *self, void *aux) 21423005153Sderaadt{ 21523005153Sderaadt struct foo_softc *sc = (struct foo_softc *)self; 21623005153Sderaadt [ . . . ] 21723005153Sderaadt 21823005153Sderaadt /* Initialize and attach the disk structure. */ 21923005153Sderaadt sc->sc_dk.dk_driver = &foodkdriver; 22023005153Sderaadt sc->sc_dk.dk_name = sc->sc_dev.dv_xname; 22123005153Sderaadt disk_attach(&sc->sc_dk); 22223005153Sderaadt 22323005153Sderaadt /* Read geometry and fill in pertinent parts of disklabel. */ 22423005153Sderaadt [ . . . ] 22523005153Sderaadt} 22623005153Sderaadt.Ed 22723005153Sderaadt.Pp 22823005153SderaadtThe 22923005153Sderaadt.Nm foodkdriver 23023005153Sderaadtabove is the disk's 23123005153Sderaadt.Dq driver 232ab967380Saaronswitch. 233ab967380SaaronThis switch currently includes a pointer to the disk's 23423005153Sderaadt.Dq strategy 235ab967380Saaronroutine. 236962020bbSavsmThis switch needs to have global scope and should be initialized as follows: 23723005153Sderaadt.Bd -literal 238c72b5b24Smillertvoid foostrategy(struct buf *); 23923005153Sderaadtstruct dkdriver foodkdriver = { foostrategy }; 24023005153Sderaadt.Ed 24123005153Sderaadt.Pp 242ab967380SaaronOnce the disk is attached, metrics may be gathered on that disk. 243ab967380SaaronIn order to gather metrics data, the driver must tell the framework 244ab967380Saaronwhen the disk starts and stops operations. 245ab967380SaaronThis functionality is provided by the 24623005153Sderaadt.Fn disk_busy 24723005153Sderaadtand 24823005153Sderaadt.Fn disk_unbusy 249ab967380Saaronroutines. 250ab967380SaaronThe 25123005153Sderaadt.Fn disk_busy 25223005153Sderaadtroutine should be called immediately before a command to the disk is 253ab967380Saaronsent, e.g.: 25423005153Sderaadt.Bd -literal 25523005153Sderaadtvoid 256*bc843134Sjsgfoostart(struct foo_softc *sc) 25723005153Sderaadt{ 25823005153Sderaadt [ . . . ] 25923005153Sderaadt 26023005153Sderaadt /* Get buffer from drive's transfer queue. */ 26123005153Sderaadt [ . . . ] 26223005153Sderaadt 26323005153Sderaadt /* Build command to send to drive. */ 26423005153Sderaadt [ . . . ] 26523005153Sderaadt 266ad3614a8Sderaadt /* Tell the disk framework we're going busy. */ 26723005153Sderaadt disk_busy(&sc->sc_dk); 26823005153Sderaadt 26923005153Sderaadt /* Send command to the drive. */ 27023005153Sderaadt [ . . . ] 27123005153Sderaadt} 27223005153Sderaadt.Ed 27323005153Sderaadt.Pp 27423005153SderaadtWhen 27523005153Sderaadt.Fn disk_busy 27623005153Sderaadtis called, a timestamp is taken if the disk's busy counter moves from 27723005153Sderaadt0 to 1, indicating the disk has gone from an idle to non-idle state. 27823005153SderaadtNote that 27923005153Sderaadt.Fn disk_busy 28023005153Sderaadtmust be called at 28123005153Sderaadt.Fn splbio . 28223005153SderaadtAt the end of a transaction, the 28323005153Sderaadt.Fn disk_unbusy 284ab967380Saaronroutine should be called. 285ab967380SaaronThis routine performs some consistency checks, 28623005153Sderaadtsuch as ensuring that the calls to 28723005153Sderaadt.Fn disk_busy 28823005153Sderaadtand 28923005153Sderaadt.Fn disk_unbusy 290ab967380Saaronare balanced. 291ab967380SaaronThis routine also performs the actual metrics calculation. 29223005153SderaadtA timestamp is taken, and the difference from the timestamp taken in 29323005153Sderaadt.Fn disk_busy 294ab967380Saaronis added to the disk's total running time. 295ab967380SaaronThe disk's timestamp is then 29623005153Sderaadtupdated in case there is more than one pending transfer on the disk. 29723005153SderaadtA byte count is also added to the disk's running total, and if greater than 29823005153Sderaadtzero, the number of transfers the disk has performed is incremented. 29923005153Sderaadt.Bd -literal 30023005153Sderaadtvoid 301*bc843134Sjsgfoodone(struct foo_xfer *xfer) 30223005153Sderaadt{ 30323005153Sderaadt struct foo_softc = (struct foo_softc *)xfer->xf_softc; 30423005153Sderaadt struct buf *bp = xfer->xf_buf; 30523005153Sderaadt long nbytes; 30623005153Sderaadt [ . . . ] 30723005153Sderaadt 30823005153Sderaadt /* 309cc10a18aSespie * Get number of bytes transferred. If there is no buf 31023005153Sderaadt * associated with the xfer, we are being called at the 31123005153Sderaadt * end of a non-I/O command. 31223005153Sderaadt */ 31323005153Sderaadt if (bp == NULL) 31423005153Sderaadt nbytes = 0; 31523005153Sderaadt else 31623005153Sderaadt nbytes = bp->b_bcount - bp->b_resid; 31723005153Sderaadt 31823005153Sderaadt [ . . . ] 31923005153Sderaadt 32023005153Sderaadt /* Notify the disk framework that we've completed the transfer. */ 32123005153Sderaadt disk_unbusy(&sc->sc_dk, nbytes); 32223005153Sderaadt 32323005153Sderaadt [ . . . ] 32423005153Sderaadt} 32523005153Sderaadt.Ed 32623005153Sderaadt.Pp 32723005153SderaadtLike 32823005153Sderaadt.Fn disk_busy , 32923005153Sderaadt.Fn disk_unbusy 33023005153Sderaadtmust be called at 33123005153Sderaadt.Fn splbio . 33223005153Sderaadt.Sh CODE REFERENCES 33323005153SderaadtThe disk framework itself is implemented within the file 334657686c6Sespie.Pa sys/kern/subr_disk.c . 33523005153SderaadtData structures and function prototypes for the framework are located in 336657686c6Sespie.Pa sys/sys/disk.h . 33723005153Sderaadt.Pp 338657686c6SespieThe 339cc10a18aSespie.Ox 340ab967380Saaronmachine-independent SCSI disk and CD-ROM drivers utilize the disk framework. 341ab967380SaaronThey are located in 342657686c6Sespie.Pa sys/scsi/sd.c 34323005153Sderaadtand 344657686c6Sespie.Pa sys/scsi/cd.c . 34523005153Sderaadt.Pp 346657686c6SespieThe 347cc10a18aSespie.Ox 34818280b74Sjmc.Xr vnd 4 349ad711cadSjsingdriver utilizes the detachment capability of the framework. 350ad711cadSjsingThis is located in 351657686c6Sespie.Pa sys/dev/vnd.c . 35223005153Sderaadt.Sh SEE ALSO 35323005153Sderaadt.Xr vnd 4 , 354b4d7a29bSmpech.Xr spl 9 35523005153Sderaadt.Sh HISTORY 356657686c6SespieThe 357cc10a18aSespie.Ox 358cc10a18aSespiegeneric disk framework first appeared in 3597b7ac515Smickey.Nx 1.2 . 360601d79cdSjmc.Sh AUTHORS 361601d79cdSjmcThe 362601d79cdSjmc.Ox 363601d79cdSjmcgeneric disk framework was architected and implemented within 364601d79cdSjmc.Nx 365601d79cdSjmcby 366f0641c22Sschwarze.An Jason R. Thorpe Aq Mt thorpej@NetBSD.ORG . 367