Remove the old release infrastructure documentation inherited from
[dragonfly.git] / share / man / man9 / buf.9
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. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
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: src/share/man/man9/buf.9,v 1.5.2.5 2001/12/17 11:30:18 ru Exp $
33 .\" $DragonFly: src/share/man/man9/buf.9,v 1.3 2004/12/22 05:53:09 hmp Exp $
34 .\"
35 .Dd December 21, 2004
36 .Dt BUF 9
37 .Os
38 .Sh NAME
39 .Nm buf
40 .Nd "kernel buffer I/O scheme used in FreeBSD VM system"
41 .Sh DESCRIPTION
42 The kernel implements a KVM abstraction of the buffer cache which allows it
43 to map potentially disparate vm_page's into contiguous KVM for use by
44 (mainly filesystem) devices and device I/O.  This abstraction supports
45 block sizes from DEV_BSIZE (usually 512) to upwards of several pages or more.
46 It also supports a relatively primitive byte-granular valid range and dirty
47 range currently hardcoded for use by NFS.
48 .Pp
49 The code implementing the VM Buffer abstraction is mostly concentrated in
50 .Pa /usr/src/sys/kern/vfs_bio.c and
51 .Pa /usr/src/sys/sys/buf.h .
52 .Pp
53 One of the most important things to remember when dealing with buffer pointers
54 (struct buf) is that the underlying pages are mapped directly from the buffer
55 cache.  No data copying occurs in the scheme proper, though some filesystems
56 such as UFS do have to copy a little when dealing with file fragments.  The
57 second most important thing to remember is that due to the underlying page
58 mapping, the b_data base pointer in a buf is always *page* aligned, not
59 *block* aligned.  When you have a VM buffer representing some b_offset and
60 b_size, the actual start of the buffer is (b_data + (b_offset & PAGE_MASK))
61 and not just b_data.  Finally, the VM system's core buffer cache supports
62 valid and dirty bits (m->valid, m->dirty) for pages in DEV_BSIZE chunks.  Thus
63 a platform with a hardware page size of 4096 bytes has 8 valid and 8 dirty
64 bits.  These bits are generally set and cleared in groups based on the device
65 block size of the device backing the page.  Complete page's worth are often
66 referred to using the VM_PAGE_BITS_ALL bitmask (i.e. 0xFF if the hardware page
67 size is 4096).
68 .Pp
69 VM buffers also keep track of a byte-granular dirty range and valid range.
70 This feature is normally only used by the NFS subsystem.  I'm not sure why it
71 is used at all, actually, since we have DEV_BSIZE valid/dirty granularity
72 within the VM buffer.  If a buffer dirty operation creates a 'hole',
73 the dirty range will extend to cover the hole.  If a buffer validation
74 operation creates a 'hole' the byte-granular valid range is left alone and
75 will not take into account the new extension.  Thus the whole byte-granular
76 abstraction is considered a bad hack and it would be nice if we could get rid
77 of it completely.
78 .Pp
79 A VM buffer is capable of mapping the underlying VM cache pages into KVM in
80 order to allow the kernel to directly manipulate the data associated with
81 the (vnode,b_offset,b_size).  The kernel typically unmaps VM buffers the moment
82 they are no longer needed but often keeps the 'struct buf' structure
83 instantiated and even bp->b_pages array instantiated despite having unmapped
84 them from KVM.  If a page making up a VM buffer is about to undergo I/O, the
85 system typically unmaps it from KVM and replaces the page in the b_pages[]
86 array with a placemarker called bogus_page.  The placemarker forces any kernel
87 subsystems referencing the associated struct buf to re-lookup the associated
88 page.  I believe the placemarker hack is used to allow sophisticated devices
89 such as filesystem devices to remap underlying pages in order to deal with,
90 for example, remapping a file fragment into a file block.
91 .Pp
92 VM buffers are used to track I/O operations within the kernel.  Unfortunately,
93 the I/O implementation is also somewhat of a hack because the kernel wants
94 to clear the dirty bit on the underlying pages the moment it queues the I/O
95 to the VFS device, not when the physical I/O is actually initiated.  This
96 can create confusion within filesystem devices that use delayed-writes because
97 you wind up with pages marked clean that are actually still dirty.  If not
98 treated carefully, these pages could be thrown away!  Indeed, a number of
99 serious bugs related to this hack were not fixed until the 2.2.8/3.0 release.
100 The kernel uses an instantiated VM buffer (i.e. struct buf) to placemark pages
101 in this special state.  The buffer is typically flagged B_DELWRI.  When a
102 device no longer needs a buffer it typically flags it as B_RELBUF.  Due to
103 the underlying pages being marked clean, the B_DELWRI|B_RELBUF combination must
104 be interpreted to mean that the buffer is still actually dirty and must be
105 written to its backing store before it can actually be released.  In the case
106 where B_DELWRI is not set, the underlying dirty pages are still properly
107 marked as dirty and the buffer can be completely freed without losing that
108 clean/dirty state information.  ( XXX do we have to check other flags in
109 regards to this situation ??? ).
110 .Pp
111 The kernel reserves a portion of its KVM space to hold VM Buffer's data
112 maps.  Even though this is virtual space (since the buffers are mapped
113 from the buffer cache), we cannot make it arbitrarily large because
114 instantiated VM Buffers (struct buf's) prevent their underlying pages in the
115 buffer cache from being freed.  This can complicate the life of the paging
116 system.
117 .Pp
118 .\" .Sh SEE ALSO
119 .\" .Xr <fillmein> 9
120 .Sh HISTORY
121 The
122 .Nm
123 manual page was originally written by
124 .An Matthew Dillon
125 and first appeared in
126 .Fx 3.1 ,
127 December 1998.