]>
git.saurik.com Git - apple/xnu.git/blob - bsd/sys/buf_internal.h
9558b02f4d410b8cf05086a22e05dd59040c4af6
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)buf.h 8.9 (Berkeley) 3/30/95
71 #ifndef _SYS_BUF_INTERNAL_H_
72 #define _SYS_BUF_INTERNAL_H_
74 #include <sys/appleapiopts.h>
77 #include <sys/queue.h>
78 #include <sys/errno.h>
80 #include <sys/cdefs.h>
85 extern lck_mtx_t
*buf_mtxp
;
86 #define NOLIST ((struct buf *)0x87654321)
89 * The buffer header describes an I/O operation in the kernel.
92 LIST_ENTRY(buf
) b_hash
; /* Hash chain. */
93 LIST_ENTRY(buf
) b_vnbufs
; /* Buffer's associated vnode. */
94 TAILQ_ENTRY(buf
) b_freelist
; /* Free list position if not active. */
95 int b_timestamp
; /* timestamp for queuing operation */
96 long b_whichq
; /* the free list the buffer belongs to */
97 volatile long b_flags
; /* B_* flags. */
98 volatile long b_lflags
; /* BL_BUSY | BL_WANTED flags... protected by buf_mtx */
99 int b_error
; /* errno value. */
100 long b_bufsize
; /* Allocated buffer size. */
101 long b_bcount
; /* Valid bytes in buffer. */
102 long b_resid
; /* Remaining I/O. */
103 dev_t b_dev
; /* Device associated with buffer. */
104 uintptr_t b_datap
; /* Memory, superblocks, indirect etc.*/
105 daddr64_t b_lblkno
; /* Logical block number. */
106 daddr64_t b_blkno
; /* Underlying physical block number. */
107 void (*b_iodone
)(buf_t
, void *); /* Function to call upon completion. */
108 vnode_t b_vp
; /* Device vnode. */
109 struct ucred
*b_rcred
; /* Read credentials reference. */
110 struct ucred
*b_wcred
; /* Write credentials reference. */
111 void * b_upl
; /* Pointer to UPL */
112 buf_t b_real_bp
; /* used to track bp generated through cluster_bp */
113 TAILQ_ENTRY(buf
) b_act
; /* Device driver queue when active */
114 void * b_drvdata
; /* Device driver private use */
115 void * b_fsprivate
; /* filesystem private use */
116 void * b_transaction
; /* journal private use */
117 int b_dirtyoff
; /* Offset in buffer of dirty region. */
118 int b_dirtyend
; /* Offset of end of dirty region. */
119 int b_validoff
; /* Offset in buffer of valid region. */
120 int b_validend
; /* Offset of end of valid region. */
121 proc_t b_proc
; /* Associated proc; NULL if kernel. */
126 int b_stackbrelse
[6];
127 int b_stackgetblk
[6];
132 /* cluster_io definitions for use with io bufs */
133 #define b_uploffset b_bufsize
134 #define b_trans_head b_freelist.tqe_prev
135 #define b_trans_next b_freelist.tqe_next
136 #define b_iostate b_rcred
139 * These flags are kept in b_lflags...
140 * buf_mtxp must be held before examining/updating
142 #define BL_BUSY 0x00000001 /* I/O in progress. */
143 #define BL_WANTED 0x00000002 /* Process wants this buffer. */
144 #define BL_IOBUF 0x00000004 /* buffer allocated via 'buf_alloc' */
148 * mask used by buf_flags... these are the readable external flags
150 #define BUF_X_RDFLAGS (B_CLUSTER | B_PHYS | B_LOCKED | B_DELWRI | B_ASYNC |\
151 B_READ | B_WRITE | B_META | B_PAGEIO)
153 * mask used by buf_clearflags/buf_setflags... these are the writable external flags
155 #define BUF_X_WRFLAGS (B_LOCKED | B_NOCACHE | B_ASYNC | B_READ | B_WRITE | B_PAGEIO)
158 * These flags are kept in b_flags... access is lockless
159 * External flags are defined in buf.h and cannot overlap
162 * these flags are internal... there definition may change
164 #define B_CACHE 0x00010000 /* getblk found us in the cache. */
165 #define B_DONE 0x00020000 /* I/O completed. */
166 #define B_INVAL 0x00040000 /* Does not contain valid info. */
167 #define B_ERROR 0x00080000 /* I/O error occurred. */
168 #define B_EINTR 0x00100000 /* I/O was interrupted */
169 #define B_AGE 0x00200000 /* Move to age queue when I/O done. */
170 #define B_FILTER 0x00400000 /* call b_iodone from biodone as an in-line filter */
171 #define B_CALL 0x00800000 /* Call b_iodone from biodone, assumes b_iodone consumes bp */
172 #define B_RAW 0x01000000 /* Set by physio for raw transfers. */
173 #define B_WASDIRTY 0x02000000 /* page was found dirty in the VM cache */
174 #define B_HDRALLOC 0x04000000 /* zone allocated buffer header */
175 #define B_ZALLOC 0x08000000 /* b_datap is zalloc()ed */
177 * private flags used by the journal layer
179 #define B_NORELSE 0x10000000 /* don't brelse() in bwrite() */
181 * private flags used by by the cluster layer
183 #define B_NEED_IODONE 0x20000000 /* need biodone on the real_bp associated with a cluster_io */
184 #define B_COMMIT_UPL 0x40000000 /* commit/abort the UPL on I/O success/failure */
188 #define B_TAPE 0x80000000 /* Magnetic tape I/O. */
191 /* Flags to low-level allocation routines. */
192 #define B_CLRBUF 0x01 /* Request allocated buffer be cleared. */
193 #define B_SYNC 0x02 /* Do all allocations synchronously. */
194 #define B_NOBUFF 0x04 /* Do not allocate struct buf */
197 extern int niobuf
; /* The number of IO buffer headers for cluster IO */
198 extern int nbuf
; /* The number of buffer headers */
199 extern struct buf
*buf
; /* The buffer headers. */
203 * Definitions for the buffer free lists.
205 #define BQUEUES 6 /* number of free buffer queues */
207 #define BQ_LOCKED 0 /* super-blocks &c */
208 #define BQ_LRU 1 /* lru, useful buffers */
209 #define BQ_AGE 2 /* rubbish */
210 #define BQ_EMPTY 3 /* buffer headers with no memory */
211 #define BQ_META 4 /* buffer containing metadata */
212 #define BQ_LAUNDRY 5 /* buffers that need cleaning */
217 buf_t
alloc_io_buf(vnode_t
, int);
218 void free_io_buf(buf_t
);
220 int allocbuf(struct buf
*, int);
223 void buf_setfilter(buf_t
, void (*)(buf_t
, void *), void *, void **, void **);
226 * Flags for buf_acquire
228 #define BAC_NOWAIT 0x01 /* Don't wait if buffer is busy */
229 #define BAC_REMOVE 0x02 /* Remove from free list once buffer is acquired */
230 #define BAC_SKIP_NONLOCKED 0x04 /* Don't return LOCKED buffers */
231 #define BAC_SKIP_LOCKED 0x08 /* Only return LOCKED buffers */
233 void cluster_init(void);
234 void buf_drop(buf_t
);
235 errno_t
buf_acquire(buf_t
, int, int, int);
237 int count_busy_buffers(void);
238 int count_lock_queue(void);
245 * Stats on usefulness of the buffer cache
248 long bufs_incore
; /* found incore */
249 long bufs_busyincore
; /* found incore. was busy */
250 long bufs_vmhits
; /* not incore. found in VM */
251 long bufs_miss
; /* not incore. not in VM */
252 long bufs_sleeps
; /* buffer starvation */
253 long bufs_eblk
; /* Calls to geteblk */
254 long bufs_iobufmax
; /* Max. number of IO buffers used */
255 long bufs_iobufinuse
; /* number of IO buffers in use */
256 long bufs_iobufsleeps
; /* IO buffer starvation */
260 #endif /* !_SYS_BUF_H_ */