xref: /freebsd/share/man/man9/buf.9 (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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.\" $FreeBSD$
29.\"
30.Dd December 22, 1998
31.Dt BUF 9
32.Os
33.Sh NAME
34.Nm buf
35.Nd "kernel buffer I/O scheme used in FreeBSD VM system"
36.Sh DESCRIPTION
37The kernel implements a KVM abstraction of the buffer cache which allows it
38to map potentially disparate vm_page's into contiguous KVM for use by
39(mainly file system) devices and device I/O.
40This abstraction supports
41block sizes from DEV_BSIZE (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 /usr/src/sys/kern/vfs_bio.c .
47.Pp
48One of the most important things to remember when dealing with buffer pointers
49(struct buf) is that the underlying pages are mapped directly from the buffer
50cache.
51No data copying occurs in the scheme proper, though some file systems
52such as UFS do have to copy a little when dealing with file fragments.
53The second most important thing to remember is that due to the underlying page
54mapping, the b_data base pointer in a buf is always *page* aligned, not
55*block* aligned.
56When you have a VM buffer representing some b_offset and
57b_size, the actual start of the buffer is (b_data + (b_offset & PAGE_MASK))
58and not just b_data.
59Finally, the VM system's core buffer cache supports
60valid and dirty bits (m->valid, m->dirty) for pages in DEV_BSIZE chunks.
61Thus
62a platform with a hardware page size of 4096 bytes has 8 valid and 8 dirty
63bits.
64These bits are generally set and cleared in groups based on the device
65block size of the device backing the page.
66Complete page's worth are often
67referred to using the VM_PAGE_BITS_ALL bitmask (i.e., 0xFF if the hardware page
68size is 4096).
69.Pp
70VM buffers also keep track of a byte-granular dirty range and valid range.
71This feature is normally only used by the NFS subsystem.
72I am not sure why it
73is used at all, actually, since we have DEV_BSIZE valid/dirty granularity
74within the VM buffer.
75If a buffer dirty operation creates a 'hole',
76the dirty range will extend to cover the hole.
77If a buffer validation
78operation creates a 'hole' the byte-granular valid range is left alone and
79will not take into account the new extension.
80Thus the whole byte-granular
81abstraction is considered a bad hack and it would be nice if we could get rid
82of it completely.
83.Pp
84A VM buffer is capable of mapping the underlying VM cache pages into KVM in
85order to allow the kernel to directly manipulate the data associated with
86the (vnode,b_offset,b_size).
87The kernel typically unmaps VM buffers the moment
88they are no longer needed but often keeps the 'struct buf' structure
89instantiated and even bp->b_pages array instantiated despite having unmapped
90them from KVM.
91If a page making up a VM buffer is about to undergo I/O, the
92system typically unmaps it from KVM and replaces the page in the b_pages[]
93array with a place-marker called bogus_page.
94The place-marker forces any kernel
95subsystems referencing the associated struct buf to re-lookup the associated
96page.
97I believe the place-marker hack is used to allow sophisticated devices
98such as file system devices to remap underlying pages in order to deal with,
99for example, re-mapping a file fragment into a file block.
100.Pp
101VM buffers are used to track I/O operations within the kernel.
102Unfortunately,
103the I/O implementation is also somewhat of a hack because the kernel wants
104to clear the dirty bit on the underlying pages the moment it queues the I/O
105to the VFS device, not when the physical I/O is actually initiated.
106This
107can create confusion within file system devices that use delayed-writes because
108you wind up with pages marked clean that are actually still dirty.
109If not
110treated carefully, these pages could be thrown away!
111Indeed, a number of
112serious bugs related to this hack were not fixed until the 2.2.8/3.0 release.
113The kernel uses an instantiated VM buffer (i.e., struct buf) to place-mark pages
114in this special state.
115The buffer is typically flagged B_DELWRI.
116When a
117device no longer needs a buffer it typically flags it as B_RELBUF.
118Due to
119the underlying pages being marked clean, the B_DELWRI|B_RELBUF combination must
120be interpreted to mean that the buffer is still actually dirty and must be
121written to its backing store before it can actually be released.
122In the case
123where B_DELWRI is not set, the underlying dirty pages are still properly
124marked as dirty and the buffer can be completely freed without losing that
125clean/dirty state information.
126(XXX do we have to check other flags in
127regards to this situation ???)
128.Pp
129The kernel reserves a portion of its KVM space to hold VM Buffer's data
130maps.
131Even though this is virtual space (since the buffers are mapped
132from the buffer cache), we cannot make it arbitrarily large because
133instantiated VM Buffers (struct buf's) prevent their underlying pages in the
134buffer cache from being freed.
135This can complicate the life of the paging
136system.
137.\" .Sh SEE ALSO
138.\" .Xr <fillmein> 9
139.Sh HISTORY
140The
141.Nm
142manual page was originally written by
143.An Matthew Dillon
144and first appeared in
145.Fx 3.1 ,
146December 1998.
147