1 .\" Copyright (c) 2002 Sun Microsystems, Inc. All Rights Reserved.
   2 .\" Copyright 1989 AT&T
   3 .\" The contents of this file are subject to the terms of the
   4 .\" Common Development and Distribution License (the "License").
   5 .\" You may not use this file except in compliance with the License.
   6 .\"
   7 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   8 .\" or http://www.opensolaris.org/os/licensing.
   9 .\" See the License for the specific language governing permissions
  10 .\" and limitations under the License.
  11 .\"
  12 .\" When distributing Covered Code, include this CDDL HEADER in each
  13 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  14 .\" If applicable, add the following below this CDDL HEADER, with the
  15 .\" fields enclosed by brackets "[]" replaced with your own identifying
  16 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  17 .Dd July 9, 2018
  18 .Dt BUF 9S
  19 .Os
  20 .Sh NAME
  21 .Nm buf
  22 .Nd block I/O data transfer structure
  23 .Sh SYNOPSIS
  24 .In sys/ddi.h
  25 .In sys/sunddi.h
  26 .Sh INTERFACE LEVEL
  27 Architecture independent level 1 (DDI/DKI)
  28 .Sh DESCRIPTION
  29 The
  30 .Vt buf
  31 structure is the basic data structure for block
  32 .Sy I/O
  33 transfers.
  34 Each block
  35 .Sy I/O
  36 transfer has an associated buffer header.
  37 The header contains all the buffer control and status information.
  38 For drivers, the buffer header pointer is the sole argument to a block driver
  39 .Xr strategy 9E
  40 routine.
  41 Do not depend on the size of the
  42 .Vt buf
  43 structure when writing a driver.
  44 .Pp
  45 A buffer header can be linked in multiple lists simultaneously.
  46 Because of
  47 this, most of the members in the buffer header cannot be changed by the driver,
  48 even when the buffer header is in one of the driver's work lists.
  49 .Pp
  50 Buffer headers are also used by the system for unbuffered or physical
  51 .Sy I/O
  52 for block drivers.
  53 In this case, the buffer describes a portion of user data
  54 space that is locked into memory.
  55 .Pp
  56 Block drivers often chain block requests so that overall throughput for the
  57 device is maximized.
  58 The
  59 .Fa av_forw
  60 and the
  61 .Fa av_back
  62 members of the
  63 .Vt buf
  64 structure can serve as link pointers for chaining block requests.
  65 .Sh STRUCTURE MEMBERS
  66 .Bd -literal -offset 2n
  67 int           b_flags;           /* Buffer status */
  68 struct buf    *av_forw;          /* Driver work list link */
  69 struct buf    *av_back;          /* Driver work list link */
  70 size_t        b_bcount;          /* # of bytes to transfer */
  71 union {
  72     caddr_t  b_addr;            /* Buffer's virtual address */
  73 } b_un;
  74 daddr_t       b_blkno;           /* Block number on device */
  75 diskaddr_t    b_lblkno;          /* Expanded block number on dev.  */
  76 size_t        b_resid;           /* # of bytes not xferred */
  77 size_t        b_bufsize;         /* size of alloc. buffer */
  78 int           (*b_iodone)(struct buf *); /* function called */
  79                                          /* by biodone */
  80 int           b_error;           /* expanded error field */
  81 void          *b_private;        /* "opaque" driver private area */
  82 dev_t         b_edev;            /* expanded dev field */
  83 .Ed
  84 .Pp
  85 The members of the buffer header available to test or set by a driver are as
  86 follows:
  87 .Pp
  88 .Fa b_flags
  89 stores the buffer status and indicates to the driver whether to
  90 read or write to the device.
  91 The driver must never clear the
  92 .Fa b_flags
  93 member.
  94 If this is done, unpredictable results can occur including loss of disk
  95 sanity and the possible failure of other kernel processes.
  96 .Pp
  97 All
  98 .Fa b_flags
  99 bit values not otherwise specified above are reserved by the
 100 kernel and may not be used.
 101 .Pp
 102 Valid flags are as follows:
 103 .Bl -tag -width "B_PAGEIO"
 104 .It Dv B_BUSY
 105 Indicates the buffer is in use.
 106 The driver must not change this flag unless it allocated the buffer with
 107 .Xr getrbuf 9F
 108 and no
 109 .Sy I/O
 110 operation is in progress.
 111 .It Dv B_DONE
 112 Indicates the data transfer has completed.
 113 This flag is read-only.
 114 .It Dv B_ERROR
 115 Indicates an
 116 .Sy I/O
 117 transfer error.
 118 It is set in conjunction with the
 119 .Fa b_error
 120 field.
 121 .Xr bioerror 9F
 122 should be used in preference to setting the
 123 .Dv B_ERROR
 124 bit.
 125 .It Dv B_PAGEIO
 126 Indicates the buffer is being used in a paged
 127 .Sy I/O
 128 request.
 129 See the description of the
 130 .Fa b_un.b_addr
 131 field for more information.
 132 This flag is read-only.
 133 .It Dv B_PHYS
 134 indicates the buffer header is being used for physical (direct)
 135 .Sy I/O
 136 to a user data area.
 137 See the description of the
 138 .Fa b_un.b_addr
 139 field for more information.
 140 This flag is read-only.
 141 .It Dv B_READ
 142 Indicates that data is to be read from the peripheral device into main memory.
 143 .It Dv B_WRITE
 144 Indicates that the data is to be transferred from main memory to the peripheral
 145 device.
 146 .Dv B_WRITE
 147 is a pseudo flag and cannot be directly tested; it is
 148 only detected as the NOT form of
 149 .Dv B_READ .
 150 .El
 151 .Pp
 152 .Fa av_forw
 153 and
 154 .Fa av_back
 155 can be used by the driver to link the buffer into driver work lists.
 156 .Pp
 157 .Fa b_bcount
 158 specifies the number of bytes to be transferred in both a paged and a non-paged
 159 .Sy I/O
 160 request.
 161 .Pp
 162 .Fa b_un.b_addr
 163 is the virtual address of the
 164 .Sy I/O
 165 request, unless
 166 .Dv B_PAGEIO
 167 is set.
 168 The address is a kernel virtual address, unless
 169 .Dv B_PHYS
 170 is set, in which case it is a user virtual address.
 171 If
 172 .Dv B_PAGEIO
 173 is set,
 174 .Fa b_un.b_addr
 175 contains kernel private data.
 176 Note that either one of
 177 .Dv B_PHYS
 178 and
 179 .Dv B_PAGEIO ,
 180 or neither, can be set, but
 181 not both.
 182 .Pp
 183 .Fa b_blkno
 184 identifies which logical block on the device (the device is
 185 defined by the device number) is to be accessed.
 186 The driver might have to
 187 convert this logical block number to a physical location such as a cylinder,
 188 track, and sector of a disk.
 189 This is a 32-bit value.
 190 The driver should use
 191 .Fa b_blkno
 192 or
 193 .Fa b_lblkno ,
 194 but not both.
 195 .Pp
 196 .Fa b_lblkno
 197 identifies which logical block on the device (the device is
 198 defined by the device number) is to be accessed.
 199 The driver might have to
 200 convert this logical block number to a physical location such as a cylinder,
 201 track, and sector of a disk.
 202 This is a 64-bit value.
 203 The driver should use
 204 .Fa b_lblkno
 205 or
 206 .Fa b_blkno ,
 207 but not both.
 208 .Pp
 209 .Fa b_resid
 210 should be set to the number of bytes not transferred because of an error.
 211 .Pp
 212 .Fa b_bufsize
 213 contains the size of the allocated buffer.
 214 .Pp
 215 .Fa b_iodone
 216 identifies a specific
 217 .Xr biodone 9F
 218 routine to be called by the driver when the
 219 .Sy I/O
 220 is complete.
 221 .Pp
 222 .Fa b_error
 223 can hold an error code that should be passed as a return code
 224 from the driver.
 225 .Fa b_error
 226 is set in conjunction with the
 227 .Dv B_ERROR
 228 bit
 229 set in the
 230 .Fa b_flags
 231 member.
 232 .Xr bioerror 9F
 233 should be used in
 234 preference to setting the
 235 .Fa b_error
 236 field.
 237 .Pp
 238 .Fa b_private
 239 is for the private use of the device driver.
 240 .Pp
 241 .Fa b_edev
 242 contains the major and minor device numbers of the device
 243 accessed.
 244 .Sh SEE ALSO
 245 .Xr strategy 9E ,
 246 .Xr aphysio 9F ,
 247 .Xr bioclone 9F ,
 248 .Xr biodone 9F ,
 249 .Xr bioerror 9F ,
 250 .Xr bioinit 9F ,
 251 .Xr clrbuf 9F ,
 252 .Xr getrbuf 9F ,
 253 .Xr physio 9F ,
 254 .Xr iovec 9S ,
 255 .Xr uio 9S
 256 .Rs
 257 .%T Writing Device Drivers
 258 .Re
 259 .Sh WARNINGS
 260 Buffers are a shared resource within the kernel.
 261 Drivers should read or write only the members listed in this section.
 262 Drivers that attempt to use undocumented members of the
 263 .Fa buf
 264 structure risk corrupting data in the kernel or on the device.