1.\" Copyright (c) 1998 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd December 22, 1998 29.Dt BUF 9 30.Os 31.Sh NAME 32.Nm buf 33.Nd "kernel buffer I/O scheme used in FreeBSD VM system" 34.Sh DESCRIPTION 35The kernel implements a KVM abstraction of the buffer cache which allows it 36to map potentially disparate vm_page's into contiguous KVM for use by 37(mainly file system) devices and device I/O. 38This abstraction supports 39block sizes from 40.Dv DEV_BSIZE 41(usually 512) to upwards of several pages or more. 42It also supports a relatively primitive byte-granular valid range and dirty 43range currently hardcoded for use by NFS. 44The code implementing the 45VM Buffer abstraction is mostly concentrated in 46.Pa sys/kern/vfs_bio.c 47in the 48.Fx 49source tree. 50.Pp 51One of the most important things to remember when dealing with buffer pointers 52.Pq Vt struct buf 53is that the underlying pages are mapped directly from the buffer 54cache. 55No data copying occurs in the scheme proper, though some file systems 56such as UFS do have to copy a little when dealing with file fragments. 57The second most important thing to remember is that due to the underlying page 58mapping, the 59.Va b_data 60base pointer in a buf is always 61.Em page Ns -aligned , 62not 63.Em block Ns -aligned . 64When you have a VM buffer representing some 65.Va b_offset 66and 67.Va b_size , 68the actual start of the buffer is 69.Ql b_data + (b_offset & PAGE_MASK) 70and not just 71.Ql b_data . 72Finally, the VM system's core buffer cache supports 73valid and dirty bits 74.Pq Va m->valid , m->dirty 75for pages in 76.Dv DEV_BSIZE 77chunks. 78Thus 79a platform with a hardware page size of 4096 bytes has 8 valid and 8 dirty 80bits. 81These bits are generally set and cleared in groups based on the device 82block size of the device backing the page. 83Complete page's worth are often 84referred to using the 85.Dv VM_PAGE_BITS_ALL 86bitmask (i.e., 0xFF if the hardware page 87size is 4096). 88.Pp 89VM buffers also keep track of a byte-granular dirty range and valid range. 90This feature is normally only used by the NFS subsystem. 91I am not sure why it 92is used at all, actually, since we have 93.Dv DEV_BSIZE 94valid/dirty granularity 95within the VM buffer. 96If a buffer dirty operation creates a 97.Dq hole , 98the dirty range will extend to cover the hole. 99If a buffer validation 100operation creates a 101.Dq hole 102the byte-granular valid range is left alone and 103will not take into account the new extension. 104Thus the whole byte-granular 105abstraction is considered a bad hack and it would be nice if we could get rid 106of it completely. 107.Pp 108A VM buffer is capable of mapping the underlying VM cache pages into KVM in 109order to allow the kernel to directly manipulate the data associated with 110the 111.Pq Va vnode , b_offset , b_size . 112The kernel typically unmaps VM buffers the moment 113they are no longer needed but often keeps the 114.Vt struct buf 115structure 116instantiated and even 117.Va bp->b_pages 118array instantiated despite having unmapped 119them from KVM. 120If a page making up a VM buffer is about to undergo I/O, the 121system typically unmaps it from KVM and replaces the page in the 122.Va b_pages[] 123array with a place-marker called bogus_page. 124The place-marker forces any kernel 125subsystems referencing the associated 126.Vt struct buf 127to re-lookup the associated 128page. 129I believe the place-marker hack is used to allow sophisticated devices 130such as file system devices to remap underlying pages in order to deal with, 131for example, re-mapping a file fragment into a file block. 132.Pp 133VM buffers are used to track I/O operations within the kernel. 134Unfortunately, 135the I/O implementation is also somewhat of a hack because the kernel wants 136to clear the dirty bit on the underlying pages the moment it queues the I/O 137to the VFS device, not when the physical I/O is actually initiated. 138This 139can create confusion within file system devices that use delayed-writes because 140you wind up with pages marked clean that are actually still dirty. 141If not 142treated carefully, these pages could be thrown away! 143Indeed, a number of 144serious bugs related to this hack were not fixed until the 145.Fx 2.2.8 / 146.Fx 3.0 147release. 148The kernel uses an instantiated VM buffer (i.e., 149.Vt struct buf ) 150to place-mark pages 151in this special state. 152The buffer is typically flagged 153.Dv B_DELWRI . 154When a 155device no longer needs a buffer it typically flags it as 156.Dv B_RELBUF . 157Due to 158the underlying pages being marked clean, the 159.Ql B_DELWRI|B_RELBUF 160combination must 161be interpreted to mean that the buffer is still actually dirty and must be 162written to its backing store before it can actually be released. 163In the case 164where 165.Dv B_DELWRI 166is not set, the underlying dirty pages are still properly 167marked as dirty and the buffer can be completely freed without losing that 168clean/dirty state information. 169(XXX do we have to check other flags in 170regards to this situation ???) 171.Pp 172The kernel reserves a portion of its KVM space to hold VM Buffer's data 173maps. 174Even though this is virtual space (since the buffers are mapped 175from the buffer cache), we cannot make it arbitrarily large because 176instantiated VM Buffers 177.Pq Vt struct buf Ap s 178prevent their underlying pages in the 179buffer cache from being freed. 180This can complicate the life of the paging 181system. 182.Sh HISTORY 183The 184.Nm 185manual page was originally written by 186.An Matthew Dillon 187and first appeared in 188.Fx 3.1 , 189December 1998. 190