buf.9 (fda40672d09a911535818413bce1298550d57bcb) buf.9 (88b85f7467f06bac27e50e664da04aed9ecb9929)
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.

--- 15 unchanged lines hidden (view full) ---

24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
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.

--- 15 unchanged lines hidden (view full) ---

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