188b85f74SMatthew Dillon.\" Copyright (c) 1998 288b85f74SMatthew Dillon.\" The Regents of the University of California. All rights reserved. 388b85f74SMatthew Dillon.\" 488b85f74SMatthew Dillon.\" Redistribution and use in source and binary forms, with or without 588b85f74SMatthew Dillon.\" modification, are permitted provided that the following conditions 688b85f74SMatthew Dillon.\" are met: 788b85f74SMatthew Dillon.\" 1. Redistributions of source code must retain the above copyright 888b85f74SMatthew Dillon.\" notice, this list of conditions and the following disclaimer. 988b85f74SMatthew Dillon.\" 2. Redistributions in binary form must reproduce the above copyright 1088b85f74SMatthew Dillon.\" notice, this list of conditions and the following disclaimer in the 1188b85f74SMatthew Dillon.\" documentation and/or other materials provided with the distribution. 1288b85f74SMatthew Dillon.\" 3. All advertising materials mentioning features or use of this software 1388b85f74SMatthew Dillon.\" must display the following acknowledgement: 1488b85f74SMatthew Dillon.\" This product includes software developed by the University of 1588b85f74SMatthew Dillon.\" California, Berkeley and its contributors. 1688b85f74SMatthew Dillon.\" 4. Neither the name of the University nor the names of its contributors 1788b85f74SMatthew Dillon.\" may be used to endorse or promote products derived from this software 1888b85f74SMatthew Dillon.\" without specific prior written permission. 1988b85f74SMatthew Dillon.\" 2088b85f74SMatthew Dillon.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2188b85f74SMatthew Dillon.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2288b85f74SMatthew Dillon.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2388b85f74SMatthew Dillon.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2488b85f74SMatthew Dillon.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2588b85f74SMatthew Dillon.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2688b85f74SMatthew Dillon.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2788b85f74SMatthew Dillon.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2888b85f74SMatthew Dillon.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2988b85f74SMatthew Dillon.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3088b85f74SMatthew Dillon.\" SUCH DAMAGE. 3188b85f74SMatthew Dillon.\" 327f3dea24SPeter Wemm.\" $FreeBSD$ 3388b85f74SMatthew Dillon.\" 3488b85f74SMatthew Dillon.Dd December 22, 1998 3588b85f74SMatthew Dillon.Dt BUF 9 3688b85f74SMatthew Dillon.Os 3788b85f74SMatthew Dillon.Sh NAME 389264a4f3SChad David.Nm buf 39eb083802SRuslan Ermilov.Nd "kernel buffer I/O scheme used in FreeBSD VM system" 4088b85f74SMatthew Dillon.Sh DESCRIPTION 4188b85f74SMatthew DillonThe kernel implements a KVM abstraction of the buffer cache which allows it 4288b85f74SMatthew Dillonto map potentially disparate vm_page's into contiguous KVM for use by 433a858f37SHiten Pandya(mainly file system) devices and device I/O. 443a858f37SHiten PandyaThis abstraction supports 4588b85f74SMatthew Dillonblock sizes from DEV_BSIZE (usually 512) to upwards of several pages or more. 4688b85f74SMatthew DillonIt also supports a relatively primitive byte-granular valid range and dirty 473a858f37SHiten Pandyarange currently hardcoded for use by NFS. 483a858f37SHiten PandyaThe code implementing the 49a169de1eSAlexey ZelkinVM Buffer abstraction is mostly concentrated in 50a169de1eSAlexey Zelkin.Pa /usr/src/sys/kern/vfs_bio.c . 5188b85f74SMatthew Dillon.Pp 5288b85f74SMatthew DillonOne of the most important things to remember when dealing with buffer pointers 5388b85f74SMatthew Dillon(struct buf) is that the underlying pages are mapped directly from the buffer 543a858f37SHiten Pandyacache. 553a858f37SHiten PandyaNo data copying occurs in the scheme proper, though some file systems 563a858f37SHiten Pandyasuch as UFS do have to copy a little when dealing with file fragments. 573a858f37SHiten PandyaThe second most important thing to remember is that due to the underlying page 5888b85f74SMatthew Dillonmapping, the b_data base pointer in a buf is always *page* aligned, not 593a858f37SHiten Pandya*block* aligned. 603a858f37SHiten PandyaWhen you have a VM buffer representing some b_offset and 6188b85f74SMatthew Dillonb_size, the actual start of the buffer is (b_data + (b_offset & PAGE_MASK)) 623a858f37SHiten Pandyaand not just b_data. 633a858f37SHiten PandyaFinally, the VM system's core buffer cache supports 643a858f37SHiten Pandyavalid and dirty bits (m->valid, m->dirty) for pages in DEV_BSIZE chunks. 653a858f37SHiten PandyaThus 6688b85f74SMatthew Dillona platform with a hardware page size of 4096 bytes has 8 valid and 8 dirty 673a858f37SHiten Pandyabits. 683a858f37SHiten PandyaThese bits are generally set and cleared in groups based on the device 693a858f37SHiten Pandyablock size of the device backing the page. 703a858f37SHiten PandyaComplete page's worth are often 715203edcdSRuslan Ermilovreferred to using the VM_PAGE_BITS_ALL bitmask (i.e., 0xFF if the hardware page 7288b85f74SMatthew Dillonsize is 4096). 7388b85f74SMatthew Dillon.Pp 7488b85f74SMatthew DillonVM buffers also keep track of a byte-granular dirty range and valid range. 753a858f37SHiten PandyaThis feature is normally only used by the NFS subsystem. 763a858f37SHiten PandyaI'm not sure why it 7788b85f74SMatthew Dillonis used at all, actually, since we have DEV_BSIZE valid/dirty granularity 783a858f37SHiten Pandyawithin the VM buffer. 793a858f37SHiten PandyaIf a buffer dirty operation creates a 'hole', 803a858f37SHiten Pandyathe dirty range will extend to cover the hole. 813a858f37SHiten PandyaIf a buffer validation 8288b85f74SMatthew Dillonoperation creates a 'hole' the byte-granular valid range is left alone and 833a858f37SHiten Pandyawill not take into account the new extension. 843a858f37SHiten PandyaThus the whole byte-granular 8588b85f74SMatthew Dillonabstraction is considered a bad hack and it would be nice if we could get rid 8688b85f74SMatthew Dillonof it completely. 8788b85f74SMatthew Dillon.Pp 8888b85f74SMatthew DillonA VM buffer is capable of mapping the underlying VM cache pages into KVM in 8988b85f74SMatthew Dillonorder to allow the kernel to directly manipulate the data associated with 903a858f37SHiten Pandyathe (vnode,b_offset,b_size). 913a858f37SHiten PandyaThe kernel typically unmaps VM buffers the moment 9288b85f74SMatthew Dillonthey are no longer needed but often keeps the 'struct buf' structure 9388b85f74SMatthew Dilloninstantiated and even bp->b_pages array instantiated despite having unmapped 943a858f37SHiten Pandyathem from KVM. 953a858f37SHiten PandyaIf a page making up a VM buffer is about to undergo I/O, the 9688b85f74SMatthew Dillonsystem typically unmaps it from KVM and replaces the page in the b_pages[] 973a858f37SHiten Pandyaarray with a place-marker called bogus_page. 983a858f37SHiten PandyaThe place-marker forces any kernel 9988b85f74SMatthew Dillonsubsystems referencing the associated struct buf to re-lookup the associated 1003a858f37SHiten Pandyapage. 1013a858f37SHiten PandyaI believe the place-marker hack is used to allow sophisticated devices 10288b85f74SMatthew Dillonsuch as file system devices to remap underlying pages in order to deal with, 103b82af3f5SMike Pritchardfor example, re-mapping a file fragment into a file block. 10488b85f74SMatthew Dillon.Pp 1053a858f37SHiten PandyaVM buffers are used to track I/O operations within the kernel. 1063a858f37SHiten PandyaUnfortunately, 10788b85f74SMatthew Dillonthe I/O implementation is also somewhat of a hack because the kernel wants 10888b85f74SMatthew Dillonto clear the dirty bit on the underlying pages the moment it queues the I/O 1093a858f37SHiten Pandyato the VFS device, not when the physical I/O is actually initiated. 1103a858f37SHiten PandyaThis 11188b85f74SMatthew Dilloncan create confusion within file system devices that use delayed-writes because 1123a858f37SHiten Pandyayou wind up with pages marked clean that are actually still dirty. 1133a858f37SHiten PandyaIf not 1145203edcdSRuslan Ermilovtreated carefully, these pages could be thrown away! 1155203edcdSRuslan ErmilovIndeed, a number of 116a169de1eSAlexey Zelkinserious bugs related to this hack were not fixed until the 2.2.8/3.0 release. 1175203edcdSRuslan ErmilovThe kernel uses an instantiated VM buffer (i.e., struct buf) to place-mark pages 1183a858f37SHiten Pandyain this special state. 1193a858f37SHiten PandyaThe buffer is typically flagged B_DELWRI. 1203a858f37SHiten PandyaWhen a 1213a858f37SHiten Pandyadevice no longer needs a buffer it typically flags it as B_RELBUF. 1223a858f37SHiten PandyaDue to 12388b85f74SMatthew Dillonthe underlying pages being marked clean, the B_DELWRI|B_RELBUF combination must 12488b85f74SMatthew Dillonbe interpreted to mean that the buffer is still actually dirty and must be 1253a858f37SHiten Pandyawritten to its backing store before it can actually be released. 1263a858f37SHiten PandyaIn the case 12788b85f74SMatthew Dillonwhere B_DELWRI is not set, the underlying dirty pages are still properly 12888b85f74SMatthew Dillonmarked as dirty and the buffer can be completely freed without losing that 129d5a8819cSHiten Pandyaclean/dirty state information. 130bf7f20c2SRuslan Ermilov(XXX do we have to check other flags in 131bf7f20c2SRuslan Ermilovregards to this situation ???) 13288b85f74SMatthew Dillon.Pp 13388b85f74SMatthew DillonThe kernel reserves a portion of its KVM space to hold VM Buffer's data 1343a858f37SHiten Pandyamaps. 1353a858f37SHiten PandyaEven though this is virtual space (since the buffers are mapped 13688b85f74SMatthew Dillonfrom the buffer cache), we cannot make it arbitrarily large because 13788b85f74SMatthew Dilloninstantiated VM Buffers (struct buf's) prevent their underlying pages in the 1383a858f37SHiten Pandyabuffer cache from being freed. 1393a858f37SHiten PandyaThis can complicate the life of the paging 14088b85f74SMatthew Dillonsystem. 14188b85f74SMatthew Dillon.Pp 1425d70612bSMike Pritchard.\" .Sh SEE ALSO 1435d70612bSMike Pritchard.\" .Xr <fillmein> 9 14488b85f74SMatthew Dillon.Sh HISTORY 14588b85f74SMatthew DillonThe 14688b85f74SMatthew Dillon.Nm 147a169de1eSAlexey Zelkinmanual page was originally written by 148a169de1eSAlexey Zelkin.An Matthew Dillon 149a169de1eSAlexey Zelkinand first appeared in 150a169de1eSAlexey Zelkin.Fx 3.1 , 1515d70612bSMike PritchardDecember 1998. 152