BUF(9S) Data Structures for Drivers BUF(9S) NNAAMMEE bbuuff - block I/O data transfer structure SSYYNNOOPPSSIISS ##iinncclluuddee <> ##iinncclluuddee <> IINNTTEERRFFAACCEE LLEEVVEELL Architecture independent level 1 (DDI/DKI) DDEESSCCRRIIPPTTIIOONN The _b_u_f structure is the basic data structure for block II//OO transfers. Each block II//OO 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 _b_u_f structure when writing a driver. A buffer header can be linked in multiple lists simultaneously. 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 II//OO 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 _a_v___f_o_r_w and the _a_v___b_a_c_k members of the _b_u_f structure can serve as link pointers for chaining block requests. SSTTRRUUCCTTUURREE MMEEMMBBEERRSS int b_flags; /* Buffer status */ struct buf *av_forw; /* Driver work list link */ struct buf *av_back; /* Driver work list link */ size_t b_bcount; /* # of bytes to transfer */ union { caddr_t b_addr; /* Buffer's virtual address */ } b_un; daddr_t b_blkno; /* Block number on device */ diskaddr_t b_lblkno; /* Expanded block number on dev. */ size_t b_resid; /* # of bytes not xferred */ size_t b_bufsize; /* size of alloc. buffer */ int (*b_iodone)(struct buf *); /* function called */ /* by biodone */ int b_error; /* expanded error field */ void *b_private; /* "opaque" driver private area */ dev_t b_edev; /* expanded dev field */ The members of the buffer header available to test or set by a driver are as follows: _b___f_l_a_g_s stores the buffer status and indicates to the driver whether to read or write to the device. The driver must never clear the _b___f_l_a_g_s member. If this is done, unpredictable results can occur including loss of disk sanity and the possible failure of other kernel processes. All _b___f_l_a_g_s bit values not otherwise specified above are reserved by the kernel and may not be used. Valid flags are as follows: B_BUSY Indicates the buffer is in use. The driver must not change this flag unless it allocated the buffer with getrbuf(9F) and no II//OO operation is in progress. B_DONE Indicates the data transfer has completed. This flag is read- only. B_ERROR Indicates an II//OO transfer error. It is set in conjunction with the _b___e_r_r_o_r field. bioerror(9F) should be used in preference to setting the B_ERROR bit. B_PAGEIO Indicates the buffer is being used in a paged II//OO request. See the description of the _b___u_n_._b___a_d_d_r field for more information. This flag is read-only. B_PHYS indicates the buffer header is being used for physical (direct) II//OO to a user data area. See the description of the _b___u_n_._b___a_d_d_r field for more information. This flag is read- only. B_READ Indicates that data is to be read from the peripheral device into main memory. B_WRITE Indicates that the data is to be transferred from main memory to the peripheral device. B_WRITE is a pseudo flag and cannot be directly tested; it is only detected as the NOT form of B_READ. _a_v___f_o_r_w and _a_v___b_a_c_k can be used by the driver to link the buffer into driver work lists. _b___b_c_o_u_n_t specifies the number of bytes to be transferred in both a paged and a non-paged II//OO request. _b___u_n_._b___a_d_d_r is the virtual address of the II//OO request, unless B_PAGEIO is set. The address is a kernel virtual address, unless B_PHYS is set, in which case it is a user virtual address. If B_PAGEIO is set, _b___u_n_._b___a_d_d_r contains kernel private data. Note that either one of B_PHYS and B_PAGEIO, or neither, can be set, but not both. _b___b_l_k_n_o 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 _b___b_l_k_n_o or _b___l_b_l_k_n_o, but not both. _b___l_b_l_k_n_o 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 _b___l_b_l_k_n_o or _b___b_l_k_n_o, but not both. _b___r_e_s_i_d should be set to the number of bytes not transferred because of an error. _b___b_u_f_s_i_z_e contains the size of the allocated buffer. _b___i_o_d_o_n_e identifies a specific biodone(9F) routine to be called by the driver when the II//OO is complete. _b___e_r_r_o_r can hold an error code that should be passed as a return code from the driver. _b___e_r_r_o_r is set in conjunction with the B_ERROR bit set in the _b___f_l_a_g_s member. bioerror(9F) should be used in preference to setting the _b___e_r_r_o_r field. _b___p_r_i_v_a_t_e is for the private use of the device driver. _b___e_d_e_v contains the major and minor device numbers of the device accessed. SSEEEE AALLSSOO strategy(9E), aphysio(9F), bioclone(9F), biodone(9F), bioerror(9F), bioinit(9F), clrbuf(9F), getrbuf(9F), physio(9F), iovec(9S), uio(9S) _W_r_i_t_i_n_g _D_e_v_i_c_e _D_r_i_v_e_r_s. WWAARRNNIINNGGSS Buffers are a shared resource within the kernel. Drivers should read or write only the members listed in this section. Drivers that attempt to use undocumented members of the _b_u_f structure risk corrupting data in the kernel or on the device. illumos July 9, 2018 illumos