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