]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/buf.h
xnu-344.26.tar.gz
[apple/xnu.git] / bsd / sys / buf.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)buf.h 8.9 (Berkeley) 3/30/95
61 */
62
63 #ifndef _SYS_BUF_H_
64 #define _SYS_BUF_H_
65
66 #include <sys/appleapiopts.h>
67
68 #ifdef KERNEL
69 #include <sys/queue.h>
70 #include <sys/errno.h>
71 #include <sys/vm.h>
72 #include <sys/cdefs.h>
73
74 #ifdef __APPLE_API_PRIVATE
75
76 #define NOLIST ((struct buf *)0x87654321)
77
78 /*
79 * The buffer header describes an I/O operation in the kernel.
80 */
81 struct buf {
82 LIST_ENTRY(buf) b_hash; /* Hash chain. */
83 LIST_ENTRY(buf) b_vnbufs; /* Buffer's associated vnode. */
84 TAILQ_ENTRY(buf) b_freelist; /* Free list position if not active. */
85 struct proc *b_proc; /* Associated proc; NULL if kernel. */
86 volatile long b_flags; /* B_* flags. */
87 int b_error; /* Errno value. */
88 long b_bufsize; /* Allocated buffer size. */
89 long b_bcount; /* Valid bytes in buffer. */
90 long b_resid; /* Remaining I/O. */
91 dev_t b_dev; /* Device associated with buffer. */
92 struct {
93 caddr_t b_addr; /* Memory, superblocks, indirect etc.*/
94 } b_un;
95 void *b_saveaddr; /* Original b_addr for physio. */
96 daddr_t b_lblkno; /* Logical block number. */
97 daddr_t b_blkno; /* Underlying physical block number. */
98 /* Function to call upon completion. */
99 void (*b_iodone) __P((struct buf *));
100 struct vnode *b_vp; /* Device vnode. */
101 int b_dirtyoff; /* Offset in buffer of dirty region. */
102 int b_dirtyend; /* Offset of end of dirty region. */
103 int b_validoff; /* Offset in buffer of valid region. */
104 int b_validend; /* Offset of end of valid region. */
105 struct ucred *b_rcred; /* Read credentials reference. */
106 struct ucred *b_wcred; /* Write credentials reference. */
107 int b_timestamp; /* timestamp for queuing operation */
108 long b_vectorcount; /* number of vectors in b_vectorlist */
109 void *b_vectorlist; /* vector list for I/O */
110 void *b_pagelist; /* to save pagelist info */
111 long b_vects[2]; /* vectorlist when b_vectorcount is 1 */
112 long b_whichq; /* the free list the buffer belongs to */
113 TAILQ_ENTRY(buf) b_act; /* Device driver queue when active */
114 void *b_drvdata; /* Device driver private use */
115 };
116
117 /*
118 * For portability with historic industry practice, the cylinder number has
119 * to be maintained in the `b_resid' field.
120 */
121 #define b_cylinder b_resid /* Cylinder number for disksort(). */
122
123 /* Device driver compatibility definitions. */
124 #define b_active b_bcount /* Driver queue head: drive active. */
125 #define b_data b_un.b_addr /* b_un.b_addr is not changeable. */
126 #define b_errcnt b_resid /* Retry count while I/O in progress. */
127 #define iodone biodone /* Old name for biodone. */
128 #define iowait biowait /* Old name for biowait. */
129
130 /* cluster_io definitions for use with io bufs */
131 #define b_uploffset b_bufsize
132 #define b_trans_head b_freelist.tqe_prev
133 #define b_trans_next b_freelist.tqe_next
134 #define b_real_bp b_saveaddr
135 #define b_iostate b_rcred
136
137 /* journaling uses this cluster i/o field for its own
138 * purposes because meta data buf's should never go
139 * through the clustering code.
140 */
141 #define b_transaction b_vectorlist
142
143
144
145 /*
146 * These flags are kept in b_flags.
147 */
148 #define B_AGE 0x00000001 /* Move to age queue when I/O done. */
149 #define B_NEEDCOMMIT 0x00000002 /* Append-write in progress. */
150 #define B_ASYNC 0x00000004 /* Start I/O, do not wait. */
151 #define B_BAD 0x00000008 /* Bad block revectoring in progress. */
152 #define B_BUSY 0x00000010 /* I/O in progress. */
153 #define B_CACHE 0x00000020 /* Bread found us in the cache. */
154 #define B_CALL 0x00000040 /* Call b_iodone from biodone. */
155 #define B_DELWRI 0x00000080 /* Delay I/O until buffer reused. */
156 #define B_DIRTY 0x00000100 /* Dirty page to be pushed out async. */
157 #define B_DONE 0x00000200 /* I/O completed. */
158 #define B_EINTR 0x00000400 /* I/O was interrupted */
159 #define B_ERROR 0x00000800 /* I/O error occurred. */
160 #define B_WASDIRTY 0x00001000 /* page was found dirty in the VM cache */
161 #define B_INVAL 0x00002000 /* Does not contain valid info. */
162 #define B_LOCKED 0x00004000 /* Locked in core (not reusable). */
163 #define B_NOCACHE 0x00008000 /* Do not cache block after use. */
164 #define B_PAGEOUT 0x00010000 /* Page out indicator... */
165 #define B_PGIN 0x00020000 /* Pagein op, so swap() can count it. */
166 #define B_PHYS 0x00040000 /* I/O to user memory. */
167 #define B_RAW 0x00080000 /* Set by physio for raw transfers. */
168 #define B_READ 0x00100000 /* Read buffer. */
169 #define B_TAPE 0x00200000 /* Magnetic tape I/O. */
170 #define B_PAGELIST 0x00400000 /* Buffer describes pagelist I/O. */
171 #define B_WANTED 0x00800000 /* Process wants this buffer. */
172 #define B_WRITE 0x00000000 /* Write buffer (pseudo flag). */
173 #define B_WRITEINPROG 0x01000000 /* Write in progress. */
174 #define B_HDRALLOC 0x02000000 /* zone allocated buffer header */
175 #define B_NORELSE 0x04000000 /* don't brelse() in bwrite() */
176 #define B_NEED_IODONE 0x08000000
177 /* need to do a biodone on the */
178 /* real_bp associated with a cluster_io */
179 #define B_COMMIT_UPL 0x10000000
180 /* commit pages in upl when */
181 /* I/O completes/fails */
182 #define B_ZALLOC 0x20000000 /* b_data is zalloc()ed */
183 #define B_META 0x40000000 /* buffer contains meta-data. */
184 #define B_VECTORLIST 0x80000000 /* Used by device drivers. */
185
186
187 /*
188 * Zero out the buffer's data area.
189 */
190 #define clrbuf(bp) { \
191 bzero((bp)->b_data, (u_int)(bp)->b_bcount); \
192 (bp)->b_resid = 0; \
193 }
194
195 /* Flags to low-level allocation routines. */
196 #define B_CLRBUF 0x01 /* Request allocated buffer be cleared. */
197 #define B_SYNC 0x02 /* Do all allocations synchronously. */
198 #define B_NOBUFF 0x04 /* Do not allocate struct buf */
199
200 /* Flags for operation type in getblk() */
201 #define BLK_READ 0x01 /* buffer for read */
202 #define BLK_WRITE 0x02 /* buffer for write */
203 #define BLK_PAGEIN 0x04 /* buffer for pagein */
204 #define BLK_PAGEOUT 0x08 /* buffer for pageout */
205 #define BLK_META 0x10 /* buffer for metadata */
206 #define BLK_CLREAD 0x20 /* buffer for cluster read */
207 #define BLK_CLWRITE 0x40 /* buffer for cluster write */
208
209 extern int nbuf; /* The number of buffer headers */
210 extern struct buf *buf; /* The buffer headers. */
211
212 #endif /* __APPLE_API_PRIVATE */
213
214
215 #ifdef __APPLE_API_UNSTABLE
216 /* Macros to clear/set/test flags. */
217 #define SET(t, f) (t) |= (f)
218 #define CLR(t, f) (t) &= ~(f)
219 #define ISSET(t, f) ((t) & (f))
220 #endif /* __APPLE_API_UNSTABLE */
221
222 #ifdef __APPLE_API_PRIVATE
223 /*
224 * Definitions for the buffer free lists.
225 */
226 #define BQUEUES 6 /* number of free buffer queues */
227
228 #define BQ_LOCKED 0 /* super-blocks &c */
229 #define BQ_LRU 1 /* lru, useful buffers */
230 #define BQ_AGE 2 /* rubbish */
231 #define BQ_EMPTY 3 /* buffer headers with no memory */
232 #define BQ_META 4 /* buffer containing metadata */
233 #define BQ_LAUNDRY 5 /* buffers that need cleaning */
234 #endif /* __APPLE_API_PRIVATE */
235
236 __BEGIN_DECLS
237 #ifdef __APPLE_API_UNSTABLE
238 int allocbuf __P((struct buf *, int));
239 void bawrite __P((struct buf *));
240 void bdwrite __P((struct buf *));
241 void biodone __P((struct buf *));
242 int biowait __P((struct buf *));
243 int bread __P((struct vnode *, daddr_t, int,
244 struct ucred *, struct buf **));
245 int meta_bread __P((struct vnode *, daddr_t, int,
246 struct ucred *, struct buf **));
247 int breada __P((struct vnode *, daddr_t, int, daddr_t, int,
248 struct ucred *, struct buf **));
249 int breadn __P((struct vnode *, daddr_t, int, daddr_t *, int *, int,
250 struct ucred *, struct buf **));
251 void brelse __P((struct buf *));
252 void bremfree __P((struct buf *));
253 void bufinit __P((void));
254 void bwillwrite __P((void));
255 int bwrite __P((struct buf *));
256 struct buf *getblk __P((struct vnode *, daddr_t, int, int, int, int));
257 struct buf *geteblk __P((int));
258 struct buf *incore __P((struct vnode *, daddr_t));
259 u_int minphys __P((struct buf *bp));
260 int physio __P((void (*)(struct buf *), struct buf *, dev_t, int , u_int (*)(struct buf *), struct uio *, int ));
261 int count_busy_buffers __P((void));
262 struct buf *alloc_io_buf __P((struct vnode *, int));
263 void free_io_buf __P((struct buf *));
264 void reassignbuf __P((struct buf *, struct vnode *));
265 #endif /* __APPLE_API_UNSTABLE */
266 __END_DECLS
267
268 #ifdef __APPLE_API_PRIVATE
269 /*
270 * Stats on usefulness of the buffer cache
271 */
272 struct bufstats {
273 long bufs_incore; /* found incore */
274 long bufs_busyincore; /* found incore. was busy */
275 long bufs_vmhits; /* not incore. found in VM */
276 long bufs_miss; /* not incore. not in VM */
277 long bufs_sleeps; /* buffer starvation */
278 long bufs_eblk; /* Calls to geteblk */
279 long bufs_iobufmax; /* Max. number of IO buffers used */
280 long bufs_iobufinuse; /* number of IO buffers in use */
281 long bufs_iobufsleeps; /* IO buffer starvation */
282 };
283 #endif /* __APPLE_API_PRIVATE */
284
285 #endif /* KERNEL */
286 #endif /* !_SYS_BUF_H_ */