1buf(9S) Data Structures for Drivers buf(9S)
2
3
4
6 buf - block I/O data transfer structure
7
9 #include <sys/ddi.h>
10 #include <sys/sunddi.h>
11
12
14 Architecture independent level 1 (DDI/DKI)
15
17 The buf structure is the basic data structure for block I/O transfers.
18 Each block I/O transfer has an associated buffer header. The header
19 contains all the buffer control and status information. For drivers,
20 the buffer header pointer is the sole argument to a block driver stratā
21 egy(9E) routine. Do not depend on the size of the buf structure when
22 writing a driver.
23
24
25 A buffer header can be linked in multiple lists simultaneously. Because
26 of this, most of the members in the buffer header cannot be changed by
27 the driver, even when the buffer header is in one of the driver's work
28 lists.
29
30
31 Buffer headers are also used by the system for unbuffered or physical
32 I/O for block drivers. In this case, the buffer describes a portion of
33 user data space that is locked into memory.
34
35
36 Block drivers often chain block requests so that overall throughput for
37 the device is maximized. The av_forw and the av_back members of the buf
38 structure can serve as link pointers for chaining block requests.
39
41 int b_flags; /* Buffer status */
42 struct buf *av_forw; /* Driver work list link */
43 struct buf *av_back; /* Driver work list link */
44 size_t b_bcount; /* # of bytes to transfer */
45 union {
46 caddr_t b_addr; /* Buffer's virtual address */
47 } b_un;
48 daddr_t b_blkno; /* Block number on device */
49 diskaddr_t b_lblkno; /* Expanded block number on dev. */
50 size_t b_resid; /* # of bytes not xferred */
51 size_t b_bufsize; /* size of alloc. buffer */
52 int (*b_iodone)(struct buf *); /* function called */
53 /* by biodone */
54 int b_error; /* expanded error field */
55 void *b_private; /* "opaque" driver private area */
56 dev_t b_edev; /* expanded dev field */
57
58
59
60 The members of the buffer header available to test or set by a driver
61 are as follows:
62
63
64 b_flags stores the buffer status and indicates to the driver whether to
65 read or write to the device. The driver must never clear the b_flags
66 member. If this is done, unpredictable results can occur including loss
67 of disk sanity and the possible failure of other kernel processes.
68
69
70 All b_flags bit values not otherwise specified above are reserved by
71 the kernel and may not be used.
72
73
74 Valid flags are as follows:
75
76 B_BUSY Indicates the buffer is in use. The driver must not change
77 this flag unless it allocated the buffer with getrbuf(9F)
78 and no I/O operation is in progress.
79
80
81 B_DONE Indicates the data transfer has completed. This flag is
82 read-only.
83
84
85 B_ERROR Indicates an I/O transfer error. It is set in conjunction
86 with the b_error field. bioerror(9F) should be used in
87 preference to setting the B_ERROR bit.
88
89
90 B_PAGEIO Indicates the buffer is being used in a paged I/O request.
91 See the description of the b_un.b_addr field for more
92 information. This flag is read-only.
93
94
95 B_PHYS indicates the buffer header is being used for physical
96 (direct) I/O to a user data area. See the description of
97 the b_un.b_addr field for more information. This flag is
98 read-only.
99
100
101 B_READ Indicates that data is to be read from the peripheral
102 device into main memory.
103
104
105 B_WRITE Indicates that the data is to be transferred from main
106 memory to the peripheral device. B_WRITE is a pseudo flag
107 and cannot be directly tested; it is only detected as the
108 NOT form of B_READ.
109
110
111
112 av_forw and av_back can be used by the driver to link the buffer into
113 driver work lists.
114
115
116 b_bcount specifies the number of bytes to be transferred in both a
117 paged and a non-paged I/O request.
118
119
120 b_un.b_addr is the virtual address of the I/O request, unless B_PAGEIO
121 is set. The address is a kernel virtual address, unless B_PHYS is set,
122 in which case it is a user virtual address. If B_PAGEIO is set,
123 b_un.b_addr contains kernel private data. Note that either one of
124 B_PHYS and B_PAGEIO, or neither, can be set, but not both.
125
126
127 b_blkno identifies which logical block on the device (the device is
128 defined by the device number) is to be accessed. The driver might have
129 to convert this logical block number to a physical location such as a
130 cylinder, track, and sector of a disk. This is a 32-bit value. The
131 driver should use b_blkno or b_lblkno, but not both.
132
133
134 b_lblkno identifies which logical block on the device (the device is
135 defined by the device number) is to be accessed. The driver might have
136 to convert this logical block number to a physical location such as a
137 cylinder, track, and sector of a disk. This is a 64-bit value. The
138 driver should use b_lblkno or b_blkno, but not both.
139
140
141 b_resid should be set to the number of bytes not transferred because of
142 an error.
143
144
145 b_bufsize contains the size of the allocated buffer.
146
147
148 b_iodone identifies a specific biodone routine to be called by the
149 driver when the I/O is complete.
150
151
152 b_error can hold an error code that should be passed as a return code
153 from the driver. b_error is set in conjunction with the B_ERROR bit set
154 in the b_flags member. bioerror(9F) should be used in preference to
155 setting the b_error field.
156
157
158 b_private is for the private use of the device driver.
159
160
161 b_edev contains the major and minor device numbers of the device
162 accessed.
163
165 strategy(9E), aphysio(9F), bioclone(9F), biodone(9F), bioerror(9F),
166 bioinit(9F), clrbuf(9F), getrbuf(9F), physio(9F), iovec(9S), uio(9S)
167
168
169 Writing Device Drivers
170
172 Buffers are a shared resource within the kernel. Drivers should read or
173 write only the members listed in this section. Drivers that attempt to
174 use undocumented members of the buf structure risk corrupting data in
175 the kernel or on the device.
176
177
178
179SunOS 5.11 19 Sep 2002 buf(9S)