xref: /openbsd/share/man/man9/disk.9 (revision b4d7a29b)
1.\"	$OpenBSD: disk.9,v 1.17 2001/09/06 15:04:34 mpech 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 January 7, 1996
35.Dt DISK 9
36.Os
37.Sh NAME
38.Nm disk
39.Nd generic disk framework
40.Sh SYNOPSIS
41.Fd #include <sys/types.h>
42.Fd #include <sys/disklabel.h>
43.Fd #include <sys/disk.h>
44.Ft void
45.Fn disk_init "void"
46.Ft void
47.Fn disk_attach "struct disk *"
48.Ft void
49.Fn disk_detach "struct disk *"
50.Ft void
51.Fn disk_busy "struct disk *"
52.Ft void
53.Fn disk_unbusy "struct disk *"
54.Ft void
55.Fn disk_resetstat "struct disk *"
56.Ft struct disk *
57.Fn disk_find "char *"
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	char	 *dk_name;	/* disk name */
70	int	 dk_bopenmask;	/* block devices open */
71	int	 dk_copenmask;	/* character devices open */
72	int	 dk_openmask;	/* composite (bopen|copen) */
73	int	 dk_state;	/* label state */
74	int	 dk_blkshift;	/* shift to convert DEV_BSIZE to blks */
75	int	 dk_byteshift;	/* shift to convert bytes to blks */
76
77	/*
78	 * Metrics data; note that some metrics may have no meaning
79	 * on certain types of disks.
80	 */
81	int	  dk_busy;	/* busy counter */
82	u_int64_t dk_xfer;	/* total number of transfers */
83	u_int64_t dk_seek;	/* total independent seek operations */
84	u_int64_t dk_bytes;	/* total bytes transferred */
85	struct timeval	dk_attachtime;	/* time disk was attached */
86	struct timeval	dk_timestamp;	/* timestamp of last unbusy */
87	struct timeval	dk_time;	/* total time spent busy */
88
89	struct	dkdriver *dk_driver;	/* pointer to driver */
90
91	/*
92	 * Disk label information.  Storage for the in-core disk label
93	 * must be dynamically allocated, otherwise the size of this
94	 * structure becomes machine-dependent.
95	 */
96	daddr_t	 dk_labelsector;	/* sector containing label */
97	struct disklabel *dk_label;	/* label */
98	struct cpu_disklabel *dk_cpulabel;
99};
100.Ed
101.Pp
102The system maintains a global linked-list of all disks attached to the
103system.
104This list, called
105.Nm disklist ,
106may grow or shrink over time as disks are dynamically added and removed
107from the system.
108Drivers which currently make use of the detachment
109capability of the framework are the
110.Nm ccd
111and
112.Nm vnd
113pseudo-device drivers.
114.Pp
115The following is a brief description of each function in the framework:
116.Bl -tag -width "disk_resetstat()"
117.It Fn disk_init
118Initialize the disklist and other data structures used by the framework.
119Called by
120.Fn main
121before autoconfiguration.
122.It Fn disk_attach
123Attach a disk; allocate storage for the disklabel, set the
124.Dq attached time
125timestamp, insert the disk into the disklist, and increment the
126system disk count.
127.It Fn disk_detach
128Detach a disk; free storage for the disklabel, remove the disk
129from the disklist, and decrement the system disk count.
130If the count drops below zero, panic.
131.It Fn disk_busy
132Increment the disk's
133.Dq busy counter .
134If this counter goes from 0 to 1, set the timestamp corresponding to
135this transfer.
136.It Fn disk_unbusy
137Decrement a disk's busy counter.
138If the count drops below zero, print a warning message.
139Get the current time, subtract it from the disk's timestamp, and add
140the difference to the disk's running total.
141Set the disk's timestamp to the current time.
142If the provided byte count is greater than 0,
143add it to the disk's running total and increment the number of transfers
144performed by the disk.
145.It Fn disk_resetstat
146Reset the running byte, transfer, and time totals.
147.It Fn disk_find
148Return a pointer to the disk structure corresponding to the name provided, or
149.Dv NULL
150if the disk does not exist.
151.El
152.Pp
153The functions typically called by device drivers are
154.Fn disk_attach ,
155.Fn disk_detach ,
156.Fn disk_busy ,
157.Fn disk_unbusy ,
158and
159.Fn disk_resetstat .
160The function
161.Fn disk_find
162is provided as a utility function.
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 portions 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(parent, self, aux)
214	struct device *parent, *self;
215	void *aux;
216{
217	struct foo_softc *sc = (struct foo_softc *)self;
218	[ . . . ]
219
220	/* Initialize and attach the disk structure. */
221	sc->sc_dk.dk_driver = &foodkdriver;
222	sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
223	disk_attach(&sc->sc_dk);
224
225	/* Read geometry and fill in pertinent parts of disklabel. */
226	[ . . . ]
227}
228.Ed
229.Pp
230The
231.Nm foodkdriver
232above is the disk's
233.Dq driver
234switch.
235This switch currently includes a pointer to the disk's
236.Dq strategy
237routine.
238This switch needs to have global scope and should be initialized as follows:
239.Bd -literal
240void	foostrategy __P((struct buf *));
241struct	dkdriver foodkdriver = { foostrategy };
242.Ed
243.Pp
244Once the disk is attached, metrics may be gathered on that disk.
245In order to gather metrics data, the driver must tell the framework
246when the disk starts and stops operations.
247This functionality is provided by the
248.Fn disk_busy
249and
250.Fn disk_unbusy
251routines.
252The
253.Fn disk_busy
254routine should be called immediately before a command to the disk is
255sent, e.g.:
256.Bd -literal
257void
258foostart(sc)
259	struct foo_softc *sc;
260{
261	[ . . . ]
262
263	/* Get buffer from drive's transfer queue. */
264	[ . . . ]
265
266	/* Build command to send to drive. */
267	[ . . . ]
268
269	/* Tell the disk framework we're going busy. */
270	disk_busy(&sc->sc_dk);
271
272	/* Send command to the drive. */
273	[ . . . ]
274}
275.Ed
276.Pp
277When
278.Fn disk_busy
279is called, a timestamp is taken if the disk's busy counter moves from
2800 to 1, indicating the disk has gone from an idle to non-idle state.
281Note that
282.Fn disk_busy
283must be called at
284.Fn splbio .
285At the end of a transaction, the
286.Fn disk_unbusy
287routine should be called.
288This routine performs some consistency checks,
289such as ensuring that the calls to
290.Fn disk_busy
291and
292.Fn disk_unbusy
293are balanced.
294This routine also performs the actual metrics calculation.
295A timestamp is taken, and the difference from the timestamp taken in
296.Fn disk_busy
297is added to the disk's total running time.
298The disk's timestamp is then
299updated in case there is more than one pending transfer on the disk.
300A byte count is also added to the disk's running total, and if greater than
301zero, the number of transfers the disk has performed is incremented.
302.Bd -literal
303void
304foodone(xfer)
305	struct foo_xfer *xfer;
306{
307	struct foo_softc = (struct foo_softc *)xfer->xf_softc;
308	struct buf *bp = xfer->xf_buf;
309	long nbytes;
310	[ . . . ]
311
312	/*
313	 * Get number of bytes transferred.  If there is no buf
314	 * associated with the xfer, we are being called at the
315	 * end of a non-I/O command.
316	 */
317	if (bp == NULL)
318		nbytes = 0;
319	else
320		nbytes = bp->b_bcount - bp->b_resid;
321
322	[ . . . ]
323
324	/* Notify the disk framework that we've completed the transfer. */
325	disk_unbusy(&sc->sc_dk, nbytes);
326
327	[ . . . ]
328}
329.Ed
330.Pp
331Like
332.Fn disk_busy ,
333.Fn disk_unbusy
334must be called at
335.Fn splbio .
336.Pp
337At some point a driver may wish to reset the metrics data gathered on a
338particular disk.
339For this function, the
340.Fn disk_resetstat
341routine is provided.
342.Sh CODE REFERENCES
343The disk framework itself is implemented within the file
344.Pa sys/kern/subr_disk.c .
345Data structures and function prototypes for the framework are located in
346.Pa sys/sys/disk.h .
347.Pp
348The
349.Ox
350machine-independent SCSI disk and CD-ROM drivers utilize the disk framework.
351They are located in
352.Pa sys/scsi/sd.c
353and
354.Pa sys/scsi/cd.c .
355.Pp
356The
357.Ox
358.Nm ccd ,
359.Nm raid ,
360and
361.Nm vnd
362drivers utilize the detachment capability of the framework.
363They are located in
364.Pa sys/dev/ccd.c ,
365.Pa sys/dev/raidframe/ ,
366and
367.Pa sys/dev/vnd.c .
368.Sh AUTHORS
369The
370.Ox
371generic disk framework was architected and implemented within
372.Nx
373by Jason R. Thorpe <thorpej@NetBSD.ORG>.
374.Sh SEE ALSO
375.Xr ccd 4 ,
376.Xr raid 4 ,
377.Xr vnd 4 ,
378.Xr spl 9
379.Sh HISTORY
380The
381.Ox
382generic disk framework first appeared in
383.Nx 1.2 .
384