1998-12-22 19:47:48 +00:00
|
|
|
.\" Copyright (c) 1998
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the University of
|
|
|
|
.\" California, Berkeley and its contributors.
|
|
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
1999-08-28 00:22:10 +00:00
|
|
|
.\" $FreeBSD$
|
1998-12-22 19:47:48 +00:00
|
|
|
.\"
|
|
|
|
.Dd December 22, 1998
|
|
|
|
.Dt BUF 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
2001-12-08 04:15:37 +00:00
|
|
|
.Nm buf
|
2001-04-18 15:54:10 +00:00
|
|
|
.Nd "kernel buffer I/O scheme used in FreeBSD VM system"
|
1998-12-22 19:47:48 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The kernel implements a KVM abstraction of the buffer cache which allows it
|
2001-07-14 19:41:16 +00:00
|
|
|
to map potentially disparate vm_page's into contiguous KVM for use by
|
2003-10-23 02:22:07 +00:00
|
|
|
(mainly file system) devices and device I/O.
|
|
|
|
This abstraction supports
|
1998-12-22 19:47:48 +00:00
|
|
|
block sizes from DEV_BSIZE (usually 512) to upwards of several pages or more.
|
|
|
|
It also supports a relatively primitive byte-granular valid range and dirty
|
2003-10-23 02:22:07 +00:00
|
|
|
range currently hardcoded for use by NFS.
|
|
|
|
The code implementing the
|
1999-10-30 14:15:49 +00:00
|
|
|
VM Buffer abstraction is mostly concentrated in
|
|
|
|
.Pa /usr/src/sys/kern/vfs_bio.c .
|
1998-12-22 19:47:48 +00:00
|
|
|
.Pp
|
|
|
|
One of the most important things to remember when dealing with buffer pointers
|
|
|
|
(struct buf) is that the underlying pages are mapped directly from the buffer
|
2003-10-23 02:22:07 +00:00
|
|
|
cache.
|
|
|
|
No data copying occurs in the scheme proper, though some file systems
|
|
|
|
such as UFS do have to copy a little when dealing with file fragments.
|
|
|
|
The second most important thing to remember is that due to the underlying page
|
1998-12-22 19:47:48 +00:00
|
|
|
mapping, the b_data base pointer in a buf is always *page* aligned, not
|
2003-10-23 02:22:07 +00:00
|
|
|
*block* aligned.
|
|
|
|
When you have a VM buffer representing some b_offset and
|
1998-12-22 19:47:48 +00:00
|
|
|
b_size, the actual start of the buffer is (b_data + (b_offset & PAGE_MASK))
|
2003-10-23 02:22:07 +00:00
|
|
|
and not just b_data.
|
|
|
|
Finally, the VM system's core buffer cache supports
|
|
|
|
valid and dirty bits (m->valid, m->dirty) for pages in DEV_BSIZE chunks.
|
|
|
|
Thus
|
1998-12-22 19:47:48 +00:00
|
|
|
a platform with a hardware page size of 4096 bytes has 8 valid and 8 dirty
|
2003-10-23 02:22:07 +00:00
|
|
|
bits.
|
|
|
|
These bits are generally set and cleared in groups based on the device
|
|
|
|
block size of the device backing the page.
|
|
|
|
Complete page's worth are often
|
1999-02-01 22:55:54 +00:00
|
|
|
referred to using the VM_PAGE_BITS_ALL bitmask (i.e. 0xFF if the hardware page
|
1998-12-22 19:47:48 +00:00
|
|
|
size is 4096).
|
|
|
|
.Pp
|
|
|
|
VM buffers also keep track of a byte-granular dirty range and valid range.
|
2003-10-23 02:22:07 +00:00
|
|
|
This feature is normally only used by the NFS subsystem.
|
|
|
|
I'm not sure why it
|
2001-07-14 19:41:16 +00:00
|
|
|
is used at all, actually, since we have DEV_BSIZE valid/dirty granularity
|
2003-10-23 02:22:07 +00:00
|
|
|
within the VM buffer.
|
|
|
|
If a buffer dirty operation creates a 'hole',
|
|
|
|
the dirty range will extend to cover the hole.
|
|
|
|
If a buffer validation
|
1998-12-22 19:47:48 +00:00
|
|
|
operation creates a 'hole' the byte-granular valid range is left alone and
|
2003-10-23 02:22:07 +00:00
|
|
|
will not take into account the new extension.
|
|
|
|
Thus the whole byte-granular
|
2001-07-14 19:41:16 +00:00
|
|
|
abstraction is considered a bad hack and it would be nice if we could get rid
|
1998-12-22 19:47:48 +00:00
|
|
|
of it completely.
|
|
|
|
.Pp
|
|
|
|
A VM buffer is capable of mapping the underlying VM cache pages into KVM in
|
|
|
|
order to allow the kernel to directly manipulate the data associated with
|
2003-10-23 02:22:07 +00:00
|
|
|
the (vnode,b_offset,b_size).
|
|
|
|
The kernel typically unmaps VM buffers the moment
|
2001-07-14 19:41:16 +00:00
|
|
|
they are no longer needed but often keeps the 'struct buf' structure
|
1998-12-22 19:47:48 +00:00
|
|
|
instantiated and even bp->b_pages array instantiated despite having unmapped
|
2003-10-23 02:22:07 +00:00
|
|
|
them from KVM.
|
|
|
|
If a page making up a VM buffer is about to undergo I/O, the
|
1998-12-22 19:47:48 +00:00
|
|
|
system typically unmaps it from KVM and replaces the page in the b_pages[]
|
2003-10-23 02:22:07 +00:00
|
|
|
array with a place-marker called bogus_page.
|
|
|
|
The place-marker forces any kernel
|
1998-12-22 19:47:48 +00:00
|
|
|
subsystems referencing the associated struct buf to re-lookup the associated
|
2003-10-23 02:22:07 +00:00
|
|
|
page.
|
|
|
|
I believe the place-marker hack is used to allow sophisticated devices
|
2002-12-12 17:26:04 +00:00
|
|
|
such as file system devices to remap underlying pages in order to deal with,
|
2002-01-09 11:43:48 +00:00
|
|
|
for example, re-mapping a file fragment into a file block.
|
1998-12-22 19:47:48 +00:00
|
|
|
.Pp
|
2003-10-23 02:22:07 +00:00
|
|
|
VM buffers are used to track I/O operations within the kernel.
|
|
|
|
Unfortunately,
|
1998-12-22 19:47:48 +00:00
|
|
|
the I/O implementation is also somewhat of a hack because the kernel wants
|
|
|
|
to clear the dirty bit on the underlying pages the moment it queues the I/O
|
2003-10-23 02:22:07 +00:00
|
|
|
to the VFS device, not when the physical I/O is actually initiated.
|
|
|
|
This
|
2002-12-12 17:26:04 +00:00
|
|
|
can create confusion within file system devices that use delayed-writes because
|
2003-10-23 02:22:07 +00:00
|
|
|
you wind up with pages marked clean that are actually still dirty.
|
|
|
|
If not
|
1998-12-22 19:47:48 +00:00
|
|
|
treated carefully, these pages could be thrown away! Indeed, a number of
|
1999-10-30 14:15:49 +00:00
|
|
|
serious bugs related to this hack were not fixed until the 2.2.8/3.0 release.
|
2002-01-09 11:43:48 +00:00
|
|
|
The kernel uses an instantiated VM buffer (i.e. struct buf) to place-mark pages
|
2003-10-23 02:22:07 +00:00
|
|
|
in this special state.
|
|
|
|
The buffer is typically flagged B_DELWRI.
|
|
|
|
When a
|
|
|
|
device no longer needs a buffer it typically flags it as B_RELBUF.
|
|
|
|
Due to
|
1998-12-22 19:47:48 +00:00
|
|
|
the underlying pages being marked clean, the B_DELWRI|B_RELBUF combination must
|
|
|
|
be interpreted to mean that the buffer is still actually dirty and must be
|
2003-10-23 02:22:07 +00:00
|
|
|
written to its backing store before it can actually be released.
|
|
|
|
In the case
|
2001-07-14 19:41:16 +00:00
|
|
|
where B_DELWRI is not set, the underlying dirty pages are still properly
|
1998-12-22 19:47:48 +00:00
|
|
|
marked as dirty and the buffer can be completely freed without losing that
|
2004-05-05 10:47:19 +00:00
|
|
|
clean/dirty state information.
|
|
|
|
.Po
|
|
|
|
XXX do we have to check other flags in
|
|
|
|
regards to this situation ???
|
|
|
|
.Pc
|
1998-12-22 19:47:48 +00:00
|
|
|
.Pp
|
|
|
|
The kernel reserves a portion of its KVM space to hold VM Buffer's data
|
2003-10-23 02:22:07 +00:00
|
|
|
maps.
|
|
|
|
Even though this is virtual space (since the buffers are mapped
|
2001-07-14 19:41:16 +00:00
|
|
|
from the buffer cache), we cannot make it arbitrarily large because
|
1998-12-22 19:47:48 +00:00
|
|
|
instantiated VM Buffers (struct buf's) prevent their underlying pages in the
|
2003-10-23 02:22:07 +00:00
|
|
|
buffer cache from being freed.
|
|
|
|
This can complicate the life of the paging
|
1998-12-22 19:47:48 +00:00
|
|
|
system.
|
|
|
|
.Pp
|
1999-06-27 02:38:46 +00:00
|
|
|
.\" .Sh SEE ALSO
|
|
|
|
.\" .Xr <fillmein> 9
|
1998-12-22 19:47:48 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Nm
|
1999-10-30 14:15:49 +00:00
|
|
|
manual page was originally written by
|
|
|
|
.An Matthew Dillon
|
|
|
|
and first appeared in
|
|
|
|
.Fx 3.1 ,
|
1999-06-27 02:38:46 +00:00
|
|
|
December 1998.
|