Data Structures for Drivers buf(9S)
NAME
buf - block I/O data transfer structure
SYNOPSIS
#include
#include
INTERFACE LEVEL
Architecture independent level 1 (DI/DKI)
DESCRIPTION
The buf structure is the basic data structure for block I/O
transfers. Each block I/O transfer has an associated buffer
header. The header contains all the buffer control and
status information. For drivers, the buffer header pointer
is the sole argument to a block driver strategy(9E) routine.
Do not depend on the size of the buf structure when writing
a driver.
A buffer header can be linked in multiple lists simultane-
ously. Because of this, most of the members in the buffer
header cannot be changed by the driver, even when the buffer
header is in one of the driver's work lists.
Buffer headers are also used by the system for unbuffered or
physical I/O for block drivers. In this case, the buffer
describes a portion of user data space that is locked into
memory.
Block drivers often chain block requests so that overall
throughput for the device is maximized. The avforw and the
avback members of the buf structure can serve as link
pointers for chaining block requests.
STRUCTURE MEMBERS
int bflags; /* Buffer status */
struct buf *avforw; /* Driver work list link */
struct buf *avback; /* Driver work list link */
sizet bbcount; /* # of bytes to transfer */
union {
caddrt baddr; /* Buffer's virtual address */
} bun;
daddrt bblkno; /* Block number on device */
diskaddrt blblkno; /* Expanded block number on dev. */
sizet bresid; /* # of bytes not xferred */
sizet bbufsize; /* size of alloc. buffer */
int (*biodone)(struct buf *); /* function called */
/* by biodone */
SunOS 5.11 Last change: 19 Sep 2002 1
Data Structures for Drivers buf(9S)
int berror; /* expanded error field */
void *bprivate; /* "opaque" driver private area */
devt bedev; /* expanded dev field */
The members of the buffer header available to test or set by
a driver are as follows:
bflags stores the buffer status and indicates to the driver
whether to read or write to the device. The driver must
never clear the bflags member. If this is done, unpredict-
able results can occur including loss of disk sanity and the
possible failure of other kernel processes.
All bflags bit values not otherwise specified above are
reserved by the kernel and may not be used.
Valid flags are as follows:
BUSY Indicates the buffer is in use. The driver must
not change this flag unless it allocated the
buffer with getrbuf(9F) and no I/O operation is
in progress.
BDONE Indicates the data transfer has completed. This
flag is read-only.
BEROR Indicates an I/O transfer error. It is set in
conjunction with the berror field.
bioerror(9F) should be used in preference to
setting the BEROR bit.
BPAGEIO Indicates the buffer is being used in a paged
I/O request. See the description of the
bun.baddr field for more information. This
flag is read-only.
BPHYS indicates the buffer header is being used for
physical (direct) I/O to a user data area. See
the description of the bun.baddr field for
more information. This flag is read-only.
SunOS 5.11 Last change: 19 Sep 2002 2
Data Structures for Drivers buf(9S)
BREAD Indicates that data is to be read from the
peripheral device into main memory.
BWRITE Indicates that the data is to be transferred
from main memory to the peripheral device.
BWRITE is a pseudo flag and cannot be directly
tested; it is only detected as the NOT form of
BREAD.
avforw and avback can be used by the driver to link the
buffer into driver work lists.
bbcount specifies the number of bytes to be transferred in
both a paged and a non-paged I/O request.
bun.baddr is the virtual address of the I/O request,
unless BPAGEIO is set. The address is a kernel virtual
address, unless BPHYS is set, in which case it is a user
virtual address. If BPAGEIO is set, bun.baddr contains
kernel private data. Note that either one of BPHYS and
BPAGEIO, or neither, can be set, but not both.
bblkno identifies which logical block on the device (the
device is defined by the device number) is to be accessed.
The driver might have to convert this logical block number
to a physical location such as a cylinder, track, and sector
of a disk. This is a 32-bit value. The driver should use
bblkno or blblkno, but not both.
blblkno identifies which logical block on the device (the
device is defined by the device number) is to be accessed.
The driver might have to convert this logical block number
to a physical location such as a cylinder, track, and sector
of a disk. This is a 64-bit value. The driver should use
blblkno or bblkno, but not both.
bresid should be set to the number of bytes not transferred
because of an error.
bbufsize contains the size of the allocated buffer.
SunOS 5.11 Last change: 19 Sep 2002 3
Data Structures for Drivers buf(9S)
biodone identifies a specific biodone routine to be called
by the driver when the I/O is complete.
berror can hold an error code that should be passed as a
return code from the driver. berror is set in conjunction
with the BEROR bit set in the bflags member. bioerror(9F)
should be used in preference to setting the berror field.
bprivate is for the private use of the device driver.
bedev contains the major and minor device numbers of the
device accessed.
SEE ALSO
strategy(9E), aphysio(9F), bioclone(9F), biodone(9F),
bioerror(9F), bioinit(9F), clrbuf(9F), getrbuf(9F),
physio(9F), iovec(9S), uio(9S)
Writing Device Drivers
WARNINGS
Buffers are a shared resource within the kernel. Drivers
should read or write only the members listed in this sec-
tion. Drivers that attempt to use undocumented members of
the buf structure risk corrupting data in the kernel or on
the device.
SunOS 5.11 Last change: 19 Sep 2002 4
|